December 15, 2014 Leave a comment
The Azure API Management portal provides a useful dashboard showing number of calls to an API over time, bandwidth used, number of errors and average response time.
Viewing this information in the online dashboard is useful, but integrating it into existing dashboards and/or monitoring tools is even more powerful so – what it takes to get this data programmatically?
Not much, as it turns out!
APIM has a good set of management APIs to itself, including reporting on metrics. The first step is to enable the API Management REST API, which can be done within the security tab of the APIM admin portal –
Then, for my exercise, I manually created an Access Token at the bottom of the page, which I’ll use later when issuing requests to the portal. This can be done programmatically
Now that I’m able to call the management API, it is time to experiment with some requests.
The security tab showed the base Url for the management api which takes the form of
Every request to this url must include an api- version with the current value being – 2014-02-14-preview
The request also needs to include an Authorization HTTP header with the value being the access token generated either in the portal or programmatically.
Using this entity I can get usage metric by time period, geographical region, user, product, api or operation. I chose to get metrics by time so I appended to my url the entity path – /reports/byTime, the requests at this stage looks like this –
the byTime report takes 2 additional parameters – Interval, which defines the aggregation period and $filer which can be used to restrict the data returned., I’ve decided to aggregate data in 40 day increments and requested data from August 2014, the full request looks like this –
https://apiyd.management.azure-api.net/reports/byTime?api-version=2014-02-14-preview&interval=P40D&$filter=timestamp ge datetime’2014-08-01T00:00:00′
and the response looks like this –
You can see that I get two groups (there are more than 40 days between today and last August, with the relevant metrics in them.
I could also change the accept HTTP header to text/csv to receive the data in CSV format –
timestamp,interval,callCountSuccess,callCountBlocked,callCountFailed,callCountOther,callCountTotal,bandwidth,cacheHitCount,cacheMissCount,apiTimeAvg,apiTimeMin,apiTimeMax,serviceTimeAvg,serviceTimeMin,serviceTimeMax 09/10/2014 00:00:00,P40D,128,9,2,13,152,153677,18,3,422.1049578125,0.376,5620.048,399.780409375,0,5598.4203 11/29/2014 00:00:00,P40D,7,0,6,0,13,10648,0,0,171.982185714286,32.0005,612.5971,162.947842857143,19.8287,579.0369