Instrumenting your Azure app with Performance Counters:

In this article I would like to describe monitoring the health of your application in Windows Azure via Performance Counters. The topic came out as a result of working on a health monitoring aspect of an Azure Application where I had to implement means for the system to give feedback to the user. I believe that Azure Diagnostics should be part of every application and value of your app instrumentation cannot be underestimated. In this article I will focus on performance counters and will provide a working code example.

Some Theory
The health of your Azure application is monitored via Azure Diagnostics, an API in Microsoft.WindowsAzure.Diagnostics assembly. Windows Azure Diagnostics provides a variety of tools to check the health of your Azure app. Such tools include:

  • Logs
  • Windows Events
  • Performance Counters
  • Crash Dumps

Using Azure Diagnostics API you can configure your Web Role or Worker Role to collect the health information. The health information is stored on a local file system. From time to time the health information is transferred into a TableStorage by a Monitoring Agent as shown in the Fig.1.

Azure Diagnostics in Action - Image

Enough said let us see some code! First we need to configure the diagnostics API to use the proper storage account, file quota, and frequency of health information transfer:

private void ConfigureDiagnostics()

    var cloudStorageAccount = CloudStorageAccount.Parse(

    var roleInstanceDiagnosticManager =

    var cfg =
        roleInstanceDiagnosticManager.GetCurrentConfiguration() ??
        new DiagnosticMonitorConfiguration();

    new PerformanceCounterConfiguration()
        CounterSpecifier = @"\Processor(_Total)\% Processor Time",
        SampleRate = TimeSpan.FromSeconds(5)
    cfg.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromSeconds(1d);
    cfg.PerformanceCounters.BufferQuotaInMB = 512;


With that my worker role is now collecting the CPU utilization every 5 seconds and writing it every minute to the WADPerformanceCountersTable in the Developer Storage.

If we just run the WebRole we will be able to see the perf counters recorded in the WADPerformanceCountersTable:


Next we need define data structures to work with the every WADPerformanceCountersTable:

public class PerformanceCounterEntry :
    public PerformanceCounterEntry()

    public string Timestamp { get; set; }
    public long EventTickCount { get; set; }
    public string DeploymentId { get; set; }
    public string Role { get; set; }
    public string RoleInstance { get; set; }
    public string CounterName { get; set; }
    public string CounterValue { get; set; }

public class PerformanceCounterDataContext : TableServiceContext
    public PerformanceCounterDataContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
    { }

    public IQueryable PerformanceCounterEntry
            return this.CreateQuery("WADPerformanceCountersTable");

public class PerformanceCounterEntryDataSource
    private static CloudStorageAccount storageAccount;
    private PerformanceCounterDataContext context;

    static PerformanceCounterEntryDataSource()
        storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");


    public PerformanceCounterEntryDataSource()
        this.context = new PerformanceCounterDataContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
        this.context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));

    public IEnumerable Select()
        var tempResults = (from pc in this.context.PerformanceCounterEntry
                            where pc.EventTickCount > DateTime.Now.AddMinutes(-10).Ticks
                        select pc).ToList();

	//show last 20 records
        var results = tempResults.OrderByDescending(pc => pc.EventTickCount).Take(20);
        return results;

Now all we need to do is to show TOP 20 counter records in the DataGrid:

<asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
 DataSourceID="ObjectDataSource1" CellPadding="10" AutoGenerateColumns="false">
 <asp:BoundColumn DataField="TimeStamp" HeaderStyle-Font-Bold="true" HeaderText="TimeStamp" ReadOnly="true"/>
 <asp:BoundColumn DataField="CounterName" HeaderStyle-Font-Bold="true" HeaderText="Counter" ReadOnly="true"/>
 <asp:BoundColumn DataField="CounterValue" HeaderStyle-Font-Bold="true" HeaderText="Value" ReadOnly="true"/>

Now we are ready to run the application. This is the result I got:

Performance Report


One can never undervalue good instrumentation of your apps with diagnostics. In this post I have discussed how to integrate performance counters into your Azure application. The article demonstrates the integration of a CPU utilization performance counter via a code sample.


About Alex Koval
Alex Koval is a CEO and President of Codemasters International, a premier provider of Software Development services. Being a technical leader for many years, Alex pursued his dream of assembling a team of talented software professionals he met over the years. He started Codemasters International LLC ( in 2008 applying his best knowledge of technical leadership, team building as well as industry best practices. The company offers quality software consulting services for Small and Medium businesses at affordable rates. Today Codemasters International is a global company of 30+ employees with offices in US, Russia, and Ukraine. Alex Koval is happily married to his wife Anna. In his free time he enjoys family, friends, sports, and travel. Alex gives back to the community by organizing the meetings for the TulaDev.NET user group ( as well as being a speaker for Florida User groups and Code Camps.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: