Skip to content

Expose SpanID and TraceID in workflow context - #477

Merged
cschleiden merged 2 commits into
mainfrom
cschleiden/expose-span-context-and-trace-logging
May 30, 2026
Merged

Expose SpanID and TraceID in workflow context#477
cschleiden merged 2 commits into
mainfrom
cschleiden/expose-span-context-and-trace-logging

Conversation

@cschleiden

Copy link
Copy Markdown
Owner

Problem

Fixes #473 — Users cannot obtain the TraceID and SpanID from within workflow code. After calling workflow.Tracer(ctx).Start(ctx, "name"), the returned Span only exposes End(), making it impossible to access trace context. Additionally, workflow.Logger(ctx) doesn't include trace correlation attributes.

Changes

1. SpanContext() on workflow.Span interface

The Span interface now exposes SpanContext() trace.SpanContext, allowing users to retrieve TraceID and SpanID:

ctx, span := workflow.Tracer(ctx).Start(ctx, "my operation")
defer span.End()

traceID := span.SpanContext().TraceID()
spanID := span.SpanContext().SpanID()

2. workflow.SpanFromContext(ctx)

New public helper to get the current active span from the workflow context (mirrors OTel's trace.SpanFromContext):

span := workflow.SpanFromContext(ctx)
if span != nil {
    traceID := span.SpanContext().TraceID()
}

3. Trace context in workflow logger

The workflow logger now automatically includes trace_id and span_id as structured log attributes, so all log entries from workflow.Logger(ctx) are correlated with the workflow's trace span.

Files changed

  • workflow/tracer.go — Added SpanContext() to interface + SpanFromContext function
  • internal/log/fields.go — Added TraceIDKey and SpanIDKey constants
  • internal/workflowstate/workflowstate.go — Added UpdateLoggerWithSpan method
  • workflow/executor/executor.go — Call UpdateLoggerWithSpan after workflow span creation

Christopher Schleiden and others added 2 commits April 10, 2026 22:51
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>
@cschleiden
cschleiden merged commit d0990f3 into main May 30, 2026
5 checks passed
@cschleiden
cschleiden deleted the cschleiden/expose-span-context-and-trace-logging branch May 30, 2026 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not possible to get the SpanID and TraceID inside Workflow

1 participant