Python OpenTelemetry 事件在 Application Insights中的使用
我正在按照下面的指南,尝试为我的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中点击菜单里的“活动日志”时,只看到一条消息:“没有事件可显示”。
所以,我的追踪功能是正常的,但我无法记录“事件”。任何帮助都将非常感激。
1 个回答
0
据我所知,这个问题是使用OpenTelemetry创建事件时出现的一个bug。我在Github上提出了支持请求。
作为替代方案,你可以把下面的代码整合到你的Django项目中的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')
输出: