census-instrumentation / opencensus-python

A stats collection and distributed tracing framework
Apache License 2.0
667 stars 248 forks source link

Missing Trace and Span Id in exceptions #1200

Closed VolkerSchiewe closed 1 year ago

VolkerSchiewe commented 1 year ago

Describe your environment. Python 3.11.3-slim-bullseye FastAPI 0.95.0 opencensus "^0.11.2" opencensus-ext-azure = "1.1.7" opencensus-ext-logging = "^0.1.1" opencensus-ext-requests = "^0.8.0" opencensus-ext-fastapi = "^0.1.0"

Steps to reproduce.

  1. Setup logging tracing with opencensus-ext-logging
  2. Setup azure log exporter with opencensus-ext-azure
  3. log exception with logger.exception(e)
  4. The exception is shown in azure, but traceId and spanId are None

What is the expected behavior? traceId and spanId are injected into all logs independent of log level

What is the actual behavior? TraceId and SpanId are part of info log level but not of warning or error level

Additional context.

VolkerSchiewe commented 1 year ago

Closing because I didn't execute the log tracing extension before initialising the logger which was calling logger.exception. Learning: if you are using opencensus-ext-logging make sure to run config_integration.trace_integrations(["logging"]) before any logger is initialised.

Note that loggers are often initialised when importing files!