Make Logfire Temporal spans replay-safe

Merged
DO
2mo ago
+353-49

Every Pydantic AI span is emitted from workflow code under Temporal, so each replay re-emits the whole span tree — 6–13x telemetry inflation on any deployment with replay pressure (Lambda, spot, evicted workers).

LogfirePlugin now makes Logfire's own tracer provider replay-safe: it installs a provider inside Logfire's tracer proxy that shares Logfire's span processor and hands out Temporal's replay-safe tracers (temporalio.contrib.opentelemetry.create_tracer_provider, available at our temporalio>=1.27.0 floor — no version bump). Pydantic AI instrumentation and Temporal's TracingInterceptor both get their tracers through that proxy.

Why both, and why this isn't opt-in

The first attempt moved only Pydantic AI spans onto the replay-safe provider and left TracingInterceptor on the global one. That orphaned 14–15 StartActivity subtrees on a zero-replay run: _ReplaySafeSpan.end() suppresses export during replay, so Pydantic AI spans emitted once while Temporal's activity spans emitted on every iteration, pointing at parents that were never exported.

Putting both tracers on the same provider fixes it. Temporal's own README says the provider should back all spans ("Set Global Tracer Provider"). With that, both existing span trees retain the same structure — no test was moved onto an escape-hatch fixture — so this ships as the default rather than an opt-in.

Behaviour change

Span and trace IDs change for existing Temporal + Logfire users, because TemporalIdGenerator derives them deterministically from the workflow's own RNG. The tree shape is unchanged. Temporal interceptor spans now use their span names as their Logfire messages instead of carrying an explicit logfire.msg attribute because the replay-safe provider is a raw OpenTelemetry SDK provider. A custom setup_logfire callback remains the escape hatch.

Why inside Logfire's proxy, not beside it

An earlier revision built the replay-safe provider beside Logfire's proxy and pointed Pydantic AI and the interceptor at it directly. Review caught, and probes confirmed, two regressions from that:

  • logfire.suppress_scopes() stopped working. Logfire enforces suppression in its proxy's get_tracer(). With suppress_scopes('pydantic-ai'), main exports 0 spans; that revision exported invoke_agent and chat spans with prompt content.
  • Spans vanished after a later logfire.configure(). The side provider kept the span processor Logfire had at connect time; reconfiguring swapped in a new one, and replay-safe spans reached neither exporter.

Inside the proxy, suppression is honored, and any tracer Logfire already handed out — including a host's existing InstrumentationSettings — is re-pointed automatically, so the host's include_content=False and other settings are kept without copying them. A reconfigure no longer loses spans; it drops back to non-replay-safe tracing until the next client, worker or replayer is created, since the plugin reinstalls at each of those hooks. The docs say to configure Logfire first.

Why a small SDKTracerProvider subclass

Logfire's proxy only forwards force_flush(), shutdown(), add_span_processor() and resource when its provider is an opentelemetry.sdk.trace.TracerProvider, and Temporal's ReplaySafeTracerProvider isn't one. Installed directly, logfire.force_flush() returned True but exported nothing from a BatchSpanProcessor — spans lost at a Lambda freeze. ReplaySafeSDKTracerProvider is an SDK provider on the same span processor that only delegates get_tracer() to Temporal's replay-safe provider.

Measured

Replay of a recorded history exported 7 spans before / 9 after on the old wiring (two duplicated Pydantic AI spans); no growth with this change. StartActivity spans were measured at 1 before / 1 after in both cases — they were not separately duplicating, contrary to an early hypothesis.

New regression test test_logfire_plugin_does_not_emit_spans_during_replay replays a recorded history and asserts the exported span count does not grow. There was no equivalent test before.

Note for review

This relies on three private details, each commented in place: OpenTelemetry's _active_span_processor (no public accessor), and Logfire's ProxyTracerProvider.provider / set_provider(). Logfire is ours, so a public hook there would let us drop the last two.

Because the replay-safe provider lives in Logfire's process-wide proxy, a second LogfirePlugin with a custom setup_logfire in the same process does not opt out once a default one has run.

temporalio.contrib.opentelemetry.OpenTelemetryPlugin is still marked experimental as of 1.33.0, so this doesn't move to it.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.