Add configure(fleet=...) to manage the SDK from Logfire Fleet

Merged
ST
1mo ago
+680-32

Requested by Bill · project thread

What

logfire.configure() gets a fleet option. With it, a Logfire Fleet policy governs everything the SDK sends: sampling, which signals are sent, log levels, redaction and volume limits, on every exporter, the console included.

pip install 'logfire[fleet]'
logfire.configure(service_name='checkout', fleet=True)  # or LOGFIRE_FLEET=true
  • fleet=True reads the Fleet server from OTEL_OPAMP_ENDPOINT and OTEL_OPAMP_HEADERS, which the Fleet setup page gives you.
  • fleet=logfire.fleet.FleetOptions(endpoint=..., headers=..., state_dir=..., heartbeat_interval=...) gives the server in code instead.
  • The client is the new fleet extra, logfire-fleet-otel>=0.2.1,<0.3; python_version >= '3.11'. Its native core is about 1 MB per platform wheel, so it stays out of the base install. It is imported only when fleet is set.

How

logfire/_internal/fleet.py finds the client and checks its contract version. _initialize() then calls it where the policy has to act:

  1. Head sampler: the head sampler goes in, and the live sampler comes out for the tracer provider.
  2. Resource: Fleet's instance UID is merged into the resource as service.instance.id.
  3. Export to Logfire:
    • redaction goes under RetryFewerSpansSpanExporter, so a span the retries make is redacted too
    • the gate goes above it, so each span is decided and counted once
    • the log and metric exporters get gates too.
  4. Start: the client gets the built providers. Every other exporter on them gets the gates (the console, additional_span_processors, OTEL_EXPORTER_OTLP_*), and the client joins the fleet. A later configure() is governed under the same client.

None of this blocks or raises. Until Fleet answers, the last saved policy is in force, else the SDK's own settings. In these cases it warns with LogfireFleetWarning and the SDK runs as it does today:

  • the extra isn't installed
  • no endpoint is set
  • the installed client speaks another contract version (LOGFIRE_FLEET_CONTRACT)
  • Emscripten, which has no threads

Smaller changes:

  • fleet_enabled is added to the "Logfire configured" span.
  • FleetOptions survives the executor round trip as a dict, like the other option dataclasses.
  • The logfire-api stubs are regenerated for the changed files only.

The client side is pydantic/opamp-clients#127. Today that package's configure_logfire() rebuilds these exporters from logfire._internal modules. With this PR it no longer has to. pydantic/logfire-js#422 adds the same option to @pydantic/logfire-node.

Before it can merge (why it's a draft)

  • logfire-fleet-otel with the hook (#127) has to be published. Until then uv lock can't resolve the fleet extra, so uv.lock isn't updated in this PR and CI's lock check will fail.
  • Agreement from the SDK maintainers on the option's name and shape.
  • User docs: the Fleet docs PR (#2726) has the applications page this belongs on.

Checks (local, Python 3.13)

  • tests/test_fleet.py (new, with a fake client) covers:
    • where each wrapper sits in the exporter chain
    • the call order
    • LOGFIRE_FLEET
    • off by default
    • each case where it runs unmanaged.
  • test_configure.py and test_logfire_api.py pass. The configuration span snapshot gains fleet_enabled.
  • Full suite: 3329 passed. Four tests didn't pass, none touching this change:
    • Two need Docker (MySQL, Celery) and were deselected.
    • One unraisable-socket warning in test_openai_agents.py passes on its own.
    • The fourth is the stack of two xfails.
  • End to end with the real client from #127: its Logfire suite runs every test both through configure_logfire() and through logfire.configure(fleet=...) against an OpAMP server and a fake ingest. All pass, covering redaction, traces off, log levels, console governance, sampling, credentials file, zstd, reconfigure and exporter placement.
  • ruff and pyright are clean on the changed files.

Generated by Claude Code