Skip to content

Enable Trace Propagation#65

Merged
brandur merged 7 commits into
riverqueue:masterfrom
AnthonySuper:enable-trace-propagation
Jun 29, 2026
Merged

Enable Trace Propagation#65
brandur merged 7 commits into
riverqueue:masterfrom
AnthonySuper:enable-trace-propagation

Conversation

@AnthonySuper

Copy link
Copy Markdown
Contributor

When the configuration parameter EnableTracePropagation is enabled, the traces generated by river workers will be linked to the traces that enqueued them. This is done by use of metadata parameters, following the example in River's documentation for the middleware functionality (https://riverqueue.com/docs/middleware).

This uses a link instead of directly making a child span which is semantically more correct—if a parent span is used, you get weird situations where a POST request that returns in 50ms somehow contains a trace that starts two hours later and takes 10 minutes. River jobs are async, after all.

Fixes #41 (I believe, at least).

When the configuration parameter `EnableTracePropagation` is enabled,
the traces generated by river workers will be *linked* to the traces
that enqueued them. This is done by use of metadata parameters,
following the example in River's documentation for the middleware
functionality (https://riverqueue.com/docs/middleware).

This uses a *link* instead of directly making a child span which is
semantically more correct.

Fixes riverqueue#41 (I believe, at least).
@brandur

brandur commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

@codex review

@AnthonySuper

Copy link
Copy Markdown
Contributor Author

Just an FYI: we've rolled this code out in production at my company and it has been successful (traces from jobs are properly linking to whatever kicked them off).

@brandur brandur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff! Want to add a changelog entry?

Comment thread otelriver/middleware.go Outdated
Comment thread otelriver/middleware.go
}
}
extracted := propagation.TraceContext{}.Extract(ctx, carrier)
return trace.SpanFromContext(extracted).SpanContext()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this piece is subtly not-quite-right in that the purpose of this function is always to pull a span context out of the given metadata and we should never be looking to get out of active context. What do you think about removing the ctx parameter from extractSpanContext in favor of using context.Background() here? If no span context is founded in metadata, then nothing should be extracted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 0ea8d93 - along with a comment explaining what's goiung on.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, thanks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you take a look at the lint problem below? For this one, you probably just want to add a //nolint:contextcheck on the line.

Comment thread otelriver/middleware.go Outdated

// EnableTracePropagation injects W3C trace context (traceparent/tracestate)
// into job metadata on insert and extracts it on work, adding a span link
// from the work span back to the span that enqueued the job. A link is used

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to maybe move the sentence starting from "A link is used ..." from here down to this line?

startOpts = append(startOpts, trace.WithLinks(trace.Link{SpanContext: sc}))

It feels like rationalizing the use of a link here is a little bit TMI for the docblock and belongs more in an internal comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c6e4073

Good call!

@AnthonySuper

Copy link
Copy Markdown
Contributor Author

Good stuff! Want to add a changelog entry?

Added in 648c0f8.

@brandur brandur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@brandur brandur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple minor lint things.

Comment thread otelriver/middleware.go
}
}
extracted := propagation.TraceContext{}.Extract(ctx, carrier)
return trace.SpanFromContext(extracted).SpanContext()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you take a look at the lint problem below? For this one, you probably just want to add a //nolint:contextcheck on the line.

Comment thread otelriver/middleware.go Outdated
Comment thread CHANGELOG.md Outdated
AnthonySuper and others added 2 commits June 29, 2026 15:12
Co-authored-by: Brandur <brandur@brandur.org>
This is no longer used. Also fixes lints.
@AnthonySuper

Copy link
Copy Markdown
Contributor Author

Should be all fixed now!

@brandur

brandur commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

thx!

@brandur brandur merged commit e29ff74 into riverqueue:master Jun 29, 2026
2 checks passed
@brandur brandur mentioned this pull request Jun 29, 2026
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.

Proposal: Persist trace context in job metadata

2 participants