Inject sampled traceparent on workflow execute calls#459
Merged
Conversation
Without a traceparent header, external workers inherit the API's HTTP span context via Temporal. In production the API's spans are frequently unsampled (ParentBasedTraceIdRatio), so workers produce no-op spans and traces never reach the collector — /trace/otel returns WF_1500. Adds a BeforeRequestHook that fires on any /execute path and injects a sampled W3C traceparent: forwarding the active OTEL span if it is already sampled, otherwise generating a fresh sampled one. An explicitly set traceparent header is never overwritten.
Covers: no-op on non-execute paths, sampled header injection, explicit header preservation, OTEL context propagation, fallback for unsampled or absent spans, and uniqueness of generated IDs.
- Shorten TraceparentInjectionHook docstring to one line - Remove module docstring from test file - Drop low-ROI uniqueness test
Remove unused opentelemetry.trace import (ruff F401) and add isinstance assertions so pyright can narrow Union[Request, Exception] before accessing .headers.
Matching on request.url.path.endswith("/execute") would affect any
future endpoint that happens to share that suffix. Keying on the
operation ID is explicit and safe.
mistralai-nfau
approved these changes
Mar 31, 2026
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
External workers return
WF_1500on/trace/otel,/trace/summary, and/trace/eventsbecause no sampled traceparent reaches the worker.Root cause
Workers use
ParentBasedTraceIdRatio: if the parent span is unsampled, every worker span becomes aNonRecordingSpan— nothing reaches the OTEL collector. Without atraceparentheader, the worker inherits the API's HTTP span context via Temporal. In production, those spans are frequently unsampled.Fix
A
BeforeRequestHook(TraceparentInjectionHook) is registered for all SDK instances. On any request whose path ends with/execute, it injects a sampled W3C traceparent header: forwarding the active OTEL span context if it is already sampled, otherwise generating a fresh sampled one. An explicitly settraceparentheader is never overwritten.Testing
9 unit tests covering: no-op on non-execute paths, header injection, explicit header preservation, OTEL context propagation, fallback for unsampled/absent spans, and ID uniqueness. All pass locally.
Fixes: WFL-927
Slack: https://mistralai.slack.com/archives/C09BLDVF57C/p1774892554307479?thread_ts=1774890508.653139&cid=C09BLDVF57C