使用Azure Application Insights创建关于调用第三方API的警报

时间:2017-07-10 15:13:03

标签: azure alert monitoring azure-application-insights

我已经在我创建的Azure WebApp上启用了应用程序洞察。我的WebApp正在调用以配额运行的第三方API。我每月只允许100k电话。

我需要跟踪这些API调用,以便在调用次数达到50%时创建警报,然后再调用75%的警报。

每次调用时我都在使用TrackEvent,AppInsights仪表板中的事件会增加。但是,当进行一定数量的呼叫时,我似乎无法创建警报。我无法从“事件”下拉列表中看到它。

另外,我需要的另一个要求是当呼叫次数超过每分钟10次时创建一个警报。

TrackEvent是否是用于满足这些要求的正确方法?

我做了这样的事......

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);

但是当我查看设置警报并设置了50000的阈值(用于测试,我只是放了5)时,我从未达到过,因为事件计数始终为1.我是否正确地接近这个?

1 个答案:

答案 0 :(得分:2)

您尝试定义的提醒始终会查看您在自定义事件中提供的值 - 而不是您要解雇的事件数量。
您可以创建自动流程来查询事件,并在查询结果超过某个阈值时向您发送电子邮件。 适用于Flow和Microsoft Logic Apps的Application Insights Connector就是为此而创建的,可以在任何文档类型(事件,指标甚至跟踪)的任何查询结果上定义。 有关如何创建自己的流的分步文档是here

至于您的查询 - 您需要一个简单的分析查询,如下所示:

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