Azure Service Bus Entity Metrics API

The other day I was presented with a project for a client wanting to add some monitoring and alerting to some of their Azure resources. Azure has some metrics and alerting functionality on most of their resources such as CPU, DTU, Memory, etc. Click here to see information related to creating these alerts. One challenge I was faced with was monitoring metrics around service bus queues.  Unfortunately, Microsoft has not provided the same monitoring and alerting functionality for service bus queues through the Azure portal.  After some research, I uncovered a REST API for retrieving metrics for service bus queues.  Problem solved, right? Not!  I spent a couple of days trying to figure out this loosely documented API.  After many searches through the web, it appears everyone is having the same problem.  The API is there but apparently no one can figure out how to use it.  Someone uncovered yet another API exposed which is completely different and I was able to get it to work.  In this link someone mentions they figured out they could use the following:

https://management.core.windows.net/%5Bsubscriberid%5D/services/monitoring/metricvalues/query?resourceId=/servicebus/namespaces/%5Bnamespace%5D/Queues/%5Bqueue%5D&names=size,incoming,outgoing,length,requests.total,requests.successful,requests.failed,requests.failed.internalservererror,requests.failed.serverbusy,requests.failed.other&timeGrain=PT5M&startTime=2015-11-16T08:59:27.7243529Z&endTime=2015-11-16T17:19:27.7243529Z

While this did work, I was not satisfied not being able to use the API that is documented above.  After more playing around, the magic sauce was finally discovered around how this API works. The following implementation worked successfully:

https://management.core.windows.net/%5Bsubscriberid%5D/services/servicebus/namespaces/%5Bnamespace%5D/queues/%5Bqueue%5D/metrics/incoming/rollups/PT1H/values?$filter=Timestamp%20gt%20datetime'2015-11-16T12:00:00.0000000Z'

There are a few very specific notes when using this API.

  • As mentioned in the Microsoft article, you will have to create a certificate and upload to the Azure management portal and then use this certificate to call the API.
  • You must send this header with the request:   x-ms-version: 2013-10-01
  • The API returns XML by default.  If you want to return JSON instead, send this header with the request:   Accept: application/json
  • If you omit the /values?$filter=Timestamp%20gt%20datetime’2015-11-16T12:00:00.0000000Z’ from your request, you will simply get metrics back but all of the values will return null.
  • The format of the filter is very specific. The “Timestamp” property is case sensitive.  If you pass it lower case you will receive a HTTP 500 error.
  • The Rollup options PT5M, PT1H, P1D, P7D must be all upper case

I created a custom WebClient that encapsulates the details of the headers and certificates.

class AzureManagementClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest)base.GetWebRequest(address);

        X509Store store = new X509Store("My", StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);
        X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindBySubjectName, "[certificate name]", false);
        var cert = certificates[0];

        request.ClientCertificates.Add(cert);
        request.Headers.Add("x-ms-version: 2013-10-01");
        request.Accept = "application/json";
        
        return request;
    }
}

blog_spacer

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s