Skip to content

fix: align exported OTel span IDs with propagated run contexts - #113

Open
pear1-pea (pear1-pea) wants to merge 1 commit into
langchain-ai:mainfrom
pear1-pea:fix/otel-run-span-identity
Open

pear1-pea (pear1-pea) wants to merge 1 commit into
langchain-ai:mainfrom
pear1-pea:fix/otel-run-span-identity

Conversation

@pear1-pea

Copy link
Copy Markdown

Native OpenTelemetry spans created inside @traceable calls use a deterministic run span ID as their parent, while the exporter currently creates the LangSmith span with a new random ID. This leaves the native spans pointing at a parent that was never exported (fixes #58). Root spans can also inherit an artificial parent, and an external OTel parent can lead to different trace IDs during execution and export.

This change gives each exported run a private copy of the standard SDK tracer with a run-specific ID generator. The copy retains the provider's sampler, processors, resource, scope, and limits without changing the shared provider or cached tracer. Execution contexts preserve an existing parent's trace ID, flags, and tracestate. Root runs use an empty parent context, and nested runs can recover their deterministic parent ID even if the parent's batch has not arrived.

Regression tests cover sync/async @traceable calls, external parent contexts, sampling inherited from a remote parent, nested and out-of-order batches, updates, concurrent exports, processor start/end identity, and late global provider initialization.

The ID override applies to standard opentelemetry-sdk tracers, including normal subclasses. Other API tracer implementations keep their own span-creation behavior. This does not change the existing synthetic-context sampling lifecycle: with no external parent and ParentBased(ALWAYS_OFF), native children can already be sampled while the run is dropped.

Validation (Python 3.12.16):

  • Regression suite on unchanged base: 13 failed, 1 passed; with the fix: 14 passed.
  • OTel exporter/URL/identity tests: 71 passed; the hybrid test module is skipped by the repository itself.
  • Run helpers: 124 passed, 2 skipped.
  • Identity suite with opentelemetry-sdk 1.30.0 and 1.45.2: 14 passed on each.
  • Independent offline Client holdouts: 79 spans for a sampled remote parent, 0 for an unsampled parent, zero network attempts.
  • Ruff lint and format, focused mypy, and wheel build passed.

The full all-extras test/typecheck suite and live-service integration suites were not run.

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.

Trace context not propagated into OpenTelemetry spans

1 participant