Add configure({ fleet }) to manage the Node.js SDK from Logfire Fleet

Merged
ST
1mo ago
+692-7

Requested by Bill · project thread

What

@pydantic/logfire-node 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.

logfire.configure({ serviceName: 'payments-api', 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: { endpoint, headers, stateDir, heartbeatIntervalMs } gives the server in code instead.
  • fleet: { client } takes the imported client (import * as fleetClient from '@pydantic/logfire-fleet-otel'). Use it where a bundler or a file tracer (Vercel, Next.js standalone) decides what ships: they can't see a package the SDK loads itself, so they may leave it out. A bundler must also keep the package external, because the client reads its WASM core from a path relative to its own files. Inside a bundle it warns and runs unmanaged.
  • The Fleet client is the optional peer dependency @pydantic/logfire-fleet-otel (^0.2.1), so nobody else installs it. Its package carries a WASM core of about 2.4 MB, but a fresh install adds about 50 MB, mostly OpenTelemetry packages. It pins exact OpenTelemetry versions, so an app on newer ones gets a second copy. That doesn't break the hook, which doesn't rely on instanceof across the copies.

How

configure() stays synchronous. When fleet is set without client, src/fleet.ts loads the client with createRequire. The client is ESM, and require() of ESM is fine on the Node versions both packages support. start() then calls the client at three points:

  1. headSampler(sampler): the SDK's head sampler goes in, else the one OTEL_TRACES_SAMPLER names, and the live sampler comes out for the NodeSDK. The SDK resolves that variable itself: NodeSDK reads it only when it's given no sampler, and under Fleet it always gets one.
  2. resourceAttributes: Fleet's instance UID is merged last into the resource as service.instance.id.
  3. start(() => sdk.start(), resource): the client starts the NodeSDK itself. That way it knows which providers the SDK registers, puts its gates on every exporter on them, and joins the fleet.

shutdown() says goodbye to Fleet before the SDK shuts down. A later configure() with fleet keeps the same Fleet client and doesn't say goodbye; one without fleet says goodbye. A process has one client, so if the endpoint, headers, heartbeat or state directory changed, the SDK warns and the client keeps the ones it joined with. A runtime being torn down skips its goodbye when a newer runtime has taken over the client during the flush. A configure({ fleet }) after a finished goodbye needs pydantic/opamp-clients#135 (in the next client release) to join again.

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

  • the package isn't installed (the warning also says to pass fleet: { client } if a bundler or tracer left it out)
  • no endpoint is set
  • the installed package speaks another contract version (LOGFIRE_FLEET_CONTRACT)
  • the client fails

The Fleet side of this is pydantic/opamp-clients#127.

Before it can merge

Checks (local)

  • vp test run in logfire-node: 177 passed. That includes the new fleet.integration.test.ts, which runs against the real NodeSDK with a fake Fleet client:
    • what the client is handed
    • LOGFIRE_FLEET
    • off by default
    • reconfigure, with and without fleet
    • a client passed in
    • OTEL_TRACES_SAMPLER, and a head rate taking precedence over it
    • configure() overlapping a teardown that's still flushing
    • changed options
    • a goodbye that throws, or fails past the shutdown deadline
    • each case where it runs unmanaged.
  • Mutation-checked: the tests fail when the client's sampler isn't used, when the hook doesn't start the SDK (they check the provider registered behind the global proxy), when dropping fleet doesn't say goodbye, and when any of the sampler, teardown, deadline or warning fixes is undone.
  • vp lint, tsc, vp fmt and test:docs pass.
  • End to end with the real Fleet client built from #127 against a small OpAMP server:
    • A redaction policy drops the attribute on the Logfire console.
    • head_sampling: 0 stops all spans.
    • The instance UID is the one Fleet knows.
    • Both the ESM and CJS builds warn cleanly when the package is missing.
  • With the published 0.2.1 client against a stub OpAMP server (it doesn't send policies):
    • OTEL_TRACES_SAMPLER=always_off doesn't record under Fleet.
    • Every exporter has a gate, including with tail sampling and after reconfigure.
    • @vercel/nft traces 966 Fleet files and both .wasm files for an app using fleet: { client }.
    • An esbuild bundle with the package external joins; one that inlines it warns and runs unmanaged.
    • The race cases keep heartbeats going.

Generated by Claude Code