閱讀英文

共用方式為

配置 Azure Monitor OpenTelemetry

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

连接字符串

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

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

  • 设置环境变量。

    APPLICATIONINSIGHTS_CONNECTION_STRING=<Your Connection String>
    
  • 使用 configure_azure_monitor 函数。

# Import the `configure_azure_monitor()` function from the `azure.monitor.opentelemetry` package.
from azure.monitor.opentelemetry import configure_azure_monitor

# Configure OpenTelemetry to use Azure Monitor with the specified connection string.
# Replace `<your-connection-string>` with the connection string of your Azure Monitor Application Insights resource.
configure_azure_monitor(
    connection_string="<your-connection-string>",
)

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

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

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

使用 OTEL_RESOURCE_ATTRIBUTES 和/或 OTEL_SERVICE_NAME 环境变量设置资源特性。 OTEL_RESOURCE_ATTRIBUTES 接收一系列以逗号分隔的键值对。 例如,若要将云角色名称设置为 my-namespace.my-helloworld-service ,并将云角色实例设置为 my-instance,可按如下所示设置 OTEL_RESOURCE_ATTRIBUTESOTEL_SERVICE_NAME

export OTEL_RESOURCE_ATTRIBUTES="service.namespace=my-namespace,service.instance.id=my-instance"
export OTEL_SERVICE_NAME="my-helloworld-service"

如果未设置 service.namespace 资源属性,也可仅使用 OTEL_SERVICE_NAME 环境变量或 service.name 资源属性来设置云角色名称。 例如,若要将云角色名称设置为 my-helloworld-service ,并将云角色实例设置为 my-instance,可按如下所示设置 OTEL_RESOURCE_ATTRIBUTESOTEL_SERVICE_NAME

export OTEL_RESOURCE_ATTRIBUTES="service.instance.id=my-instance"
export OTEL_SERVICE_NAME="my-helloworld-service"

启用采样

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

注意

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

configure_azure_monitor() 函数会自动利用 ApplicationInsightsSampler 与 Application Insights SDK 的兼容,并对遥测数据进行采样。 OTEL_TRACES_SAMPLER_ARG 环境变量可用于指定采样率,有效范围为 0 到 1,其中 0 代表 0%,1 代表 100%。 例如,值 0.1 表示会发送 10% 的跟踪。

export OTEL_TRACES_SAMPLER_ARG=0.1

提示

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

实时指标

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

重要

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

可以使用适用于 Python 的 Azure Monitor OpenTelemetry 发行版启用实时指标,如下所示:

...
configure_azure_monitor(
	enable_live_metrics=True
)
...

脱机存储和自动重试

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

默认情况下,Azure Monitor 导出程序使用以下路径:

<tempfile.gettempdir()>/Microsoft/AzureMonitor/opentelemetry-python-<your-instrumentation-key>

若要替代默认目录,应将 storage_directory 设置为所需的目录。

例如:

...
# Configure OpenTelemetry to use Azure Monitor with the specified connection string and storage directory.
# Replace `your-connection-string` with the connection string to your Azure Monitor Application Insights resource.
# Replace `C:\\SomeDirectory` with the directory where you want to store the telemetry data before it is sent to Azure Monitor.
configure_azure_monitor(
    connection_string="your-connection-string",
    storage_directory="C:\\SomeDirectory",
)
...

若要禁用此功能,应将 disable_offline_storage 设置为 True。 默认为 False

例如:

...
# Configure OpenTelemetry to use Azure Monitor with the specified connection string and disable offline storage.
# Replace `your-connection-string` with the connection string to your Azure Monitor Application Insights resource.
configure_azure_monitor(
    connection_string="your-connection-string",
    disable_offline_storage=True,
)
...

启用 OTLP 导出器

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

注意

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

  1. 安装 opentelemetry-exporter-otlp 包。

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

    # Import the `configure_azure_monitor()`, `trace`, `OTLPSpanExporter`, and `BatchSpanProcessor` classes from the appropriate packages.    
    from azure.monitor.opentelemetry import configure_azure_monitor
    from opentelemetry import trace
    from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
    from opentelemetry.sdk.trace.export import BatchSpanProcessor
    
    # Configure OpenTelemetry to use Azure Monitor with the specified connection string.
    # Replace `<your-connection-string>` with the connection string to your Azure Monitor Application Insights resource.
    configure_azure_monitor(
        connection_string="<your-connection-string>",
    )
    
    # Get the tracer for the current module.
    tracer = trace.get_tracer(__name__) 
    
    # Create an OTLP span exporter that sends spans to the specified endpoint.
    # Replace `http://localhost:4317` with the endpoint of your OTLP collector.
    otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317")
    
    # Create a batch span processor that uses the OTLP span exporter.
    span_processor = BatchSpanProcessor(otlp_exporter)
    
    # Add the batch span processor to the tracer provider.
    trace.get_tracer_provider().add_span_processor(span_processor)
    
    # Start a new span with the name "test".
    with tracer.start_as_current_span("test"):
        print("Hello world!")
    

OpenTelemetry 配置

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

有关 OpenTelemetry SDK 配置的详细信息,请参阅 OpenTelemetry 文档Azure monitor 发行版使用情况

编辑 URL 查询字符串

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

我们正在 OpenTelemetry 社区中积极工作以支持编辑。