使用 PowerShell 在 Application Insights 中设置警报Use PowerShell to set alerts in Application Insights


本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

可以在 Application Insights 中自动配置警报You can automate the configuration of alerts in Application Insights.

此外,可以将 webhook 设置为自动执行对警报的响应In addition, you can set webhooks to automate your response to an alert.


如果要同时创建资源和警报,请考虑使用 Azure 资源管理器模板If you want to create resources and alerts at the same time, consider using an Azure Resource Manager template.

一次性设置One-time setup

如果之前尚未将 PowerShell 与 Azure 订阅结合使用:If you haven't used PowerShell with your Azure subscription before:

在要运行脚本的计算机上安装 Azure Powershell 模块。Install the Azure Powershell module on the machine where you want to run the scripts.

连接到 AzureConnect to Azure

启动 Azure PowerShell 并连接到订阅Start Azure PowerShell and connect to your subscription:

    Add-AzAccount -Environment AzureChinaCloud

获取警报Get alerts

Get-AzAlertRule -ResourceGroup "Fabrikam" [-Name "My rule"] [-DetailedOutput]

添加警报Add alert

Add-AzMetricAlertRule  -Name "{ALERT NAME}" -Description "{TEXT}" `
 -ResourceGroup "{GROUP NAME}" `
 -ResourceId "/subscriptions/{SUBSCRIPTION ID}/resourcegroups/{GROUP NAME}/providers/microsoft.insights/components/{APP RESOURCE NAME}" `
 -MetricName "{METRIC NAME}" `
 -Operator GreaterThan  `
 -Threshold {NUMBER}   `
 -WindowSize {HH:MM:SS}  `
 [-SendEmailToServiceOwners] `
 [-CustomEmails "EMAIL1@X.COM","EMAIL2@Y.COM" ] `
 -Location "China East 2" // must be China East 2 at present
 -RuleType Metric

示例 1Example 1

如果服务器对 HTTP 请求的响应(平均值超过 5 分钟)慢于 1 秒,请向我发送电子邮件。Email me if the server's response to HTTP requests, averaged over 5 minutes, is slower than 1 second. 我的 Application Insights 资源名称为 IceCreamWebApp,并且位于资源组 Fabrikam 中。My Application Insights resource is called IceCreamWebApp, and it is in resource group Fabrikam. 我是 Azure 订阅的所有者。I am the owner of the Azure subscription.

GUID 是订阅 ID(不是应用程序的检测密钥)。The GUID is the subscription ID (not the instrumentation key of the application).

Add-AzMetricAlertRule -Name "slow responses" `
 -Description "email me if the server responds slowly" `
 -ResourceGroup "Fabrikam" `
 -ResourceId "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Fabrikam/providers/microsoft.insights/components/IceCreamWebApp" `
 -MetricName "request.duration" `
 -Operator GreaterThan `
 -Threshold 1 `
 -WindowSize 00:05:00 `
 -SendEmailToServiceOwners `
 -Location "China East 2" -RuleType Metric

示例 2Example 2

我在应用程序中使用 TrackMetric() 报告名为“salesPerHour”的指标。I have an application in which I use TrackMetric() to report a metric named "salesPerHour." 如果“salesPerHour”低于 100(平均值超出 24 小时),会向我的同事发送电子邮件。Send an email to my colleagues if "salesPerHour" drops below 100, averaged over 24 hours.

Add-AzMetricAlertRule -Name "poor sales" `
 -Description "slow sales alert" `
 -ResourceGroup "Fabrikam" `
 -ResourceId "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Fabrikam/providers/microsoft.insights/components/IceCreamWebApp" `
 -MetricName "salesPerHour" `
 -Operator LessThan `
 -Threshold 100 `
 -WindowSize 24:00:00 `
 -CustomEmails "satish@fabrikam.com","lei@fabrikam.com" `
 -Location "China East 2" -RuleType Metric

同一规则可用于通过使用另一跟踪调用(如 TrackEvent 或 trackPageView)的测量参数报告的指标。The same rule can be used for the metric reported by using the measurement parameter of another tracking call such as TrackEvent or trackPageView.

指标名称Metric names

指标名称Metric name 屏幕名称Screen name 说明Description
basicExceptionBrowser.count 浏览器异常Browser exceptions 浏览器中所引发未捕获异常的计数。Count of uncaught exceptions thrown in the browser.
basicExceptionServer.count 服务器异常Server exceptions 应用所引发未经处理的异常的计数Count of unhandled exceptions thrown by the app
clientPerformance.clientProcess.value 客户端处理时间Client processing time 从接收文档的最后一个字节到 DOM 加载完之间的时间。Time between receiving the last byte of a document until the DOM is loaded. 可能仍在处理异步请求。Async requests may still be processing.
clientPerformance.networkConnection.value 页面加载网络连接时间Page load network connect time 浏览器连接到网络所需的时间。Time the browser takes to connect to the network. 如果已缓存,可以为 0。Can be 0 if cached.
clientPerformance.receiveRequest.value 接收响应时间Receiving response time 从浏览器发送请求到开始接收响应之间的时间。Time between browser sending request to starting to receive response.
clientPerformance.sendRequest.value 发送请求时间Send request time 浏览器发送请求所需的时间。Time taken by browser to send request.
clientPerformance.total.value 浏览器页面加载时间Browser page load time 从用户请求一直到 DOM、样式表、脚本和映像加载完之间的时间。Time from user request until DOM, stylesheets, scripts and images are loaded.
performanceCounter.available_bytes.value 可用内存Available memory 可立即供进程或系统使用的物理内存。Physical memory immediately available for a process or for system use.
performanceCounter.io_data_bytes_per_sec.value 进程 IO 率Process IO Rate 每秒读取和写入文件、网络和设备的总字节数。Total bytes per second read and written to files, network and devices.
performanceCounter.number_of_exceps_thrown_per_sec.value 异常率exception rate 每秒引发的异常。Exceptions thrown per second.
performanceCounter.percentage_processor_time.value 进程 CPUProcess CPU 由处理器使用所有进程线程针对应用程序进程执行指令所花费的时间百分比。The percentage of elapsed time of all process threads used by the processor to execution instructions for the applications process.
performanceCounter.percentage_processor_total.value 处理器时间Processor time 处理器在非空闲线程上所花费的时间百分比。The percentage of time that the processor spends in non-Idle threads.
performanceCounter.process_private_bytes.value 进程专用字节Process private bytes 以独占方式分配给受监视应用程序进程的内存。Memory exclusively assigned to the monitored application's processes.
performanceCounter.request_execution_time.value ASP.NET 请求执行时间ASP.NET request execution time 最近的请求执行时间。Execution time of the most recent request.
performanceCounter.requests_in_application_queue.value 执行队列中的 ASP.NET 请求ASP.NET requests in execution queue 应用程序请求队列的长度。Length of the application request queue.
performanceCounter.requests_per_sec.value ASP.NET 请求速率ASP.NET request rate 每秒从 ASP.NET 发出的应用程序所有请求的速率。Rate of all requests to the application per second from ASP.NET.
remoteDependencyFailed.durationMetric.count 依赖项失败次数Dependency failures 服务器应用程序对外部资源所进行的失效调用的计数。Count of failed calls made by the server application to external resources.
request.duration 服务器响应时间Server response time 从接收 HTTP 请求到完成响应发送之间的时间。Time between receiving an HTTP request and finishing sending the response.
request.rate 请求速率Request rate 每秒应用程序所有请求的速率。Rate of all requests to the application per second.
requestFailed.count 失败的请求Failed requests 响应代码中生成的 HTTP 请求计数 >= 400Count of HTTP requests that resulted in a response code >= 400
view.count 页面视图Page views 网页的客户端用户请求的计数。Count of client user requests for a web page. 综合流量已筛选掉。Synthetic traffic is filtered out.
{自定义指标名称}{your custom metric name} {指标名称}{Your metric name} TrackMetric 报告或者跟踪调用的测量参数中的指标值。Your metric value reported by TrackMetric or in the measurements parameter of a tracking call.

指标由不同的遥测模块发送:The metrics are sent by different telemetry modules:

指标组Metric group 收集器模块Collector module
浏览器 JavaScriptBrowser JavaScript
performanceCounterperformanceCounter 性能Performance
remoteDependencyFailedremoteDependencyFailed 依赖项Dependency
服务器请求Server request


自动执行对警报的响应You can automate your response to an alert. 引发警报时,Azure 将调用所选的 Web 地址。Azure will call a web address of your choice when an alert is raised.

另请参阅See also