manage(): put an SDK the application set up already under the server (Python, Node.js)

Merged
ST
1mo ago
+711-20

Requested by Bill · project thread

Stacked on #109 (base is its branch): it reuses #109's exporter walk (govern). Retarget to main once #109 merges.

Why

As a newcomer, I wired each SDK to a local OpAMP server that pushed a redact-secrets and warn-level policy, then checked what reached a telemetry sink:

SetupBeforeWith this PR
Python, Logfire SDK, configure_logfire()worksunchanged
Python, opentelemetry-instrument (even with opentelemetry-distro installed)worksunchanged
Node.js, node --import @pydantic/opamp-otel/registerworksunchanged
Python, SDK built in code (contrib's way), then configure()"Overriding of current TracerProvider is not allowed"; APPLIED, but secrets exported unredacted; described as unknown_serviceconfigure() sees the SDK and governs it, with a warning; or call manage()
Node.js, own NodeSDK + register()the app's NodeSDK silently dead (its exporter and resource ignored), last spans lost at exitmanage() after sdk.start() governs the app's own SDK
Logfire JS SDK + our registerduplicate-registration errors; telemetry to the OTLP variables instead of Logfire; warn level ignored for Logfire's log lines (APPLIED anyway); our OpAMP requests tracedmanage() after logfire.configure(): redaction and log level reach Logfire's own exporter and its console

What changes

  • manage() (Python, Node.js): called once, after the application's own setup (its providers, its own NodeSDK, or logfire.configure()). It joins the fleet, describes the instance by the tracer provider's resource, and puts the gates in front of every exporter on the global providers (#109's walk). The sampler and instrumentations stay the application's, so head_sampling, sampling_rules, trace_rate_limit, debug and instrumentations are undeclared: a policy overriding one is FAILED (naming it), and a warning at start lists them. No SDK set up: it warns, and the app runs unmanaged.
  • Python configure() that finds an SDK tracer provider already set as the global one builds none and does what manage() does, with a warning (its other kwargs aren't used).
  • Node.js, Logfire SDK logs: under manage(), when the resource's telemetry.sdk.name is logfire, spans with logfire.span_type: log go through the log controls (send_logs, level by logfire.level_num and scope, the rate cap) and are counted as logs. This is the Node.js version of what Python's #98 did for configure_logfire().
  • Node.js govern: processors a processor holds are now looked through. Logfire JS keeps its BatchSpanProcessor as wrapped and its console (console: true) as a SimpleSpanProcessor under console; both exporters now get the gates (and are listed). Before, neither was gated nor reported.
  • Docs: adds "An SDK set up already: manage()" to python-otel.md and node-otel.md, the Logfire JS SDK to the Logfire guide and getting started, and the './python/otel[logfire]' checkout install line. The package READMEs are updated: the first connection line shows by default, and the key is called a connection key.

Spec and our choices

No protocol change. Every choice here is ours, where the spec leaves it open:

  • manage() reports the sampler controls as undeclared, the same narrowing #109 keeps for "another sampler in place of the SDK's".
  • The OpAMP instance UID isn't forced into the app's resource, so the telemetry keeps the app's own service.instance.id. The docs say so.

Checked

  • Python otel suite: 583 passed. Node.js otel suite: 162 passed. Both ran against core 0.15.0 built locally from opamp#772's branch (the lock in this branch stays at #109's).
  • New tests: Python test_an_sdk_the_application_set_up_itself_is_put_under_the_server[manage|configure], test_manage_refuses_a_policy_that_overrides_the_applications_sampler and test_manage_without_an_sdk_says_how_to_call_it; Node.js test/manage.test.ts (4 tests, one with a Logfire-shaped wrapper holding a batch processor and a console processor).
  • Mutation checks: each change undone fails its test (configure's branch, AGENT.govern, narrow, logSpans, the wrapper walk).
  • End to end, with the real @pydantic/logfire-node 0.21 and logfire 5.4 against opamp-server -scenario and an OTLP sink: redaction (mask and drop) and the warn level applied to Logfire's own exporter; with console: true, the console printed user.email: '[REDACTED]' and no dropped attribute.
  • scripts/public_check.py and redact_mutants.py --check pass.

CI: "Python client (2/3)" fails on a missing testdata/sdk-settings/exporters.json. That comes from #109's base, which needs the core 0.15.0 test vectors locked; it clears when #109 locks the 0.15.0 prerelease.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RQmH2A6nYyXXcGDW1HXzW3