Python opentelemetry wsgi 与gunicorn / Application Insights 的使用

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

我的 django 应用程序中的以下设置在开发模式下完美运行,因此当我运行

python manage.py runsslserver
时,应用程序会向 Application Insights 完美报告。

from azure.monitor.opentelemetry import configure_azure_monitor

from django.conf import settings as my_settings


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my.settings')

    # Configure OpenTelemetry to use Azure Monitor with the specified connection string
    AZ_OPENTELEMETRY_CONNECTION_STRING = impact3_settings.AZ_OPENTELEMETRY_CONNECTION_STRING
    configure_azure_monitor(
        connection_string=AZ_OPENTELEMETRY_CONNECTION_STRING,
    )

    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

但是,当我将其投入生产时,我们正在使用gunicorn和wsgi,因此,manage.py从未运行。我找到了一种将

OpenTelemetryMiddleware
添加到 wsgi 文件的方法,但不知道如何/在哪里调用
configure_azure_monitor
来记录每个请求。我错过了什么?

import os

from django.core.wsgi import get_wsgi_application

from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my.settings')

application = get_wsgi_application()
application = OpenTelemetryMiddleware(application)
python django azure-application-insights wsgi open-telemetry
1个回答
0
投票

我找到了一种将

OpenTelemetryMiddleware
添加到 wsgi 文件的方法,但不知道如何/在哪里调用
configure_azure_monitor
来记录每个请求。我错过了什么?

这里需要配置OpenTelemetry并添加中间件来拦截传入的请求。

  • 修改 WSGI 文件以包含 OpenTelemetry 配置:

代码:

import os
import sys

from django.core.wsgi import get_wsgi_application
from opentelemetry.instrumentation.django.middleware import OpenTelemetryMiddleware
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.exporter.azuremonitor.trace_exporter import AzureMonitorTraceExporter
from opentelemetry import trace

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my.settings')

# Configure OpenTelemetry
tracer_provider = TracerProvider(resource=Resource.create({'service.name': 'your-service-name'}))

# Export traces to Azure Monitor
connection_string = "YOUR_CONNECTION_STRING"
azure_exporter = AzureMonitorTraceExporter.from_connection_string(connection_string)
span_processor = BatchExportSpanProcessor(azure_exporter)
tracer_provider.add_span_processor(span_processor)

# Configure OpenTelemetry middleware
application = get_wsgi_application()
application = OpenTelemetryMiddleware(application)

# Start trace provider
trace.set_tracer_provider(tracer_provider)
  • 这里我配置了自定义事件跟踪。

自定义事件:

enter image description here

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