Skip to content

fix(diagnostics-otel): own the tracer and meter providers instead of the global ones - #237

Merged
insomnius merged 3 commits into
boonfrom
fix/otel-owned-tracer-provider
Sep 22, 2026
Merged

insomnius merged 3 commits into
boonfrom
fix/otel-owned-tracer-provider

Conversation

@insomnius

@insomnius insomnius commented Sep 22, 2026 •

Copy link
Copy Markdown

What Problem This Solves

diagnostics-otel exports nothing on a gateway that also runs another OpenTelemetry SDK, and it fails silently.

The plugin read its tracer off the global API (trace.getTracer("openclaw")). Global provider registration is first-writer-wins: whoever registers first owns it, and a later NodeSDK.start() cannot replace it. On our fleet sentry-monitor calls Sentry.init(), and @sentry/node sets up its own OpenTelemetry layer, so the plugin's spans were created by Sentry's tracer and dropped at its tracesSampleRate: 0. Nothing was logged on either side — the plugin reported healthy, plugins inspect showed the bundled copy loaded, and Tempo stayed empty.

Measured on the Boon fleet after the exporter itself was repaired (all 65 running hosts on boon.45, bundled origin, no capability warning):

  • Real turn traces reached Tempo from exactly one customer — the single host whose sentry-monitor is inactive for lack of a DSN.
  • A 130-second packet capture on a busy host recorded 6 turns processed and zero POST /v1/traces. The only OTLP traffic was POST /v1/metrics → 404.
  • The same host's Alloy accepted a synthetic span posted by hand, and its otelcol.* components were healthy, so transport, credentials and the collector were never the problem.

The stray /v1/metrics traffic is the second half of the same root cause: with diagnostics.otel.metrics: false the plugin passed no metricReader, and NodeSDK then built one from OTEL_METRICS_EXPORTER defaults and shipped metrics to localhost:4318/v1/metrics on a traces-only collector.

Why This Change Was Made

The plugin now owns its providers rather than borrowing the process's:

  • BasicTracerProvider and MeterProvider are constructed with the plugin's resource, sampler and processors, and the tracer and meter come from them. Neither is registered globally, so the plugin and any other SDK coexist instead of overwriting each other.
  • NodeSDK is gone, which removes its env-driven fallbacks — no metric reader is created when metrics are disabled.
  • Resource detectors (env, host, os, process) are wired explicitly, because NodeSDK used to supply them and spans need host.name and process.* to tell two gateways apart.
  • OPENCLAW_OTEL_PRELOADED=1 still reads the tracer and meter off the global API. That flag exists precisely to say a host process owns the SDK on purpose.

User Impact

Traces start flowing on every gateway that runs both the Sentry plugin and the OTLP exporter, which on our fleet is 64 of 65 hosts. No configuration changes: same diagnostics.otel keys, same endpoints, same sampling. Spans gain nothing and lose nothing in shape; the resource attributes that NodeSDK used to attach are preserved. Collectors that only accept traces stop receiving unrequested metric posts.

Evidence

Live reproduction against the real service (unmocked OTel, fleet config: traces: true, metrics: false, sampleRate: 0.5), with a local OTLP receiver and three synthetic turn events:

Scenario Before After
Plugin alone POST /v1/traces 1225B POST /v1/traces 1405B
Another SDK registers the global provider first NONE POST /v1/traces 1511B

The larger payload after the change is the explicitly detected host/process resource attributes.

Tests: extensions/diagnostics-otel/src/service.test.ts, 98 passed. Two are new and encode the defect — one asserts the plugin builds its own provider and never calls the global getTracer, the other asserts it does use the global one under OPENCLAW_OTEL_PRELOADED=1.

node scripts/run-vitest.mjs extensions/diagnostics-otel/src/service.test.ts   # 98 passed
node scripts/build-all.mjs                                                    # clean
node_modules/.bin/oxfmt <changed files>                                       # clean
node scripts/run-oxlint.mjs extensions/diagnostics-otel                       # clean

tsgo --noEmit reports two errors in src/config/io.ts and src/secrets/config-io.ts. Both files are untouched by this PR and unmodified in the working tree, so they are pre-existing on boon (f0e3a1f).

@opentelemetry/sdk-node is removed from the root and plugin manifests in the second commit. The extension-runtime-dependencies contract fails on a declared dependency no runtime file mentions, and the package was carrying 173 transitive entries in npm-shrinkwrap.json — the gRPC, Zipkin and Prometheus exporters among them — none of which this plugin ever used. pnpm-lock.yaml and npm-shrinkwrap.json are regenerated; the plugin contract lane passes (43 files, 976 tests).

…the global ones

A gateway that also loads an SDK which registers a global OpenTelemetry
tracer provider silences this plugin entirely. Global registration is
first-writer-wins, so when @sentry/node gets there first — sentry-monitor
calls Sentry.init(), which sets up its own OpenTelemetry layer — the
plugin's trace.getTracer("openclaw") returns Sentry's tracer, every span
is dropped at its tracesSampleRate of 0, and nothing is ever exported. No
error is logged on either side.

The plugin now builds its own BasicTracerProvider and MeterProvider and
reads the tracer/meter off them. It still falls back to the global API
under OPENCLAW_OTEL_PRELOADED=1, where a host process deliberately owns
the SDK.

Dropping NodeSDK also removes its env-driven fallbacks: with metrics
disabled the plugin no longer creates a metric reader from
OTEL_METRICS_EXPORTER defaults, which had it POSTing to
localhost:4318/v1/metrics on a traces-only collector. Resource detectors
(env/host/os/process) are now wired explicitly so spans keep host.name
and process.* attributes NodeSDK used to add.
@insomnius
insomnius requested a review from a team September 22, 2026 14:46
@claude

claude Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Claude encountered an error —— View job


I'll analyze this and get back to you.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

…dependency

The plugin builds its own tracer and meter providers, so NodeSDK is gone
from the source. The extension-runtime-dependencies contract fails on a
declared dependency no runtime file mentions, and it is right to: the
package pulled 173 transitive entries into npm-shrinkwrap.json, including
the gRPC, Zipkin and Prometheus exporters this plugin never used.
@github-actions

Copy link
Copy Markdown

Dependency Guard

This PR changes dependency-related files. Maintainers should confirm these changes are intentional.

Changed files:

  • extensions/diagnostics-otel/package.json
  • npm-shrinkwrap.json
  • package.json
  • pnpm-lock.yaml

Maintainer follow-up:

  • Review whether the dependency changes are intentional.
  • Inspect resolved package deltas when lockfile, shrinkwrap, or workspace dependency policy changes are present.
  • Treat package-lock.json and npm-shrinkwrap.json diffs as security-review surfaces.
  • Run pnpm deps:changes:report -- --base-ref origin/main --markdown /tmp/dependency-changes.md --json /tmp/dependency-changes.json locally for detailed release-style evidence.

@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Dependency graph changes noted

This PR includes dependency graph changes. The dependency guard is informational because the PR author is a repository admin or a member of @openclaw/openclaw-secops.

  • Current SHA: e8b4fb7e27e03f75372bbf412e1fba4edc76fa91
  • Trusted actor: @insomnius
  • Trusted role: pull request author; repository admin

Security review is still recommended before merge when the dependency graph change is intentional.

…detected resource

Review follow-ups on #237:

- The meter still fell back to the global MeterProvider whenever metrics
  were disabled, and the event handlers record instruments unconditionally,
  so a co-running SDK's provider would have collected them. The plugin now
  always builds its own MeterProvider (readerless when metrics are off)
  unless OPENCLAW_OTEL_PRELOADED says a host process owns the SDK.
- The resources module was mocked to empty objects, so nothing tested the
  guarantee that host.name and process.* still reach spans now that NodeSDK
  no longer runs the detectors. The mocks return detector attributes and a
  new test asserts the provider gets them alongside service.name.
- Trimmed the tracer-ownership comment to the invariant.

@augusteo augusteo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the complete three-dot range f0e3a1f..e8b4fb7. The provider ownership fix is correct: the plugin owns tracer and meter providers, preserves the preloaded global-provider exception, and restores the NodeSDK resource detectors needed for host/process identity. The sdk-node removal and regenerated lockfiles also pass the dependency and plugin-contract checks.

Exact-head verification is green: CI and OpenGrep passed, with build, lint, test types, shrinkwrap, dependency, security, and plugin-contract checks successful. Approving this head. This approval does not merge, release boon.46, or roll the fleet.

@insomnius
insomnius merged commit c7b9754 into boon Sep 22, 2026
138 of 141 checks passed
@insomnius
insomnius deleted the fix/otel-owned-tracer-provider branch September 22, 2026 15:42
insomnius added a commit that referenced this pull request Sep 22, 2026
…238)

Root version, 135 plugin manifests synced via scripts/sync-plugin-versions.ts, changed npm-shrinkwrap.json files regenerated, CHANGELOG section for boon.46.

Shipping: #237 (diagnostics-otel owns its tracer and meter providers instead of the global ones, so it stops handing spans to a co-running SDK), #235 (model.usage stays inside the turn trace), #233 (a plugin's scheduled turn is no longer rolled back by an unrelated registry becoming active), #229 (browser-handoff recheck scheduling failure is logged), #212 (dev-only vitest bump in ui).

Base = 2026.6.11-boon.45. Gateway plus @openclaw/slack, @openclaw/msteams and @openclaw/diagnostics-prometheus bumped in lockstep; no separately published plugin source changed in this train, so a gateway roll delivers all of it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants