Application Insights 中的 Python opentelemetry 事件

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

我正在按照以下指南尝试为我的 django 应用程序在 Azure Application Insights 中设置日志记录:

https://uptrace.dev/get/instrument/opentelemetry-django.html https://uptrace.dev/opentelemetry/python-tracing.html https://opentelemetry.io/docs/languages/python/automatic/logs-example/

最终得到的代码如下所示:

myapp/manage.py

def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')

    # Configure OpenTelemetry to use Azure Monitor with the specified connection string
    configure_azure_monitor(
        connection_string="InstrumentationKey=myKey;IngestionEndpoint=https://centralus-2.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/",
    )

    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()

myapp/views.py

import logging
from opentelemetry import trace

class SomeView(LoginRequiredMixin, TemplateView):
    login_required = True
    template_name = "myapp/index.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        tracer = trace.get_tracer(__name__)

        with tracer.start_as_current_span("SomeView") as span:
            if span.is_recording():
                span.set_attribute("user.id", self.request.user.id)
                span.set_attribute("user.email", self.request.user.email)

                span.add_event("log", {
                    "log.severity": "info",
                    "log.message": "Mark was here.",
                    "user.id": self.request.user.id,
                    "user.email": self.request.user.email,
                })

                span.add_event("This is a span event")
                logging.getLogger().error("This is a log message")

        context['something'] = SomeThing.objects.all()
        
        return context

好处:我确实在 Application Insights 中得到了结果。

当我查看端到端交易详细信息时,我看到类似这样的内容,这很棒。

Traces & Events
10 Traces
0 Events <- THIS IS THE ISSUE
View timeline
Filter to a specific component and call
Local time  Type    Details
1:32:52.989 PM  Request Name: GET some/path/, Successful request: true, Response time: 5.6 s, URL: https://someurl.com
1:32:53.260 PM  Trace   Message: log
1:32:53.260 PM  Trace   Message: This is a span event
1:32:53.260 PM  Trace   Severity level: Error, Message: This is a log message
1:32:53.260 PM  Internal    Name: SomeView, Type: InProc, Call status: true
1:32:53.577 PM  Trace   Severity level: Information, Message: some
1:32:53.587 PM  Trace   Severity level: Information, Message: message
1:32:53.602 PM  Trace   Severity level: Information, Message: here

但是,我无法记录实际的事件。因此,当我查看 Application Insights 并单击菜单中的“活动日志”时,我只看到一条消息“没有要显示的事件”。

所以,我的跟踪工作正常,但我无法记录“事件”。任何帮助将不胜感激。

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

AFAK,这是使用 OpenTelemetry 创建事件时的一个错误,我在 Github 中提出了支持请求 作为替代方案,您可以将以下代码集成到您的 djjango

views.py
manage.py
中:

import logging
from opencensus.ext.azure.log_exporter import AzureEventHandler


rithwik_lor = logging.getLogger(__name__)
rithwik_lor.addHandler(AzureEventHandler(connection_string='InstrumentationKey=hg65f87-u8frx;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/'))
rithwik_lor.setLevel(logging.INFO)
rithwik_lor.info('Hi Mr.Bojja, Event Created In Events')

Output:

enter image description here

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