Expose SpanID and TraceID in workflow context - #477
Merged
cschleiden merged 2 commits intoMay 30, 2026
Conversation
Add SpanContext() to workflow.Span interface so users can access TraceID and SpanID from spans created via workflow.Tracer(ctx).Start(). Add workflow.SpanFromContext(ctx) to retrieve the current span from the workflow context without creating a new one. Include trace_id and span_id as structured log attributes in the workflow logger so workflow.Logger(ctx) output is automatically correlated with traces. Fixes #473 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add unit tests for SpanContext(), SpanFromContext(), and UpdateLoggerWithSpan covering: no-span context, real spans with valid TraceID/SpanID, replay behavior, and logger trace attribute injection. Add integration tests via the tester package verifying the APIs work end-to-end in workflow execution. Update documentation to describe accessing TraceID/SpanID from workflow spans and the automatic trace context in workflow logger output. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #473 — Users cannot obtain the TraceID and SpanID from within workflow code. After calling
workflow.Tracer(ctx).Start(ctx, "name"), the returnedSpanonly exposesEnd(), making it impossible to access trace context. Additionally,workflow.Logger(ctx)doesn't include trace correlation attributes.Changes
1.
SpanContext()onworkflow.SpaninterfaceThe
Spaninterface now exposesSpanContext() trace.SpanContext, allowing users to retrieve TraceID and SpanID:2.
workflow.SpanFromContext(ctx)New public helper to get the current active span from the workflow context (mirrors OTel's
trace.SpanFromContext):3. Trace context in workflow logger
The workflow logger now automatically includes
trace_idandspan_idas structured log attributes, so all log entries fromworkflow.Logger(ctx)are correlated with the workflow's trace span.Files changed
workflow/tracer.go— AddedSpanContext()to interface +SpanFromContextfunctioninternal/log/fields.go— AddedTraceIDKeyandSpanIDKeyconstantsinternal/workflowstate/workflowstate.go— AddedUpdateLoggerWithSpanmethodworkflow/executor/executor.go— CallUpdateLoggerWithSpanafter workflow span creation