如何将自定义维度添加到 ASGI Web 应用程序中的 App Insights 请求遥测?

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

我有一个使用 Quart 的 python Web 应用程序。以下是我向其中添加样板 App Insights 请求遥测的方法:

...
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

bp = Blueprint("routes", __name__, static_folder='static')

def create_app():
    tracer_provider = TracerProvider()
    tracer_provider.add_span_processor(BatchSpanProcessor(AzureMonitorTraceExporter()))

    app = Quart(__name__)
    app.asgi_app = OpenTelemetryMiddleware(app.asgi_app, tracer_provider = tracer_provider)
    app.register_blueprint(bp)
    return app
...

我还将

APPLICATIONINSIGHTS_CONNECTION_STRING
环境变量设置为 App Insights 连接字符串。

它有效 - 我在 App Insights 中获得了样板请求遥测。

现在我想用自定义维度来丰富请求遥测。我知道它的理论:

  1. 具有公开字典的请求范围单例。
  2. 在代码中的任何一点,我们都可以向该字典添加条目
  3. 在请求结束之前,有一段代码用于发送 App Insights 遥测数据。应该有一种方法以某种方式自定义此代码,以将字典添加到请求遥测中,如
    customDimensions

我们已在 Asp.Net 应用程序中实现了它。但我不知道如何使用 Quart 将其转换为 python Web 应用程序。

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

探索图书馆提供的示例怎么样

azure-monitor-opentelemetry

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