Tuesday, December 27, 2011

Azure Storage Analytics for Azure Storage –Blob, Queue and Tables - Metrics example

Metrics – Using Metrics to track storage usage –
To enable Azure Storage Analytics metrics on Azure storage refer to the link for library code – Azure Storage Analytics for Azure Storage Services – Logging example.
You need complete library code and for enabling Azure Storage Analytics metrics for storage use method – btnSetServiceSettings_Click and to verify use btnGetServiceSettings_Click method.
So till this point we have already enabled Azure Storage Analytics Metrics for blob storage. Now the Azure Storage Analytics metric data for azure BLOB storagewill be collected in tables named as - $MetricsCapaciltyBlob and $MetricsTransactionsBlob.

The screenshot showing the tables list that hold storage analytics metrics data is as shown above.
To read metrics information from above mentioned tables, we need to add two more classes to our storage extension library. I have named these classes as - MetricsEntities and MetricsHelper. The detailed code for these two classes can be found out on link – Azure Storage Analytics Metrics Classes.
So our final project structure will be as shown below –


I have a button labeled as - Dump Metrics on my analytics.aspx page; which I will use for retrieving metrics information and save in CSV format. To save metrics information in CSV format I have added a key in my web.config file as shown –
<appSettings>
    <add key="TableFilePath" value="c:\kunal\metrics\kunaltable.csv"/>
    <add key="BlobFilePath" value="c:\kunal\metrics\kunablob.csv"/>
    <add key="QueueFilePath" value="c:\kunal\metrics\kunaqueue.csv"/>
    <add key="BlobCapacityFilePath" value="c:\kunal\metrics\kunablobcapacity.csv"/>
  </appSettings>

Make sure that, ASPNET user has full access to c:\kunal\metrics\ (or folder of your choice which you will use in above configuration settings) folder as I am using StreamWriter class to create CSV file. So provide access to folder of your choice else you will end up with Access Denied error.
Code to read metrics information from above table is as follows –
protected void btnDumpMetrics_Click(object sender, EventArgs e)
        {
            StorageCredentialsAccountAndKey storageCredentials = new StorageCredentialsAccountAndKey("YourStorageAccount", "YourStorageAccountKey");
//alternatively you can read above key and account name from configuration file web.config of your web role project.
            CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            DateTime startTimeOfSearch = new DateTime(2011, 12, 20);//here provide date of your choice.
            DateTime endTimeOfSearch = DateTime.UtcNow;

            //dump metrics for blob storage service
            MetricsHelper.DumpTransactionsMetrics(tableClient, "Blob", startTimeOfSearch.ToUniversalTime(), endTimeOfSearch.ToUniversalTime(), ConfigurationManager.AppSettings["BlobFilePath"].ToString());           
           
            //Capacity is available only for BLOB service
            MetricsHelper.DumpCapacityMetrics(tableClient, "Blob", startTimeOfSearch, endTimeOfSearch, ConfigurationManager.AppSettings["BlobCapacityFilePath"].ToString());           
        }
This will create CSV file to the mentioned path having Storage Analytics Metrics data. Then this CSV file can be used to create various reports as per choice.

Hope this helps.
Cheers…

Happy Analyzing!!!

1 comment: