Skip to content

Decorate Python logs with Workflow UUID and Trace ID #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2025
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
14 changes: 14 additions & 0 deletions dbos/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.sdk.resources import Resource
from opentelemetry.trace.span import format_trace_id

from dbos._utils import GlobalParams

Expand All @@ -26,6 +27,19 @@ def filter(self, record: Any) -> bool:
record.applicationID = self.app_id
record.applicationVersion = GlobalParams.app_version
record.executorID = GlobalParams.executor_id

# If available, decorate the log entry with Workflow ID and Trace ID
from dbos._context import get_local_dbos_context

ctx = get_local_dbos_context()
if ctx:
if ctx.is_within_workflow():
record.operationUUID = ctx.workflow_id
span = ctx.get_current_span()
if span:
trace_id = format_trace_id(span.get_span_context().trace_id)
record.traceId = trace_id

return True


Expand Down