如何使用 azure.monitor.opentelemetry 在 Python 中设置跟踪的 application_Version

问题描述 投票:0回答:1

在我的跟踪中,我想记录我的应用程序的版本,以便我可以过滤每个构建。 ApplicationInsights 的 Traces 表有一列 application_Version,我认为这就是要使用的列。

我找不到如何从 Python 中做到这一点,但根据这篇文章,这似乎是可能的: 如何在 Azure Application Insights 中设置用户和异常数据的应用程序版本号?

看起来我应该使用 SERVICE_VERSION 属性: https://github.com/open-telemetry/opentelemetry-python/blob/41b9e26d8324ae0496c85326b35e92bf617932d9/opentelemetry-semantic-conventions/src/opentelemetry/semconv/resource/__init__.py#L433

我尝试添加:

"OTEL_RESOURCE_ATTRIBUTES": "service.version=4.3.2",
"OTEL_SERVICE_VERSION": "4.3.2",

在 local.settings.json 中,但这不起作用。

python-3.x azure-application-insights open-telemetry
1个回答
0
投票

使用适用于 Python 的 Azure Core OpenTelemetry Tracing plugin。该库支持使用 OpenTelemetry 框架跟踪 Azure SDK,从而允许您监视和跟踪应用程序

import azure.functions as func
import logging
from azure.core.settings import settings
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

# Enable OpenTelemetry tracing for Azure SDKs
settings.tracing_implementation = OpenTelemetrySpan

# Set up OpenTelemetry tracer
exporter = ConsoleSpanExporter()
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
trace.get_tracer_provider().add_span_processor(
    SimpleSpanProcessor(exporter)
)

def main(req: func.HttpRequest) -> func.HttpResponse:
    with tracer.start_as_current_span(name="HTTPTriggerFunction"):
        logging.info('Python HTTP trigger function processed a request.')

        name = req.params.get('name')
        if not name:
            try:
                req_body = req.get_json()
            except ValueError:
                pass
            else:
                name = req_body.get('name')

        if name:
            return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
        else:
            return func.HttpResponse(
                "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
                status_code=200
            )

enter image description here

  • 服务详细信息可在属性下找到。

  • 请参阅此 SO 在 Application Insights 事件中设置 appName。

  • 下面的代码将遥测数据发送到 Azure Monitor,创建名称为

    hello
    的范围,并设置值为
    "service.version"
    的自定义属性
    "4.3.2"

  • 以下是在 Python 应用程序中启用 Azure Monitor OpenTelemetry 的代码,取自 MSDOC

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace

# Configure Azure Monitor with your connection string
configure_azure_monitor(connection_string="AzureApplicationInsightsConnectionString")

# Get a tracer for the current module
tracer = trace.get_tracer(__name__)

# Start a new span with the name "hello" (or any span name you prefer)
with tracer.start_as_current_span("hello") as span:
    # Set the service version attribute on the span
    span.set_attribute("service.version", "4.3.2")
    span.set_attribute("OTEL_RESOURCE_ATTRIBUTES ", "service.version=4.3.2")
    print("Hello, World!")

enter image description here

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.