Azure App Insights/Opentelemetry-跟踪不起作用

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

将 Opentelemetry 导出器用于 azure 监视器。我正在运行在 here 找到的 hello world 跟踪。它在应用程序见解中显示为“依赖项”而不是跟踪。我尝试了许多实现,但所有内容都显示为依赖项,无论它是 Web 请求还是本地代码执行。感谢任何帮助,显然有些东西我不明白。我不知道如何创建痕迹。

"""
An example to show an application using Opentelemetry tracing api and sdk. Custom dependencies are
tracked via spans and telemetry is exported to application insights with the AzureMonitorTraceExporter.
"""
import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
tracer = trace.get_tracer(__name__)
# This is the exporter that sends data to Application Insights
exporter = AzureMonitorTraceExporter(
    connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
span_processor = BatchSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)

with tracer.start_as_current_span("hello"):
    print("Hello, World!")

# Telemetry records are flushed automatically upon application exit
# If you would like to flush records manually yourself, you can call force_flush()
tracer_provider.force_flush()
azure azure-application-insights open-telemetry
1个回答
0
投票

您从 Microsoft-Document 获取的代码明确指出:

使用 Opentelemetry 跟踪 api 和 sdk 的应用程序。

Custom dependencies are tracked via spans
,遥测数据将通过 AzureMonitorTraceExporter 导出到应用程序见解。

您使用的代码只会将数据发送到依赖项,在注释中您可以清楚地看到您正在创建自定义依赖项,这就是为什么您会得到如下内容:

enter image description here

您无法仅使用 print 语句在跟踪中记录任何内容,您应该使用 Logger 及其处理程序进行记录,如下所示,我已经按照 SO-ThreadMicrosoft-Document 修改了代码:

import logging
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter

from opentelemetry._logs import (
    get_logger_provider,
    set_logger_provider,
)
from opentelemetry.sdk._logs import (
    LoggerProvider,
    LoggingHandler,
)
set_logger_provider(LoggerProvider())
chotu_exporter = AzureMonitorLogExporter(
    connection_string="InstrumentationKey=4ad5ee33-d4ae-4197-b799-fac737d3de20;IngestionEndpoint=https://centralindia-0.in.applicationinsights.azure.com/;LiveEndpoint=https://centralindia.livediagnostics.monitor.azure.com/"
)
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(chotu_exporter))

chotu_handler = LoggingHandler()
chotu_logger = logging.getLogger(__name__)
chotu_logger.addHandler(chotu_handler)
chotu_logger.setLevel(logging.INFO)

chotu_logger.info("Rithwik Bojja from Log INFO")
chotu_logger.warning("Rithwik Bojja from Log WARNING ")
chotu_logger.error("Rithwik Bojja from Log ERROR")

Output:

enter image description here

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