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:,incoming,outgoing,length,,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:$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);
        X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindBySubjectName, "[certificate name]", false);
        var cert = certificates[0];

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