如何在 Django 中将 Azure AppInsights 与 OpenTelemetry 结合使用

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

您好,我有一个 Django 应用程序,我想将我的传统日志与 Azure AppInsights 连接起来。

我正在使用日志记录库来记录日志,这段代码在项目中出现了 100 次。

import logging 
logger = logging.getLogger(__name__) 

当我检查开放遥测 Azure 文档时,我发现了以下示例,但要使用这个示例,我必须更改所有记录器实例,因为我们需要添加开放遥测的处理程序和 Azure 的导出器。有什么方法可以在不更改所有记录器出现次数的情况下做到这一点吗?

logger_provider = LoggerProvider()
set_logger_provider(logger_provider)
exporter = AzureMonitorLogExporter.from_connection_string(
    os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(exporter, schedule_delay_millis=60000))

# Attach LoggingHandler to namespaced logger
handler = LoggingHandler()
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

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

是的,您可以使用 OpenTelemetry 日志集成将 OpenTelemetry 与 Azure Monitor 集成,而无需更改所有记录器实例。

此示例展示了如何使用日志记录

open telemetry 

  • 下面的代码使用 OpenTelemetry 与 Azure Monitor 集成以从应用程序收集跟踪。
  • 代码参考取自 MSDOCOpenTelemetry Django
# Import necessary libraries
import logging
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.instrumentation.django import DjangoInstrumentor

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Configure OpenTelemetry to use Azure Monitor with the specified connection string
configure_azure_monitor(
    connection_string="AzureApplication Insights connection_string",
)

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

# Instrument Django app
DjangoInstrumentor().instrument()

# Your Django app code goes here...

# Start a new span with the name "hello"
with tracer.start_as_current_span("hello"):
    logger.info("Hello, World!")

# Wait for export to take place in the background
input()


使用 OpenTelemetry 日志记录集成:

  • 下面的代码充当 OpenTelemetry 之间的桥梁并跨越您现有的日志记录基础设施。它捕获跨度信息并将其附加到日志中,而无需修改现有的日志记录调用。
import logging
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.logging import LoggingInstrumentor

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Configure OpenTelemetry to use Azure Monitor with the specified connection string
configure_azure_monitor(
    connection_string="AzureApplication Insights connection_string",
)

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

# Instrument Django app
DjangoInstrumentor().instrument()

# Instrument logging to capture spans
LoggingInstrumentor().instrument()

# Your Django app code goes here...

# Start a new span with the name "hello"
with tracer.start_as_current_span("hello"):
    logger.info("Hello, World!")

# Wait for export to take place in the background
input()

enter image description here

enter image description here

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