Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions lite_bootstrap/instruments/logging_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@
import structlog


if import_checker.is_opentelemetry_installed:
from opentelemetry import trace

def tracer_injection(_: "WrappedLogger", __: str, event_dict: "EventDict") -> "EventDict":
current_span = trace.get_current_span()
if not current_span.is_recording():
event_dict["tracing"] = {}
return event_dict

current_span_context = current_span.get_span_context()
event_dict["tracing"] = {
"span_id": trace.format_span_id(current_span_context.span_id),
"trace_id": trace.format_trace_id(current_span_context.trace_id),
}
return event_dict

else: # pragma: no cover

def tracer_injection(_: "WrappedLogger", __: str, event_dict: "EventDict") -> "EventDict":
return event_dict


ScopeType = typing.MutableMapping[str, typing.Any]


Expand All @@ -32,25 +54,6 @@ class RequestProtocol(typing.Protocol):
method: str


def tracer_injection(_: "WrappedLogger", __: str, event_dict: "EventDict") -> "EventDict":
try:
from opentelemetry import trace # noqa: PLC0415
except ImportError: # pragma: no cover
return event_dict

current_span = trace.get_current_span()
if not current_span.is_recording():
event_dict["tracing"] = {}
return event_dict

current_span_context = current_span.get_span_context()
event_dict["tracing"] = {
"span_id": trace.format_span_id(current_span_context.span_id),
"trace_id": trace.format_trace_id(current_span_context.trace_id),
}
return event_dict


if import_checker.is_structlog_installed:

class MemoryLoggerFactory(structlog.stdlib.LoggerFactory):
Expand Down
Loading