|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.monitor.samples; |
| 5 | + |
| 6 | +import com.azure.core.credential.TokenCredential; |
| 7 | +import com.azure.core.http.HttpClient; |
| 8 | +import com.azure.core.http.policy.HttpLogDetailLevel; |
| 9 | +import com.azure.core.http.rest.PagedIterable; |
| 10 | +import com.azure.core.management.AzureEnvironment; |
| 11 | +import com.azure.identity.DefaultAzureCredentialBuilder; |
| 12 | +import com.azure.resourcemanager.AzureResourceManager; |
| 13 | +import com.azure.resourcemanager.monitor.models.EventData; |
| 14 | +import com.azure.resourcemanager.monitor.models.Metric; |
| 15 | +import com.azure.resourcemanager.monitor.models.MetricCollection; |
| 16 | +import com.azure.resourcemanager.monitor.models.MetricDefinition; |
| 17 | +import com.azure.resourcemanager.monitor.models.MetricValue; |
| 18 | +import com.azure.resourcemanager.monitor.models.TimeSeriesElement; |
| 19 | +import com.azure.resourcemanager.monitor.fluent.models.MetadataValueInner; |
| 20 | +import com.azure.core.management.Region; |
| 21 | +import com.azure.core.management.profile.AzureProfile; |
| 22 | +import com.azure.resourcemanager.resources.fluentcore.utils.ResourceManagerUtils; |
| 23 | +import com.azure.resourcemanager.samples.Utils; |
| 24 | +import com.azure.resourcemanager.storage.models.AccessTier; |
| 25 | +import com.azure.resourcemanager.storage.models.StorageAccount; |
| 26 | +import com.azure.resourcemanager.storage.models.StorageAccountKey; |
| 27 | +import com.azure.storage.blob.BlobClient; |
| 28 | +import com.azure.storage.blob.BlobContainerClient; |
| 29 | +import com.azure.storage.blob.BlobContainerClientBuilder; |
| 30 | +import com.azure.storage.blob.BlobServiceClient; |
| 31 | +import com.azure.storage.blob.BlobServiceClientBuilder; |
| 32 | +import com.azure.storage.blob.models.BlobAnalyticsLogging; |
| 33 | +import com.azure.storage.blob.models.BlobMetrics; |
| 34 | +import com.azure.storage.blob.models.BlobRetentionPolicy; |
| 35 | +import com.azure.storage.blob.models.BlobServiceProperties; |
| 36 | +import com.azure.storage.blob.specialized.BlockBlobClient; |
| 37 | +import org.apache.commons.io.IOUtils; |
| 38 | + |
| 39 | +import java.io.ByteArrayInputStream; |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.InputStream; |
| 42 | +import java.time.Duration; |
| 43 | +import java.time.OffsetDateTime; |
| 44 | +import java.util.List; |
| 45 | + |
| 46 | +/** |
| 47 | + * This sample shows examples of retrieving metrics and activity logs for Storage Account. |
| 48 | + * - List all metric definitions available for a storage account |
| 49 | + * - Retrieve and show metrics for the past 7 days for Transactions where |
| 50 | + * - Api name was 'PutBlob' and |
| 51 | + * - response type was 'Success' and |
| 52 | + * - Geo type was 'Primary' |
| 53 | + * - Retrieve and show all activity logs for the past 7 days for the same Storage account. |
| 54 | + */ |
| 55 | +public final class QueryMetricsAndActivityLogs { |
| 56 | + |
| 57 | + /** |
| 58 | + * Main function which runs the actual sample. |
| 59 | + * @param azureResourceManager instance of the azure client |
| 60 | + * @return true if sample runs successfully |
| 61 | + */ |
| 62 | + public static boolean runSample(AzureResourceManager azureResourceManager) throws IOException { |
| 63 | + final String storageAccountName = Utils.randomResourceName(azureResourceManager, "saMonitor", 20); |
| 64 | + final String rgName = Utils.randomResourceName(azureResourceManager, "rgMonitor", 20); |
| 65 | + |
| 66 | + try { |
| 67 | + // ============================================================ |
| 68 | + // Create a storage account |
| 69 | + |
| 70 | + System.out.println("Creating a Storage Account"); |
| 71 | + |
| 72 | + StorageAccount storageAccount = azureResourceManager.storageAccounts().define(storageAccountName) |
| 73 | + .withRegion(Region.US_EAST) |
| 74 | + .withNewResourceGroup(rgName) |
| 75 | + .withBlobStorageAccountKind() |
| 76 | + .withAccessTier(AccessTier.COOL) |
| 77 | + .create(); |
| 78 | + |
| 79 | + System.out.println("Created a Storage Account:"); |
| 80 | + Utils.print(storageAccount); |
| 81 | + |
| 82 | + List<StorageAccountKey> storageAccountKeys = storageAccount.getKeys(); |
| 83 | + final String storageConnectionString = String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s", |
| 84 | + storageAccount.name(), |
| 85 | + storageAccountKeys.get(0).value()); |
| 86 | + |
| 87 | + // Add some blob transaction events |
| 88 | + addBlobTransactions(storageConnectionString, storageAccount.manager().httpPipeline().getHttpClient()); |
| 89 | + |
| 90 | + OffsetDateTime recordDateTime = OffsetDateTime.now(); |
| 91 | + // get metric definitions for storage account. |
| 92 | + for (MetricDefinition metricDefinition : azureResourceManager.metricDefinitions().listByResource(storageAccount.id())) { |
| 93 | + // find metric definition for Transactions |
| 94 | + if (metricDefinition.name().localizedValue().equalsIgnoreCase("transactions")) { |
| 95 | + // get metric records |
| 96 | + MetricCollection metricCollection = metricDefinition.defineQuery() |
| 97 | + .startingFrom(recordDateTime.minusDays(7)) |
| 98 | + .endsBefore(recordDateTime) |
| 99 | + .withAggregation("Average") |
| 100 | + .withInterval(Duration.ofMinutes(5)) |
| 101 | + .withOdataFilter("apiName eq 'PutBlob' and responseType eq 'Success' and geoType eq 'Primary'") |
| 102 | + .execute(); |
| 103 | + |
| 104 | + System.out.println("Metrics for '" + storageAccount.id() + "':"); |
| 105 | + System.out.println("Namespacse: " + metricCollection.namespace()); |
| 106 | + System.out.println("Query time: " + metricCollection.timespan()); |
| 107 | + System.out.println("Time Grain: " + metricCollection.interval()); |
| 108 | + System.out.println("Cost: " + metricCollection.cost()); |
| 109 | + |
| 110 | + for (Metric metric : metricCollection.metrics()) { |
| 111 | + System.out.println("\tMetric: " + metric.name().localizedValue()); |
| 112 | + System.out.println("\tType: " + metric.type()); |
| 113 | + System.out.println("\tUnit: " + metric.unit()); |
| 114 | + System.out.println("\tTime Series: "); |
| 115 | + for (TimeSeriesElement timeElement : metric.timeseries()) { |
| 116 | + System.out.println("\t\tMetadata: "); |
| 117 | + for (MetadataValueInner metadata : timeElement.metadatavalues()) { |
| 118 | + System.out.println("\t\t\t" + metadata.name().localizedValue() + ": " + metadata.value()); |
| 119 | + } |
| 120 | + System.out.println("\t\tData: "); |
| 121 | + for (MetricValue data : timeElement.data()) { |
| 122 | + System.out.println("\t\t\t" + data.timestamp() |
| 123 | + + " : (Min) " + data.minimum() |
| 124 | + + " : (Max) " + data.maximum() |
| 125 | + + " : (Avg) " + data.average() |
| 126 | + + " : (Total) " + data.total() |
| 127 | + + " : (Count) " + data.count()); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // get activity logs for the same period. |
| 136 | + PagedIterable<EventData> logs = azureResourceManager.activityLogs().defineQuery() |
| 137 | + .startingFrom(recordDateTime.minusDays(7)) |
| 138 | + .endsBefore(recordDateTime) |
| 139 | + .withAllPropertiesInResponse() |
| 140 | + .filterByResource(storageAccount.id()) |
| 141 | + .execute(); |
| 142 | + |
| 143 | + System.out.println("Activity logs for the Storage Account:"); |
| 144 | + |
| 145 | + for (EventData event : logs) { |
| 146 | + if (event.eventName() != null) { |
| 147 | + System.out.println("\tEvent: " + event.eventName().localizedValue()); |
| 148 | + } |
| 149 | + if (event.operationName() != null) { |
| 150 | + System.out.println("\tOperation: " + event.operationName().localizedValue()); |
| 151 | + } |
| 152 | + System.out.println("\tCaller: " + event.caller()); |
| 153 | + System.out.println("\tCorrelationId: " + event.correlationId()); |
| 154 | + System.out.println("\tSubscriptionId: " + event.subscriptionId()); |
| 155 | + } |
| 156 | + |
| 157 | + return true; |
| 158 | + } finally { |
| 159 | + if (azureResourceManager.resourceGroups().getByName(rgName) != null) { |
| 160 | + System.out.println("Deleting Resource Group: " + rgName); |
| 161 | + azureResourceManager.resourceGroups().beginDeleteByName(rgName); |
| 162 | + System.out.println("Deleted Resource Group: " + rgName); |
| 163 | + } else { |
| 164 | + System.out.println("Did not create any resources in Azure. No clean up is necessary"); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Main entry point. |
| 171 | + * @param args the parameters |
| 172 | + */ |
| 173 | + public static void main(String[] args) { |
| 174 | + try { |
| 175 | + |
| 176 | + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); |
| 177 | + final TokenCredential credential = new DefaultAzureCredentialBuilder() |
| 178 | + .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint()) |
| 179 | + .build(); |
| 180 | + |
| 181 | + AzureResourceManager azureResourceManager = AzureResourceManager |
| 182 | + .configure() |
| 183 | + .withLogLevel(HttpLogDetailLevel.BASIC) |
| 184 | + .authenticate(credential, profile) |
| 185 | + .withDefaultSubscription(); |
| 186 | + |
| 187 | + // Print selected subscription |
| 188 | + System.out.println("Selected subscription: " + azureResourceManager.subscriptionId()); |
| 189 | + |
| 190 | + runSample(azureResourceManager); |
| 191 | + } catch (Exception e) { |
| 192 | + System.out.println(e.getMessage()); |
| 193 | + e.printStackTrace(); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private static void addBlobTransactions(String storageConnectionString, HttpClient httpClient) throws IOException { |
| 198 | + // Get the script to upload |
| 199 | + // |
| 200 | + try (InputStream scriptFileAsStream = QueryMetricsAndActivityLogs.class.getResourceAsStream("/install_apache.sh")) { |
| 201 | + |
| 202 | + // Get the size of the stream |
| 203 | + // |
| 204 | + byte[] scriptFileBytes = IOUtils.toByteArray(scriptFileAsStream); |
| 205 | + ByteArrayInputStream scriptFileStream = new ByteArrayInputStream(scriptFileBytes); |
| 206 | + int fileSize = scriptFileBytes.length; |
| 207 | + |
| 208 | + // Upload the script file as block blob |
| 209 | + // |
| 210 | + BlobContainerClient blobContainerClient = new BlobContainerClientBuilder() |
| 211 | + .connectionString(storageConnectionString) |
| 212 | + .containerName("scripts") |
| 213 | + .httpClient(httpClient) |
| 214 | + .buildClient(); |
| 215 | + |
| 216 | + blobContainerClient.create(); |
| 217 | + |
| 218 | + // Get the service properties. |
| 219 | + BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() |
| 220 | + .connectionString(storageConnectionString) |
| 221 | + .httpClient(httpClient) |
| 222 | + .buildClient(); |
| 223 | + BlobServiceProperties serviceProps = blobServiceClient.getProperties(); |
| 224 | + |
| 225 | + // configure Storage logging and metrics |
| 226 | + BlobAnalyticsLogging logProps = new BlobAnalyticsLogging() |
| 227 | + .setRead(true) |
| 228 | + .setWrite(true) |
| 229 | + .setRetentionPolicy(new BlobRetentionPolicy() |
| 230 | + .setEnabled(true) |
| 231 | + .setDays(2)) |
| 232 | + .setVersion("1.0"); |
| 233 | + serviceProps.setLogging(logProps); |
| 234 | + |
| 235 | + BlobMetrics metricProps = new BlobMetrics() |
| 236 | + .setEnabled(true) |
| 237 | + .setIncludeApis(true) |
| 238 | + .setRetentionPolicy(new BlobRetentionPolicy() |
| 239 | + .setEnabled(true) |
| 240 | + .setDays(2)) |
| 241 | + .setVersion("1.0"); |
| 242 | + serviceProps.setHourMetrics(metricProps); |
| 243 | + serviceProps.setMinuteMetrics(metricProps); |
| 244 | + |
| 245 | + // Set the default service version to be used for anonymous requests. |
| 246 | + serviceProps.setDefaultServiceVersion("2015-04-05"); |
| 247 | + |
| 248 | + // Set the service properties. |
| 249 | + blobServiceClient.setProperties(serviceProps); |
| 250 | + |
| 251 | + BlobClient blobClient = blobContainerClient.getBlobClient("install_apache.sh"); |
| 252 | + BlockBlobClient blockBlobClient = blobClient.getBlockBlobClient(); |
| 253 | + blockBlobClient.upload(scriptFileStream, fileSize); |
| 254 | + |
| 255 | + // give sometime for the infrastructure to process the records and fit into time grain. |
| 256 | + ResourceManagerUtils.sleep(Duration.ofMinutes(6)); |
| 257 | + } |
| 258 | + } |
| 259 | +} |
0 commit comments