在 Azure 虚拟机和 Azure 虚拟机规模集上部署 Azure Monitor Application Insights 代理Deploy the Azure Monitor Application Insights Agent on Azure virtual machines and Azure virtual machine scale sets

现在比以往更容易在基于 .NET 或 Java 的 Web 应用程序上启用监视功能,这些应用程序运行在 Azure 虚拟机Azure 虚拟机规模集上。Enabling monitoring for your .NET or Java based web applications running on Azure virtual machines and Azure virtual machine scale sets is now easier than ever. 获取使用 Application Insights 的所有权益,不需修改代码。Get all the benefits of using Application Insights without modifying your code.

本文逐步讲解如何通过 Application Insights 代理启用 Application Insights 监视,并提供有关如何自动完成大规模部署过程的初步指导。This article walks you through enabling Application Insights monitoring using the Application Insights Agent and provides preliminary guidance for automating the process for large-scale deployments.

重要

在 Azure VM 和 VMSS 上运行的基于 Java 的应用程序是通过 Application Insights Java 3.0 代理 监视的,该代理已正式发布。Java based applications running on Azure VMs and VMSS are monitored with Application Insights Java 3.0 agent, which is generally available.

重要

在 Azure VM 和 VMSS 上运行的 ASP.NET 应用程序的 Azure Application Insights 代理当前为公共预览版。Azure Application Insights Agent for ASP.NET applications running on Azure VMs and VMSS is currently in public preview. 若要监视本地运行的 ASP.Net 应用程序,请使用用于本地服务器的 Azure Application Insights 代理。该代理已正式发布,是完全受支持的。For monitoring your ASP.Net applications running on-premises, use the Azure Application Insights Agent for on-premises servers, which is generally available and fully supported. 用于 Azure VM 和 VMSS 的预览版在提供时没有附带服务级别协议,不建议将其用于生产工作负荷。The preview version for Azure VMs and VMSS is provided without a service-level agreement, and we don't recommend it for production workloads. 有些功能可能不受支持,有些功能可能受到限制。Some features might not be supported, and some might have constrained capabilities. 有关详细信息,请参阅适用于 Azure 预览版的补充使用条款For more information, see Supplemental Terms of Use for Azure Previews.

启用 Application InsightsEnable Application Insights

可通过两种方法为 Azure 虚拟机和 Azure 虚拟机规模集托管的应用程序启用应用程序监视:There are two ways to enable application monitoring for Azure virtual machines and Azure virtual machine scale sets hosted applications:

通过 Application Insights 代理自动检测Auto-instrumentation via Application Insights Agent

  • 这是启用监视的最简单方法,无需完成任何高级配置。This method is the easiest to enable, and no advanced configuration is required. 这种监视通常称为“运行时”监视。It is often referred to as "runtime" monitoring.

  • 对于 Azure 虚拟机和 Azure 虚拟机规模集,建议至少启用此级别的监视。For Azure virtual machines and Azure virtual machine scale sets we recommend at a minimum enabling this level of monitoring. 然后,可以根据具体情况评估是否需要手动检测。After that, based on your specific scenario, you can evaluate whether manual instrumentation is needed.

备注

自动检测目前仅可用于 .NET IIS 托管的应用程序和 Java。Auto-instrumentation is currently only available for .NET IIS-hosted applications and Java. 请使用 SDK 检测托管在 Azure 虚拟机和虚拟机规模集上的 ASP.NET Core、Node.js 和 Python 应用程序。Use an SDK to instrument ASP.NET Core, Node.js, and Python applications hosted on an Azure virtual machines and virtual machine scale sets.

.NET.NET

  • Application Insights 代理自动收集与 .NET SDK 相同的、现成的依赖项信号。The Application Insights Agent auto-collects the same dependency signals out-of-the-box as the .NET SDK. 若要了解详细信息,请参阅依赖项自动收集See Dependency auto-collection to learn more.

JavaJava

通过 SDK 执行的“基于代码”方法Code-based via SDK

.NET.NET

  • 对于 .NET 应用,此方法的可自定义程度更高,但需要添加 Application Insights SDK NuGet 包中的一个依赖项For .NET apps, this approach is much more customizable, but it requires adding a dependency on the Application Insights SDK NuGet packages. 使用此方法还需要自行管理对最新版本的包的更新。This method, also means you have to manage the updates to the latest version of the packages yourself.

  • 如果需要发出自定义 API 调用来跟踪基于代理的监视在默认情况下不会捕获的事件/依赖项,则需要使用此方法。If you need to make custom API calls to track events/dependencies not captured by default with agent-based monitoring, you would need to use this method. 有关详细信息,请查看 自定义事件和指标的 API 一文。Check out the API for custom events and metrics article to learn more.

    备注

    仅限 .NET 应用 - 如果同时检测到了基于代理的监视和基于手动 SDK 的检测,则只会采用手动检测设置,For .NET apps only - if both agent based monitoring and manual SDK based instrumentation is detected only the manual instrumentation settings will be honored. 这是为了防止发送重复数据。This is to prevent duplicate data from being sent. 有关详细信息,请查看下面的故障排除部分To learn more about this check out the troubleshooting section below.

.NET Core.NET Core

若要监视 .NET Core 应用程序,请使用 SDKTo monitor .NET Core applications, use the SDK

JavaJava

如果需要对 Java 应用程序进行其他自定义遥测,请查看有什么可用,添加自定义维度,或使用遥测处理器If you need additional custom telemetry for Java applications, see what is available, add custom dimensions, or use telemetry processors.

Node.jsNode.js

若要检测 Node.js 应用程序,请使用 SDKTo instrument your Node.js application, use the SDK.

PythonPython

若要监视 Python 应用,请使用 SDKTo monitor Python apps, use the SDK.

使用 PowerShell 在 Azure 虚拟机上管理用于 .NET 应用程序的 Application Insights 代理Manage Application Insights Agent for .NET applications on Azure virtual machines using PowerShell

备注

在安装 Application Insights 代理之前,需要一个连接字符串。Before installing the Application Insights Agent, you'll need a connection string. 创建新的 Application Insights 资源,或从现有 Application Insights 资源复制连接字符串。Create a new Application Insights Resource or copy the connection string from an existing application insights resource.

备注

不熟悉 PowerShell?New to PowerShell? 请查看入门指南Check out the Get Started Guide.

安装或更新用作 Azure 虚拟机扩展的 Application Insights 代理Install or update the Application Insights Agent as an extension for Azure virtual machines

$publicCfgJsonString = '
{
  "redfieldConfiguration": {
    "instrumentationKeyMap": {
      "filters": [
        {
          "appFilter": ".*",
          "machineFilter": ".*",
          "virtualPathFilter": ".*",
          "instrumentationSettings" : {
            "connectionString": "InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
          }
        }
      ]
    }
  }
}
';
$privateCfgJsonString = '{}';

Set-AzVMExtension -ResourceGroupName "<myVmResourceGroup>" -VMName "<myVmName>" -Location "<myVmLocation>" -Name "ApplicationMonitoring" -Publisher "Microsoft.Azure.Diagnostics" -Type "ApplicationMonitoringWindows" -Version "2.8" -SettingString $publicCfgJsonString -ProtectedSettingString $privateCfgJsonString

备注

可以使用 PowerShell 循环跨多个虚拟机大规模安装或更新用作扩展的 Application Insights 代理。You may install or update the Application Insights Agent as an extension across multiple Virtual Machines at-scale using a PowerShell loop.

从 Azure 虚拟机卸载 Application Insights 代理扩展Uninstall Application Insights Agent extension from Azure virtual machine

Remove-AzVMExtension -ResourceGroupName "<myVmResourceGroup>" -VMName "<myVmName>" -Name "ApplicationMonitoring"

查询 Azure 虚拟机的 Application Insights 代理扩展状态Query Application Insights Agent extension status for Azure virtual machine

Get-AzVMExtension -ResourceGroupName "<myVmResourceGroup>" -VMName "<myVmName>" -Name ApplicationMonitoring -Status

获取 Azure 虚拟机的已安装扩展的列表Get list of installed extensions for Azure virtual machine

Get-AzResource -ResourceId "/subscriptions/<mySubscriptionId>/resourceGroups/<myVmResourceGroup>/providers/Microsoft.Compute/virtualMachines/<myVmName>/extensions"

# Name              : ApplicationMonitoring
# ResourceGroupName : <myVmResourceGroup>
# ResourceType      : Microsoft.Compute/virtualMachines/extensions
# Location          : China East 2
# ResourceId        : /subscriptions/<mySubscriptionId>/resourceGroups/<myVmResourceGroup>/providers/Microsoft.Compute/virtualMachines/<myVmName>/extensions/ApplicationMonitoring

你还可以在门户中的 Azure 虚拟机边栏选项卡中查看已安装的扩展。You may also view installed extensions in the Azure virtual machine blade in the Portal.

备注

验证安装的方法是:单击与你在部署 Application Insights 代理扩展时使用的连接字符串相关联的 Application Insights 资源中的“实时指标流”。Verify installation by clicking on Live Metrics Stream within the Application Insights Resource associated with the connection string you used to deploy the Application Insights Agent Extension. 如果从多个虚拟机发送数据,请在“服务器名称”下选择目标 Azure 虚拟机。If you are sending data from multiple Virtual Machines, select the target Azure virtual machines under Server Name. 可能需要等待长达一分钟的时间数据才会开始流动。It may take up to a minute for data to begin flowing.

使用 PowerShell 在 Azure 虚拟机规模集上管理用于 .NET 应用程序的 Application Insights 代理Manage Application Insights Agent for .NET applications on Azure virtual machine scale sets using PowerShell

安装或更新用作 Azure 虚拟机规模集扩展的 Application Insights 代理Install or update the Application Insights Agent as an extension for Azure virtual machine scale set

$publicCfgHashtable =
@{
  "redfieldConfiguration"= @{
    "instrumentationKeyMap"= @{
      "filters"= @(
        @{
          "appFilter"= ".*";
          "machineFilter"= ".*";
          "virtualPathFilter": ".*",
          "instrumentationSettings" : {
            "connectionString": "InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # Application Insights connection string, create new Application Insights resource if you don't have one. https://portal.azure.cn/#blade/HubsExtension/BrowseResourceBlade/resourceType/microsoft.insights%2Fcomponents
          }
        }
      )
    }
  }
};
$privateCfgHashtable = @{};

$vmss = Get-AzVmss -ResourceGroupName "<myResourceGroup>" -VMScaleSetName "<myVmssName>"

Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "ApplicationMonitoring" -Publisher "Microsoft.Azure.Diagnostics" -Type "ApplicationMonitoringWindows" -TypeHandlerVersion "2.8" -Setting $publicCfgHashtable -ProtectedSetting $privateCfgHashtable

Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -Name $vmss.Name -VirtualMachineScaleSet $vmss

# Note: depending on your update policy, you might need to run Update-AzVmssInstance for each instance

从 Azure 虚拟机规模集卸载应用程序监视扩展Uninstall application monitoring extension from Azure virtual machine scale sets

$vmss = Get-AzVmss -ResourceGroupName "<myResourceGroup>" -VMScaleSetName "<myVmssName>"

Remove-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "ApplicationMonitoring"

Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -Name $vmss.Name -VirtualMachineScaleSet $vmss

# Note: depending on your update policy, you might need to run Update-AzVmssInstance for each instance

查询 Azure 虚拟机规模集的应用程序监视扩展状态Query application monitoring extension status for Azure virtual machine scale sets

# Not supported by extensions framework

获取 Azure 虚拟机规模集的已安装扩展的列表Get list of installed extensions for Azure virtual machine scale sets

Get-AzResource -ResourceId /subscriptions/<mySubscriptionId>/resourceGroups/<myResourceGroup>/providers/Microsoft.Compute/virtualMachineScaleSets/<myVmssName>/extensions

# Name              : ApplicationMonitoringWindows
# ResourceGroupName : <myResourceGroup>
# ResourceType      : Microsoft.Compute/virtualMachineScaleSets/extensions
# Location          :
# ResourceId        : /subscriptions/<mySubscriptionId>/resourceGroups/<myResourceGroup>/providers/Microsoft.Compute/virtualMachineScaleSets/<myVmssName>/extensions/ApplicationMonitoringWindows

疑难解答Troubleshooting

请查找在 Azure 虚拟机和虚拟机规模集上运行的 .NET 应用程序的 Application Insights 监视代理扩展的故障排除提示。Find troubleshooting tips for Application Insights Monitoring Agent Extension for .NET applications running on Azure virtual machines and virtual machine scale sets.

备注

仅支持通过基于 SDK 的手动检测在 Azure 虚拟机和 Azure 虚拟机规模集中使用 .NET Core、Node.js 和 Python 应用程序,因此,以下步骤不适用于这些方案。.NET Core, Node.js, and Python applications are only supported on Azure virtual machines and Azure virtual machine scale sets via manual SDK based instrumentation and therefore the steps below do not apply to these scenarios.

扩展执行输出将记录到在以下目录中发现的文件:Extension execution output is logged to files found in the following directories:

C:\WindowsAzure\Logs\Plugins\Microsoft.Azure.Diagnostics.ApplicationMonitoringWindows\<version>\

后续步骤Next steps