0
votes

I am referring to documentation provided by azure at

https://docs.microsoft.com/en-us/azure/storage/common/storage-metrics-in-azure-monitor#read-metric-values-with-the-net-sdk

I have made changes and make the code work for java using azure-mgmt-monitor dependency. Here is the code

public void listStorageMetricDefinition() {
    String resourceId = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}";
    String subscriptionId = "*****************************";
    String tenantId = "*****************************";
    String applicationId = "*****************************";
    String accessKey = "*****************************";

    ApplicationTokenCredentials credentials = (ApplicationTokenCredentials) new ApplicationTokenCredentials(
            applicationId, tenantId, accessKey, AzureEnvironment.AZURE).withDefaultSubscriptionId(subscriptionId);
    MonitorManagementClientImpl clientImpl = new MonitorManagementClientImpl(credentials);

    Date startTime = DateTime.now().minusMinutes(30).toDate();
    Date endTime = DateTime.now().toDate();
    //DateTime must be in below format
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    String startInterval = dateFormat.format(startTime);
    String endInterval = dateFormat.format(endTime);
    String timespan = startInterval + "/" + endInterval;
    Period interval = Period.minutes(1);
    String metricNames = "Egress";
    String aggregation = "Total";
    Integer top = null;
    String orderby = null;
    String filter = null;
    String metricNamespace = null;

    ResponseInner response = clientImpl.metrics().list(resourceId, timespan, interval, metricNames, aggregation,
            top, orderby, filter, null, metricNamespace);
    List<MetricInner> value = response.value();
    for (MetricInner metric : value) {
        System.out.println("id " + metric.id());
        System.out.println("name " + metric.name().value());
        System.out.println("type " + metric.type());
        System.out.println("unit " + metric.unit());
        List<TimeSeriesElement> timeseries = metric.timeseries();
        timeseries.forEach(ts -> {
            ts.data().forEach(dt -> {
                System.out.println(dt.timeStamp() + "--" + dt.total());
            });
        });
    }
}

By using above I am able to read the metrics values at storage account level, but how can I find the metrics at container level? e.g. if I have 3 containers inside my storage account, I need to find the metrics for each container instead for complete storage account.

Please suggest if there are other ways to find metrics at container level.

1
As far as I knew, we have no way to get the metric for Azure storage container. We just can get metrics for Azure storage account level or Azure storage container level. For more details, please refer to docs.microsoft.com/en-us/azure/storage/common/…Jim Xu
I found a solution for this, we can enable the storage analytics for the storage account that will create a container named $logs and then use those logs to build your metrics as the logs contain each and every detail.Prabhat

1 Answers

0
votes

There is not direct way of doing this, but one can achieve this by configuring monitoring for the storage account. Follow the below link to configure monitoring,

https://docs.microsoft.com/en-us/azure/storage/common/storage-monitor-storage-account

Once storage account is configured for monitoring, it will create a new container with name $logs in your storage account. This new container is not visible in azure portal but you can view and explore this new container using Azure Storage Explorer tool. The link to download the tool is given below.

https://azure.microsoft.com/en-us/features/storage-explorer/

The logs inside the $logs container are segregated on the basis of date and time in separate folders.

/blob/yyyy/MM/dd/HHmm/000000.log

/blob/yyyy/MM/dd/HHmm/000001.log

where mm is always going to be 00.

enter image description here

The schema for logs can be found in azure documentation at location.

https://docs.microsoft.com/en-us/rest/api/storageservices/storage-analytics-log-format

One can read the log file using the schema format and create useful metrics out if it.