Application Insights can offer you lots of built-in telemetries such as Page Views and Exceptions. But quite often we need to track some customize/business performances. Some examples that we are using now are:
SharePoint crawled items from content source X
Daily usage of a web application of all users from company ABC
Usage and performance of different version of API
To meet these challenges, Application Insights offers API for custom event and metrics.
In this article I will show how to monitor and analysis API performance by using custom events, including export it into external database for archiving and future analysis.
Report events
Our ASP.Net Web API application is already instrumented with Application Insights. The goal is to also report the performance and usage of API in below format.
By following the documentation, we have below code in the server side for sending the performance data of each API request:
SELECT C.internal.data.id as ID , C.context.data.eventTime as [TimeStamp] ,GetRecordPropertyValue(GetArrayElement(C.context.custom.dimensions, 0),'ElapsedMilliseconds') as ElapsedMilliseconds ,GetRecordPropertyValue(GetArrayElement(C.context.custom.dimensions, 1),'AbsoluteUri') as AbsoluteUri ,GetRecordPropertyValue(GetArrayElement(C.context.custom.dimensions, 2),'UserId') as UserId ,GetRecordPropertyValue(GetArrayElement(C.context.custom.dimensions, 3),'Action') as Action ,GetRecordPropertyValue(GetArrayElement(C.context.custom.dimensions, 4),'AccountId') as AccountId ,GetRecordPropertyValue(GetArrayElement(C.context.custom.dimensions, 5),'ContractName') as ContractName ,GetRecordPropertyValue(GetArrayElement(C.context.custom.dimensions, 6),'AccountName') as AccountName INTO [output-ai-apicustomevents] FROM [input-ai-apicustomevents] C