Skip to content

fix(telemetry): bound OTLP trace export with a configurable timeout - #377

Merged
reyortiz3 merged 2 commits into
mainfrom
fix/otlp-trace-export-timeout
Oct 14, 2026
Merged

reyortiz3 merged 2 commits into
mainfrom
fix/otlp-trace-export-timeout

Conversation

@reyortiz3

Copy link
Copy Markdown
Contributor

What

The OTLP trace exporters were built with no timeout, so the SDK's 10s default applied, while the batch span processor kept its own 30s export budget.

Under OTLP/HTTP — this package's default transport — the exporter's timeout is the HTTP client's per-request deadline, and a request that trips it is classified retryable and sent again; only the processor's budget bounds that retry. A destination that accepts a batch but answers slower than 10s is therefore handed the same batch twice, and because the retry succeeds nothing is logged on this side. A receiver that counts what it receives then counts those spans twice.

That is not hypothetical here: a custom base path on Endpoint is how a caller targets a third-party ingest route, which is exactly the kind of destination that both counts spans and answers slowly.

How

Config gains an optional ExportTimeout, defaulting to 30s when unset or non-positive, applied to the HTTP exporter, the gRPC exporter, and the batch processor together.

Giving the processor the same budget is what closes the window: its own default is 30s, so a larger ExportTimeout would otherwise be silently truncated, and it is also what bounds the exporter's retry.

The field is additive and the default is applied for a zero value, so existing callers keep working without change.

Scope: traces only

ExportTimeout is wired to the trace path only, and its doc comment says so. Metrics and logs have the same asymmetry and are deliberately left for a follow-up:

  • otlpmetrichttp DefaultTimeout is 10s, while metrics.go builds sdkmetric.NewPeriodicReader(exporter) with no options — the reader's own default timeout is 30s. That is the identical exporter-vs-processor gap this PR fixes for traces.
  • logging.go builds its exporters with no timeout for the same reason.

Widening the field to all three signals is a larger change to a shared Config, and worth doing on its own. Happy to fold it in here instead if reviewers prefer.

Testing

  • HTTP: a boundary pair against one delayed ingest where the only difference between runs is ExportTimeout, so a flipped outcome can only be attributed to it. The permissive run also proves the endpoint is sound, so the strict run's failure is not a dial or handshake error in disguise.
  • gRPC: asserts the export abandons against a silent peer within the configured budget rather than the SDK default.
  • Batcher: asserts ForceFlush against an unresponsive ingest honors the configured budget rather than the processor's own 30s.
  • Table-driven coverage of the zero / negative / explicit cases for the default fallback.

task (lint + go vet + go test -race) and task license-check both pass. Package coverage is 83.3%.

🤖 Generated with Claude Code

The trace exporters were built with no timeout, so the SDK's 10s default
applied, while the span processor kept its own 30s export budget. Under
OTLP/HTTP -- this package's default transport -- the exporter's timeout is
the HTTP client's per-request deadline, and a request that trips it is
classified retryable and sent again; only the processor's budget bounds
that retry. A destination that accepts a batch but answers slower than 10s
is therefore handed the same batch twice, and because the retry succeeds
nothing is logged on this side. A receiver that counts what it receives
then counts those spans twice.

That is not hypothetical here: a custom base path on Endpoint is how a
caller targets a third-party ingest route, which is exactly the kind of
destination that both counts spans and answers slowly.

Config gains an optional ExportTimeout, defaulting to 30s when unset, and
it is applied to the HTTP exporter, the gRPC exporter and the batch
processor together. Giving the processor the same budget is what closes
the window: its own default is 30s, so a larger ExportTimeout would
otherwise be silently truncated, and it is also what bounds the retry.

The field is additive and the default is applied for a zero value, so
existing callers keep working without change.

Co-Authored-By: Claude Opus 5 <[email protected]>
aponcedeleonch
aponcedeleonch previously approved these changes Oct 14, 2026

@aponcedeleonch aponcedeleonch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. This closes the duplicate-delivery window for every consumer of the composite provider, sprout included.

My only comment is that there's no way to set the value through providers. ExportTimeout is added to otlp.Config, but there's no providers.WithExportTimeout option, and providers_strategy.go builds the config without it. So callers of NewCompositeProvider always get 30s.

On top of that, passing WithTimeout and WithExportTimeout explicitly overrides OTEL_EXPORTER_OTLP_TRACES_TIMEOUT and OTEL_BSP_EXPORT_TIMEOUT, since the SDK applies env first and options after. Today a caller like sprout can tune the timeout through env vars. After this it can't tune it at all. That's fine for an in-cluster collector, but it won't be once a composite-provider caller points at a third-party ingest. A WithExportTimeout provider option wired through the strategy would keep that door open.

ExportTimeout landed on otlp.Config but nothing built that config with it:
providers_strategy.go constructs the traces config field by field, so every
NewCompositeProvider caller got exactly DefaultExportTimeout with no way to
ask for anything else.

That gap is sharper than a missing setter. Passing WithTimeout and
WithExportTimeout explicitly overrides OTEL_EXPORTER_OTLP_TRACES_TIMEOUT and
OTEL_BSP_EXPORT_TIMEOUT, because the SDK applies env config first and options
after -- NewHTTPConfig calls ApplyHTTPEnvConfigs before the option loop, and
NewBatchSpanProcessor reads the env var only to seed the default it then lets
options overwrite. So a composite-provider caller that tuned the export
deadline through env vars lost that lever and gained nothing in its place.

Config gains ExportTimeout, WithExportTimeout sets it, and the traces
strategy passes it through to otlp.Config. The option is a plain setter: a
non-positive value stays verbatim and otlp.Config normalizes it, so the
provider layer never hides what the caller asked for.

The strategy test drives a built provider against an ingest that never
answers and asserts the flush gives up on the configured budget. A field
assertion would pass with the wiring removed; this one takes 10s instead of
150ms.

Co-Authored-By: Claude Opus 5 <[email protected]>
@reyortiz3

Copy link
Copy Markdown
Contributor Author

Good catch on both counts — I verified them before acting, and they hold:

  • providers.go had 15 With* options and none for this; providers_strategy.go built the traces otlp.Config field by field without it, so every NewCompositeProvider caller got exactly 30s.
  • The env precedence is as you describe. NewHTTPConfig calls ApplyHTTPEnvConfigs(cfg) then loops the options, and NewBatchSpanProcessor (batch_span_processor.go:101) reads env.BatchSpanProcessorExportTimeout only to seed the default it then lets options overwrite at :105. Explicit wins over env in both.

Fixed in 1b38b55: Config.ExportTimeout, a WithExportTimeout option, and pass-through in the traces strategy. The option is a plain setter — a non-positive value is stored verbatim and otlp.Config normalizes it, so the provider layer doesn't hide what the caller asked for.

The strategy test drives a built provider against an ingest that never answers and asserts the flush gives up on the configured budget, rather than asserting the struct field. I checked it fails for the right reason: with the one-line wiring removed it takes 10s instead of 150ms.

One thing worth naming that your comment implies but doesn't quite say: the env override also applies to direct otlp.Config users who leave the field unset. Zero resolves to 30s and we pass it explicitly, so "unset" now means "hard 30s" rather than "SDK/env decides". Anyone currently tuning via OTEL_EXPORTER_OTLP_TRACES_TIMEOUT gets silently overridden whether or not they go through providers.

That's inherent to overriding a bad SDK default — you can't beat the 10s default without also beating env. If we want to keep the env lever for unset callers, the fix is to read those two vars when ExportTimeout is zero and only fall back to 30s if neither is set. That pulls env-reading into this package (the repo has the env.Reader pattern for it), so I left it out here. Happy to do it in a follow-up if you think the lever is worth keeping — the doc comments currently state the precedence explicitly so it's at least not a surprise.

task and task license-check pass. providers coverage 62.6% → 63.1%, otlp 83.3%.

@aponcedeleonch aponcedeleonch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks for wiring WithExportTimeout through the strategy. Driving a real provider against an unresponsive ingest in the test is a nice touch.

One thing to keep in mind for a follow-up. Since the trace path now always passes an explicit timeout, sprout loses the OTel env vars (OTEL_EXPORTER_OTLP_TRACES_TIMEOUT, OTEL_EXPORTER_OTLP_TIMEOUT, OTEL_BSP_EXPORT_TIMEOUT) as soon as it bumps toolhive-core. Its telemetry.Config has no timeout field today, so it'll be stuck on a hard 30s.

There are two ways to reopen that:

  1. Sprout follow-up: add an ExportTimeout field to sprout's telemetry.Config, populate it from its own config in cmd/sprout/main.go, and pass providers.WithExportTimeout.
  2. toolhive-core follow-up: the one you suggested. When ExportTimeout is zero, read the env vars and only fall back to 30s if none are set. That keeps the env lever for every composite-provider caller without code changes, and an explicit option still wins.

I'll leave it up to you which one to pick. Neither blocks this PR.

@reyortiz3
reyortiz3 merged commit 0850276 into main Oct 14, 2026
6 checks passed
@reyortiz3
reyortiz3 deleted the fix/otlp-trace-export-timeout branch October 14, 2026 17:11
aponcedeleonch pushed a commit that referenced this pull request Oct 15, 2026
…nv vars (#379)

Follow-up to #377, which bounded trace export and deferred two things.

The first is that metrics and logs carry the same asymmetry traces had.
otlpmetrichttp and otlploghttp default to a 10s per-request deadline while
NewPeriodicReader and NewBatchProcessor default to a 30s export budget, so
a destination that accepts a batch and answers slower than 10s is handed it
again inside the reader's or processor's remaining window. Both now take the
resolved budget alongside their exporter, the way the batch span processor
already did.

The second is that #377 made unset mean 30s rather than "let the SDK decide",
which silently disabled OTEL_EXPORTER_OTLP_TRACES_TIMEOUT and friends: the SDK
applies env config first and explicit options after, so passing an option at
all takes the variables out of play. A caller that tuned export deadlines
through the environment lost that lever on upgrade and gained nothing, since
its own config had nowhere to put a timeout. ExportTimeout now resolves
through the variables before falling back to the default, so the lever works
again without code changes and an explicit value still wins.

Both variables for a signal feed one budget rather than their own components.
That looks lossy and is the point: honoring each on its own component would
reinstate the split this field exists to close -- a processor variable set
alone would leave the exporter on 10s with room to re-send, and an exporter
variable set alone would be truncated by the processor's 30s.

Each signal's budget reaches both halves, proven by driving a built provider
against an ingest that never answers and asserting the flush gives up on the
configured value. Removing any one of the four wirings turns those tests from
~150ms into the 10s ceiling, so none of them pass vacuously.

Also tidies two things left in #377's tests: the ingest server now releases
its handler on teardown instead of parking a goroutine until the binary
exits, and the silent gRPC peer closes its connections from a single cleanup
rather than calling t.Cleanup from its accept loop.

Co-authored-by: Claude Opus 5 <[email protected]>
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.

2 participants