Skip to content

Follow OTEL_TRACES_SAMPLER in the OTLP tracer - #10108

Merged
tim-smart merged 8 commits into
Effect-TS:v4/next-minorfrom
duailibe:otlp-tracer-sampler
Oct 29, 2026
Merged

tim-smart merged 8 commits into
Effect-TS:v4/next-minorfrom
duailibe:otlp-tracer-sampler

Conversation

@duailibe

@duailibe duailibe commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

OtlpTracer.layerFromConfig reads the exporter, endpoint, headers, compression and batch variables, but not OTEL_TRACES_SAMPLER or OTEL_TRACES_SAMPLER_ARG. It exports every trace, so a service that wants parentbased_traceidratio has to wrap the tracer by hand.

layerFromConfig now reads both variables and passes them to a new sampler option on OtlpTracer.make and OtlpTracer.layer. The option is { ratio, parentBased }, and it covers all six samplers in the spec. always_on is ratio 1 and always_off is ratio 0. The traceidratio samplers take the ratio from OTEL_TRACES_SAMPLER_ARG. The parentbased_ prefix sets parentBased. Otlp.layer, Otlp.layerJson and Otlp.layerProtobuf accept the same option as tracerSampler.

Notes:

  • The tracer decides when it creates a span, and outgoing traceparent headers carry the decision. The exporter already skipped unsampled spans.
  • The decision comes from the trace ID, not Math.random(). Like the OpenTelemetry probability sampler, the tracer keeps a trace when the last 56 bits of its ID reach (1 - ratio) * 2^56. It compares the top 52 of those bits, so the number fits in a double exactly.
  • With parentBased, a span with any parent, local or remote, follows the parent's sampled flag, even when it is created with sampled: true. This matches the spec's defaults for ParentBased.
  • When OTEL_TRACES_SAMPLER is unset, no sampler is installed and tracing behaves exactly as before, including an explicit sampled: true overriding an unsampled parent.
  • When the digits the sampler reads from a trace ID are not hex, as with an external parent whose ID is not a W3C trace ID, there is no usable randomness and the trace is kept. Ratio 0 still drops it.
  • The sampler can only drop spans. Effect's sampled flag already mixes the parent's flag, MinimumTraceLevel and an explicit sampled option, and the tracer can't tell them apart. So always_on and traceidratio don't sample the children of an unsampled remote parent, though the spec says they should, unless the child is created with sampled: true, in which case the non-parent-based samplers decide it by its trace ID. A root span created with sampled: true can still be dropped.
  • An unsupported OTEL_TRACES_SAMPLER such as jaeger_remote or xray, or an OTEL_TRACES_SAMPLER_ARG outside [0, 1], fails the layer. A bad OTEL_EXPORTER_OTLP_COMPRESSION already does the same, and values are matched case-sensitively like compression. The spec asks SDKs to warn and ignore unknown values. I kept the pattern the rest of layerFromConfig uses.

Closes EFF-3116

`OtlpTracer.layerFromConfig` reads the exporter, endpoint, headers, compression and batch variables, but not `OTEL_TRACES_SAMPLER` or `OTEL_TRACES_SAMPLER_ARG`. It exports every trace, so a service that wants `parentbased_traceidratio` has to wrap the tracer by hand.

`layerFromConfig` now reads both variables and passes them to a new `sampler` option on `OtlpTracer.make` and `OtlpTracer.layer`. The option is `{ ratio, parentBased }`, and it covers all six samplers in the spec. `always_on` is ratio 1 and `always_off` is ratio 0. The `traceidratio` samplers take the ratio from `OTEL_TRACES_SAMPLER_ARG`. The `parentbased_` prefix sets `parentBased`.

Notes:

- The tracer decides when it creates a span. Local children then follow the existing rule that children of an unsampled parent are unsampled, and outgoing `traceparent` headers carry the decision. The exporter already skipped unsampled spans.
- The decision comes from the trace ID, not `Math.random()`. Like the OpenTelemetry probability sampler, the tracer keeps a trace when the last 56 bits of its ID reach `(1 - ratio) * 2^56`. It compares the top 52 of those bits, so the number fits in a double exactly.
- With `parentBased`, a span with any parent, local or remote, keeps the parent's decision. This matches the spec's defaults for `ParentBased`.
- The sampler can only drop spans. Effect's `sampled` flag already mixes the parent's flag, `MinimumTraceLevel` and an explicit `sampled` option, and the tracer can't tell them apart. So `always_on` and `traceidratio` don't sample the children of an unsampled remote parent, though the spec says they should. A root span created with `sampled: true` can still be dropped.
- An unsupported `OTEL_TRACES_SAMPLER` such as `jaeger_remote` or `xray`, or an `OTEL_TRACES_SAMPLER_ARG` outside [0, 1], fails the layer. A bad `OTEL_EXPORTER_OTLP_COMPRESSION` already does the same, and values are matched case-sensitively like compression. The spec asks SDKs to warn and ignore unknown values. I kept the pattern the rest of `layerFromConfig` uses.
- I didn't add a `tracerSampler` option to `Otlp.layer`, `Otlp.layerJson` or `Otlp.layerProtobuf`. It's a small follow-up if wanted.
@changeset-bot

changeset-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 798522c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 33 packages
Name Type
effect Minor
@effect/opentelemetry Minor
@effect/vitest Minor
@effect/ai-anthropic Minor
@effect/ai-cloudflare Minor
@effect/ai-openai-compat Minor
@effect/ai-openai Minor
@effect/ai-openrouter Minor
@effect/ai-typesafe Minor
@effect/atom-react Minor
@effect/atom-solid Minor
@effect/atom-vue Minor
@effect/platform-browser Minor
@effect/platform-bun Minor
@effect/platform-deno Minor
@effect/platform-node-shared Minor
@effect/platform-node Minor
@effect/sql-clickhouse Minor
@effect/sql-d1 Minor
@effect/sql-libsql Minor
@effect/sql-mssql Minor
@effect/sql-mysql2 Minor
@effect/sql-pg Minor
@effect/sql-pglite Minor
@effect/sql-sqlite-bun Minor
@effect/sql-sqlite-do Minor
@effect/sql-sqlite-node Minor
@effect/sql-sqlite-react-native Minor
@effect/sql-sqlite-wasm Minor
@effect/docgen Minor
@effect/doctest Minor
@effect/graphql-generator Minor
@effect/openapi-generator Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@effect-janitor effect-janitor Bot added the 4.0 label Oct 29, 2026
@effect-bot
effect-bot changed the base branch from main to v4/next-minor October 29, 2026 10:41
@effect-janitor effect-janitor Bot removed the 4.0 label Oct 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 39.35 KB 39.35 KB 0.00 KB (0.00%)
basic.ts 7.18 KB 7.18 KB 0.00 KB (0.00%)
batching.ts 11.06 KB 11.06 KB 0.00 KB (0.00%)
brand.ts 6.84 KB 6.84 KB 0.00 KB (0.00%)
cache.ts 11.49 KB 11.49 KB 0.00 KB (0.00%)
config.ts 22.77 KB 22.77 KB 0.00 KB (0.00%)
differ.ts 21.92 KB 21.92 KB 0.00 KB (0.00%)
http-client.ts 26.86 KB 26.86 KB 0.00 KB (0.00%)
http-router.ts 36.85 KB 36.85 KB 0.00 KB (0.00%)
logger.ts 11.96 KB 11.96 KB 0.00 KB (0.00%)
metric.ts 9.42 KB 9.42 KB 0.00 KB (0.00%)
optic.ts 7.05 KB 7.05 KB 0.00 KB (0.00%)
pubsub.ts 15.97 KB 15.97 KB 0.00 KB (0.00%)
queue.ts 12.77 KB 12.77 KB 0.00 KB (0.00%)
schedule.ts 11.74 KB 11.74 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 14.02 KB 14.02 KB 0.00 KB (0.00%)
schema-binary.ts 41.06 KB 41.06 KB 0.00 KB (0.00%)
schema-class.ts 21.65 KB 21.65 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 29.51 KB 29.51 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 27.91 KB 27.91 KB 0.00 KB (0.00%)
schema-string-transformation.ts 15.27 KB 15.27 KB 0.00 KB (0.00%)
schema-string.ts 12.26 KB 12.26 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.19 KB 16.19 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 38.87 KB 38.87 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 26.10 KB 26.10 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 20.83 KB 20.83 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 20.97 KB 20.97 KB 0.00 KB (0.00%)
schema-toFormatter.ts 21.07 KB 21.07 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 26.11 KB 26.11 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 21.16 KB 21.16 KB 0.00 KB (0.00%)
schema.ts 20.75 KB 20.75 KB 0.00 KB (0.00%)
stm.ts 14.15 KB 14.15 KB 0.00 KB (0.00%)
stream.ts 10.33 KB 10.33 KB 0.00 KB (0.00%)

Comment thread .changeset/otlp-tracer-sampler.md Resolved
@tim-smart
tim-smart enabled auto-merge (squash) October 29, 2026 22:19
@tim-smart
tim-smart merged commit 658a5cb into Effect-TS:v4/next-minor Oct 29, 2026
11 checks passed
tim-smart added a commit that referenced this pull request Oct 30, 2026
tim-smart added a commit that referenced this pull request Nov 3, 2026
effect-bot added a commit that referenced this pull request Nov 3, 2026
effect-bot added a commit that referenced this pull request Nov 3, 2026
effect-bot added a commit that referenced this pull request Nov 3, 2026
effect-bot added a commit that referenced this pull request Nov 3, 2026
effect-bot added a commit that referenced this pull request Nov 3, 2026
effect-bot added a commit that referenced this pull request Nov 3, 2026
effect-bot added a commit that referenced this pull request Nov 3, 2026
effect-bot added a commit that referenced this pull request Nov 3, 2026
effect-bot added a commit that referenced this pull request Nov 3, 2026
effect-bot added a commit that referenced this pull request Nov 6, 2026
effect-bot added a commit that referenced this pull request Nov 6, 2026
effect-bot added a commit that referenced this pull request Nov 6, 2026
effect-bot added a commit that referenced this pull request Nov 6, 2026
effect-bot added a commit that referenced this pull request Nov 6, 2026
effect-bot added a commit that referenced this pull request Nov 7, 2026
effect-bot added a commit that referenced this pull request Nov 7, 2026
effect-bot added a commit that referenced this pull request Nov 7, 2026
effect-bot added a commit that referenced this pull request Nov 8, 2026
tim-smart added a commit that referenced this pull request Nov 16, 2026
effect-bot added a commit that referenced this pull request Nov 16, 2026
effect-bot added a commit that referenced this pull request Nov 16, 2026
effect-bot added a commit that referenced this pull request Nov 16, 2026
effect-bot added a commit that referenced this pull request Nov 16, 2026
effect-bot added a commit that referenced this pull request Nov 16, 2026
effect-bot added a commit that referenced this pull request Nov 16, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 17, 2026
effect-bot added a commit that referenced this pull request Nov 18, 2026
effect-bot added a commit that referenced this pull request Nov 18, 2026
effect-bot added a commit that referenced this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants