配置 Azure Monitor OpenTelemetry

本指南介绍如何使用 Azure Monitor OpenTelemetry 发行版在 Azure Monitor Application Insights 中配置 OpenTelemetry (OTel)。 正确配置可确保跨 .NET、Java、Node.js和 Python 应用程序收集一致的遥测数据收集,从而实现更可靠的监视和诊断。

连接字符串

Application Insights 中的连接字符串定义了用于发送遥测数据的目标位置。

使用以下三种方法来配置连接字符串:

  • UseAzureMonitor() 添加到 program.cs 文件:

    var builder = WebApplication.CreateBuilder(args);
    
    // Add the OpenTelemetry telemetry service to the application.
    // This service will collect and send telemetry data to Azure Monitor.
    builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
        options.ConnectionString = "<Your Connection String>";
    });
    
    var app = builder.Build();
    
    app.Run();
    
  • 设置环境变量。

    APPLICATIONINSIGHTS_CONNECTION_STRING=<Your Connection String>
    
  • 将以下部分添加到 appsettings.json 配置文件。

    {
      "AzureMonitor": {
          "ConnectionString": "<Your Connection String>"
      }
    }
    

备注

如果在多个位置设置连接字符串,我们遵循以下优先顺序:

  1. 代码
  2. 环境变量
  3. 配置文件

设置云角色名称和云角色实例

对于支持的语言,Azure Monitor OpenTelemetry 发行版会自动检测资源上下文,并为组件的云角色名称和云角色实例属性提供默认值。 但是,可能需要将默认值替代为对团队有意义的值。 云角色名称值以节点下面的名称出现在应用程序映射上。

通过资源属性设置云角色名称和云角色实例。 云角色名称使用 service.namespaceservice.name 属性,但如果未设置 service.name,它将回滚到 service.namespace。 云角色实例使用 service.instance.id 属性值。 有关资源的标准属性的信息,请参阅 OpenTelemetry 语义约定

// Setting role name and role instance

// Create a dictionary of resource attributes.
var resourceAttributes = new Dictionary<string, object> {
    { "service.name", "my-service" },
    { "service.namespace", "my-namespace" },
    { "service.instance.id", "my-instance" }};

// Create a new ASP.NET Core web application builder.
var builder = WebApplication.CreateBuilder(args);

// Add the OpenTelemetry telemetry service to the application.
// This service will collect and send telemetry data to Azure Monitor.
builder.Services.AddOpenTelemetry()
    .UseAzureMonitor()
    // Configure the ResourceBuilder to add the custom resource attributes to all signals.
    // Custom resource attributes should be added AFTER AzureMonitor to override the default ResourceDetectors.
    .ConfigureResource(resourceBuilder => resourceBuilder.AddAttributes(_testResourceAttributes));

// Build the ASP.NET Core web application.
var app = builder.Build();

// Start the ASP.NET Core web application.
app.Run();

启用采样

可能需要启用采样以减少数据引入量,从而降低成本。 Azure Monitor 提供自定义固定速率采样器,该采样器使用采样率填充事件,Application Insights 会将其转换为 ItemCount固定速率采样器可确保准确的体验和事件计数。 采样器旨在跨服务保留跟踪,并可与较旧的 Application Insights 软件开发工具包 (SDK) 互操作。 有关详细信息,请参阅详细了解采样

备注

指标和日志不受采样影响。 如果在 Application Insights 中看到意外费用或高成本,本指南将有所帮助。 它涵盖了常见的原因,例如高遥测量、数据引入峰值和配置错误的采样。 如果您正在解决与成本峰值、遥测数据量、采样失效、数据上限、数据摄取量高或意外计费相关的问题,这将特别有用。 如需入门,请参阅 对 Application Insights 中数据引入量过高的问题进行故障排除

采样器需要 0 到 1 之间(含)的采样率。 0.1 意味着会发送大约 10% 的跟踪。

// Create a new ASP.NET Core web application builder.
var builder = WebApplication.CreateBuilder(args);

// Add the OpenTelemetry telemetry service to the application.
// This service will collect and send telemetry data to Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor(options =>
{
    // Set the sampling ratio to 10%. This means that 10% of all traces will be sampled and sent to Azure Monitor.
    options.SamplingRatio = 0.1F;
});

// Build the ASP.NET Core web application.
var app = builder.Build();

// Start the ASP.NET Core web application.
app.Run();

提示

如果使用固定比率/百分比采样,并且你不确定将采样率设置为何值,请先设置为 5%。 (0.05 采样率)根据故障和性能窗格中所示的操作的准确性调整比率。 通常情况下,采样率越高,准确度越高。 但是,任何采样都会影响准确度,因此我们建议对不受采样影响的 OpenTelemetry 指标发出警报。

实时指标

实时指标提供实时分析仪表板,用于深入了解应用程序活动和性能。

重要

有关 beta 版本、预览版或尚未正式发布的版本的 Azure 功能所适用的法律条款,请参阅 Azure 预览版的补充使用条款

此功能默认启用。

配置发行版时,用户可以禁用实时指标。

builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
	// Disable the Live Metrics feature.
    options.EnableLiveMetrics = false;
});

脱机存储和自动重试

当应用程序与 Application Insights 断开连接并重试发送长达 48 小时时,基于 Azure Monitor OpenTelemetry 的产品/服务会缓存遥测数据。 有关数据处理建议,请参阅 导出和删除专用数据。 由于两个原因,高负载应用程序偶尔会删除遥测:超过允许的时间或超过最大文件大小。 如有必要,产品将最近事件优先于旧事件。

发行版包中包括 AzureMonitorExporter,默认情况下,它使用以下位置之一进行脱机存储(按优先顺序列出):

  • Windows
    • %LOCALAPPDATA%\Microsoft\AzureMonitor
    • %TEMP%\Microsoft\AzureMonitor
  • 非 Windows
    • %TMPDIR%/Microsoft/AzureMonitor
    • /var/tmp/Microsoft/AzureMonitor
    • /tmp/Microsoft/AzureMonitor

若要替代默认目录,应设置 AzureMonitorOptions.StorageDirectory

// Create a new ASP.NET Core web application builder.
var builder = WebApplication.CreateBuilder(args);

// Add the OpenTelemetry telemetry service to the application.
// This service will collect and send telemetry data to Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor(options =>
{
    // Set the Azure Monitor storage directory to "C:\\SomeDirectory".
    // This is the directory where the OpenTelemetry SDK will store any telemetry data that cannot be sent to Azure Monitor immediately.
    options.StorageDirectory = "C:\\SomeDirectory";
});

// Build the ASP.NET Core web application.
var app = builder.Build();

// Start the ASP.NET Core web application.
app.Run();

若要禁用此功能,应设置 AzureMonitorOptions.DisableOfflineStorage = true

启用 OTLP 导出器

你可能想要启用 OpenTelemetry 协议 (OTLP) 导出器和 Azure Monitor 导出器,以将遥测数据发送到两个位置。

备注

为方便起见,只展示了 OTLP 导出器。 我们并未正式支持使用 OTLP 导出器或是其下游的任何组件或第三方体验。

  1. 在你的项目中安装 OpenTelemetry.Exporter.OpenTelemetryProtocol 包。

    dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
    
  2. 添加以下代码片段。 此示例假设你已经安装了一个 OpenTelemetry 收集器,并且其中正在运行一个 OTLP 接收器。 有关详细信息,请参阅 GitHub 上的示例

    // Create a new ASP.NET Core web application builder.
    var builder = WebApplication.CreateBuilder(args);
    
    // Add the OpenTelemetry telemetry service to the application.
    // This service will collect and send telemetry data to Azure Monitor.
    builder.Services.AddOpenTelemetry().UseAzureMonitor();
    
    // Add the OpenTelemetry OTLP exporter to the application.
    // This exporter will send telemetry data to an OTLP receiver, such as Prometheus
    builder.Services.AddOpenTelemetry().WithTracing(builder => builder.AddOtlpExporter());
    builder.Services.AddOpenTelemetry().WithMetrics(builder => builder.AddOtlpExporter());
    
    // Build the ASP.NET Core web application.
    var app = builder.Build();
    
    // Start the ASP.NET Core web application.
    app.Run();
    

OpenTelemetry 配置

使用 Azure Monitor OpenTelemetry 发行版时,可以通过环境变量访问以下 OpenTelemetry 配置。

环境变量 说明
APPLICATIONINSIGHTS_CONNECTION_STRING 将其设置为 Application Insights 资源的连接字符串。
APPLICATIONINSIGHTS_STATSBEAT_DISABLED 将其设置为 true 以选择退出内部指标收集。
OTEL_RESOURCE_ATTRIBUTES 要用作资源属性的键值对。 有关资源属性的详细信息,请参阅资源 SDK 规范
OTEL_SERVICE_NAME 设置 service.name 资源属性的值。 如果 service.name 中也提供了 OTEL_RESOURCE_ATTRIBUTES,则 OTEL_SERVICE_NAME 优先。

编辑 URL 查询字符串

若要编辑 URL 查询字符串,请禁用查询字符串收集。 如果使用 SAS 令牌调用 Azure 存储,建议使用此设置。

使用 Azure.Monitor.OpenTelemetry.AspNetCore 发行版包时,会包含 ASP.NET CoreHttpClient 检测库。 默认情况下,我们的发行版包将“查询字符串编辑”设置为关闭。

若要更改此行为,必须将环境变量设置为 truefalse

  • ASP.NET Core 检测:OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_DISABLE_URL_QUERY_REDACTION 默认情况下,“查询字符串编辑”处于禁用状态。 若要启用,请将此环境变量设置为 false
  • Http 客户端检测:OTEL_DOTNET_EXPERIMENTAL_HTTPCLIENT_DISABLE_URL_QUERY_REDACTION 默认情况下,“查询字符串编辑”处于禁用状态。 若要启用,请将此环境变量设置为 false