1
votes

I've enabled application insights on an Azure WebApp that I created. My WebApp is calling a third party API which runs on a quota. I am only allowed 100k calls per month.

I need to track those API calls so that I can create an alert when the number of calls has reached 50%, then another alert 75%.

I am using TrackEvent every time the call is made and the event in the AppInsights dashboard does increment. But I can't seem to create an alert when a certain number of calls is made. I can't see it from the list of 'events' dropdown.

Also in addition, one other requirement that I need is to create an alerts when the number of calls to the goes over 10 per minutes.

Is TrackEvent the right method to use for these requirements?

I did something like this ...

var telemetryEventClient = new Microsoft.ApplicationInsights.TelemetryClient(new Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration() { InstrumentationKey = "Instrumentation Key" });
telemetryEventClient.Context.Operation.Name = "MyAPIProvider";

var properties = new Dictionary<string, string>
{
    { "Source", "WebAppToAPI" }
};

var metrics = new Dictionary<string, double>
{
    { "CallingAPIMetric", 1 }
};

telemetryEventClient.TrackEvent("CallingAPI", properties, metrics);

but when I looked at setting up the alert and placed a threshold of 50000 (for testing, I just put 5), I never reach that as the event count is always 1. Am I approaching this the right way?

1

1 Answers

2
votes

The alert you're trying to define always looks at the value you supply in your custom event - not the amount of events you're firing.
You can create an automated flow to query your events and send you an email whenever the query result passes some threshold. The Application Insights Connector which works both for Flow and Microsoft Logic Apps was created just for that, and can be defined on any query result from any document type (event, metric or even traces). Step-by-step documentation on how to create your own flow are here.

As for your query - you need a simple analytics query like this:

customEvents
| where timestamp > ago(1h) // or any time range you need
| where name == "CallingAPI"
| count