Tuesday, December 27, 2011

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


Today I will be going to explain about turning on Logging and Metrics for Azure storage like Blob, Queue and Table. Logging and metrics are two parts of Storage Analytics in Windows Azure. I will be listing few code samples also.
Before going further I will highly recommend you to understand Need for Storage Analytics and What Storage Analytics is?.
Assuming you have gone through the above link and has a fair idea about storage analytics, let’s understand how Azure storage analytics can be used for azure storage.
Logging – Using logs to track storage request for Azure storage –
Create a sample cloud application with one web role in it. We will use this web role to test the azure storage analytics logging and metrics data.
To enable logging for azure storage we will first create a library which will contain all the necessary functions. The Microsoft azure storage dll - Microsoft.WindowsAzure.StorageClient does not have any specific method related to Storage analytics logging and metrics. Therefore it is mandatory for us to write extensions methods for all the storage services. MSDN blog has an excellent article listed here along with code. Create a library project with suitable name and copy the code for classes, AnalyticsSettings, AnalyticsSettingsExtensions, SettingsSerializableHelper. Just for sake of my understanding I have changed the name of class AnalyticsSettings to StorageAnalyticsSettings.
So your final project in solution explorer will look as follows along with library.

Add reference of storage extension library to web role. Now I have added a page named as Analytics.aspx in my web role to test the storage analytics library.

Add using statement for following libraries in addition to the existing one –
using System.Configuration;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure;
using[your storage library name]AzureStorageExtensions;
using System.IO;

I have added a button named as Set Service Settings. On click of it I will be applying all the Azure Storage Analytics settings for Azure storage service.

protected void btnSetServiceSettings_Click(object sender, EventArgs e)
{
StorageCredentialsAccountAndKey storageCredentials = new StorageCredentialsAccountAndKey("YourStorageAccountName", "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);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

StorageAnalyticsSettings settings = new StorageAnalyticsSettings()
{
//to TuRn on logging AND METRICS
LogType = LoggingLevel.Delete | LoggingLevel.Read | LoggingLevel.Write,
IsLogRetentionPolicyEnabled = true,//enable logging
       LogRetentionInDays = 4,
IsMetricsRetentionPolicyEnabled = true,//enable metrics
MetricsRetentionInDays = 7,
MetricsType = MetricsType.All

                //to turn off logging and metrics
                //LogType = LoggingLevel.None,               
                //IsLogRetentionPolicyEnabled = false,
                //IsMetricsRetentionPolicyEnabled = false,
                //MetricsType = MetricsType.None
};

//applying above settings to blob service service
StorageAnalyticsSettingsExtension.SetServiceSettings(blobClient, settings);
//applying above logging settings to table service
StorageAnalyticsSettingsExtension.SetServiceSettings(tableClient, settings);
//applying above logging settings to queue service            StorageAnalyticsSettingsExtension.SetServiceSettings(queueClient,storageAccount.QueueEndpoint, settings);
}

The settings above will get applied to blob storage service when above code is executed. Now If I want to retrieve the settings that are applied; you can use following code -

protected void btnGetServiceSettings_Click(object sender, EventArgs e)
        {
            StorageCredentialsAccountAndKey storageCredentials = new StorageCredentialsAccountAndKey("YourStorageAccountName", "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);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

//retrieve blob settings
StorageAnalyticsSettings blobSettings = StorageAnalyticsSettingsExtension.GetServiceSettings(blobClient);
//retrieve table settings
StorageAnalyticsSettings tableSettings = StorageAnalyticsSettingsExtension.GetServiceSettings(tableClient);
//retrieve queue settings
StorageAnalyticsSettings queueSettings = StorageAnalyticsSettingsExtension.GetServiceSettings(queueClient, storageAccount.QueueEndpoint);
//alternatively you can write these settings to file or display in your web page or view while debugging in visual studio.
        }

Once you are done with above settings and verify that they are assigned to blob, queue and table with above get settings code, open up your storage account in any Cloud Storage Studio. I personally use Cerebrata as it is free for first 30 days. J
If you upload any file to blob storage or delete, or add an entity in table storage and delete and similarly if you perform any operation on Queue then these transactions will be recorded in Logging as per the current date and time.
You will see that, $logs container will have logs created under it as shown below -

To retrieve and show names of these log file I added a simple Listbox and populating all the logs in it on a button click. The code is as follows –
protected void btnListLoggingBlobs_Click(object sender, EventArgs e)
        {
            StorageCredentialsAccountAndKey storageCredentials = new StorageCredentialsAccountAndKey("YourStorageAccount", "YourStorageAccountKey");
            CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("$logs");

            BlobRequestOptions blobRequest = new BlobRequestOptions();
            blobRequest.BlobListingDetails = BlobListingDetails.All;
            blobRequest.UseFlatBlobListing = true;

            ResultSegment<IListBlobItem> result = container.ListBlobsSegmented(blobRequest);          
           
            foreach (var blobItem in result.Results)
            {               
                lstBlobs.Items.Add(blobItem.Uri.AbsoluteUri);// lstBlobs is id of listbox I added
            }
        }
These logs of storage analytics then can be used for various purposes.
Hope this helps.
Cheers…

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

Happy Analyzing!!

Azure Storage Analytics – Metrics classes

This blog is follow up post pertaining to blog series related to Azure Storage Analytics.  To go back to the main article and understand the background of this post click- Azure Storage Analytics for Azure Storage –Blob, Queue and Tables - Metrics example
Following is the code of classes required for analytics metrics information for Azure storage – Blob, Queue and Table.
Class MetricsHelper.cs -
public class MetricsHelper
    {
        /// <summary>
        /// Given a service, start time, end time search for, this method retrieves the metrics rows for requests and exports it to CSV file
        /// </summary>
        /// <param name="tableClient"></param> /// <param name="serviceName">The name of the service interested in</param>
        /// <param name="startTimeForSearch">Start time for reporting</param>
        /// <param name="endTimeForSearch">End time for reporting</param>
        /// <param name="fileName">The file to write the report to</param>
        public static void DumpTransactionsMetrics(CloudTableClient tableClient, string serviceName, DateTime startTimeForSearch, DateTime endTimeForSearch, string fileName)
        {
            string startingPK = startTimeForSearch.ToString("yyyyMMddTHH00");
            string endingPK = endTimeForSearch.ToString("yyyyMMddTHH00");

            // table names are case insensitive
            string tableName = string.Format("$MetricsTransactions{0}", serviceName);

Azure - Need of Storage Analytics and what is Storage Analytics

What is software optimization -?
Program optimization or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources. In general, a computer program may be optimized so that it executes more rapidly, or is capable of operating with less memory storage or other resources, or draw less power.
To perform software optimization we need to identify problem areas in software which are not working efficiently. To identify problem areas, we need to analyze the program or software and cover various aspects like usage of memory by particular instruction, time duration taken by function execution or code segment. This will allow you to determine those improvements that are required in your application.
The process that measures for example, usage of memory, usage of particular instruction, or durations of functional calls is called as profiling or program profiling or software profiling. The tool used for profiling is called as Profiler.
The profiler available for .net is CLR profiler, Perfmon.exe tool. Similarly there are different profilers available for java, mainframe, SQL Server.
Similarly, to perform profiling on Windows Azure Storage, we use Storage Analytics. So in other words, Windows Azure Storage Analytics offers profiler like capabilities for Windows Azure Storage.

Tuesday, December 20, 2011

What is VPN – Virtual Private Network

Networking always amaze me. Sometimes I feel like I should have been studied networking more. Anyways…back to the point. I will try to describe what actually "Virtual Private Network" is. Let’s take a real life example.
As companies business expands, company opens up more offices across the various countries and around world. Generally people working from remote locations are sales people. Sales people always need to travel across countries and need to work while they are travelling or from remote locations around the globe. In such case, people working remotely need a fast, secure and reliable way to share information across computer networks of the company.
The technology which helps you to achieve this is Virtual Private Network. VPN is network which uses either private network or public network means internet to share information from remote locations. The information passed over VPN is always encrypted hence anyone can’t read this encrypted data. While using VPN over internet we use connections routed through internet only; still it gives us feel like we are working in private network by enabling us to access the desired network resources. Hence the connection established in VPN is not establishing any actual physical private network. It works over public network only, however it gives feel of working in private network by securing the information and provides seamless connectivity to desired network resources, therefore VPN is referred as Virtual.

To understand VPN let’s consider an example of real life and following diagram –