Add configure({ fleet }) to manage the Node.js SDK from Logfire Fleet
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: truereads the Fleet server fromOTEL_OPAMP_ENDPOINTandOTEL_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 oninstanceofacross 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:
headSampler(sampler): the SDK's head sampler goes in, else the oneOTEL_TRACES_SAMPLERnames, 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.resourceAttributes: Fleet's instance UID is merged last into the resource asservice.instance.id.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
- A client release with pydantic/opamp-clients#135, then a higher peer floor here. On 0.2.1,
configure({ fleet })after a finished goodbye stays silently unmanaged. The Python SDK's option in pydantic/logfire#2736 is merged.
Checks (local)
vp test runin logfire-node: 177 passed. That includes the newfleet.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 itconfigure()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
fleetdoesn't say goodbye, and when any of the sampler, teardown, deadline or warning fixes is undone. vp lint,tsc,vp fmtandtest:docspass.- 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: 0stops 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_offdoesn't record under Fleet.- Every exporter has a gate, including with tail sampling and after reconfigure.
@vercel/nfttraces 966 Fleet files and both.wasmfiles for an app usingfleet: { 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