The Open Framework for Building Agents
fix(eve): flush and shut down the registered meter provider (#2818)
Chad Hietalad1b3439fb7bd
diff --git a/.changeset/otel-meter-lifecycle.md b/.changeset/otel-meter-lifecycle.mdnew file mode 100644index 000000000..2335c1ed5--- /dev/null+++ b/.changeset/otel-meter-lifecycle.md@@ -0,0 +1,5 @@+---+"eve": patch+---++eve now owns the full OpenTelemetry lifecycle it registers: declared metric readers are flushed on `forceFlush` and shut down on `shutdown`, and declared auto-instrumentations are disabled at shutdown, so metrics recorded near teardown are exported instead of silently dropped.diff --git a/packages/eve/scripts/vendor-compiled/declarations/@opentelemetry/api.d.ts b/packages/eve/scripts/vendor-compiled/declarations/@opentelemetry/api.d.tsindex a97befb4a..3f7098171 100644--- a/packages/eve/scripts/vendor-compiled/declarations/@opentelemetry/api.d.ts+++ b/packages/eve/scripts/vendor-compiled/declarations/@opentelemetry/api.d.ts@@ -130,6 +130,12 @@ export interface Meter {export declare const metrics: {getMeter(name: string, version?: string): Meter;+ /**+ * The globally registered meter provider, shared across API copies. The+ * instance comes from whichever metrics SDK registered it, so callers must+ * feature-detect lifecycle methods before invoking them.+ */+ getMeterProvider(): unknown;};export declare enum SpanKind {diff --git a/packages/eve/src/tracing/otel-registration.scenario.test.ts b/packages/eve/src/tracing/otel-registration.scenario.test.tsindex f0b092cff..735be1b45 100644--- a/packages/eve/src/tracing/otel-registration.scenario.test.ts+++ b/packages/eve/src/tracing/otel-registration.scenario.test.ts@@ -11,6 +11,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";import {ROOT_CONTEXT as COMPILED_ROOT_CONTEXT,context as runtimeContext,+ metrics as runtimeMetrics,trace as runtimeTrace,} from "#compiled/@opentelemetry/api/index.js";import { registerOtelPipeline } from "#tracing/otel-registration.js";@@ -20,12 +21,14 @@ const authoredApi = require("@opentelemetry/api") as typeof import("@opentelemetafterEach(() => {authoredApi.context.disable();+ authoredApi.metrics.disable();authoredApi.propagation.disable();authoredApi.trace.disable();context.disable();propagation.disable();trace.disable();(runtimeContext as typeof runtimeContext & { disable(): void }).disable();+ (runtimeMetrics as typeof runtimeMetrics & { disable(): void }).disable();(runtimeTrace as typeof runtimeTrace & { disable(): void }).disable();});@@ -170,6 +173,44 @@ describe("registerOtelPipeline", () => {).toBe(true);});+ it("flushes and shuts down the registered meter provider", async () => {+ const reader = {+ forceFlush: vi.fn(async () => {}),+ setMetricProducer: vi.fn(),+ shutdown: vi.fn(async () => {}),+ };+ const runtime = registerOtelPipeline({+ pipeline: { metricReaders: [reader], spanProcessors: [] },+ serviceName: "weather",+ });++ await runtime.forceFlush();+ expect(reader.forceFlush).toHaveBeenCalledOnce();+ expect(reader.shutdown).not.toHaveBeenCalled();++ await runtime.shutdown();+ expect(reader.shutdown).toHaveBeenCalledOnce();+ });++ it("disables declared instrumentations at shutdown", async () => {+ const instrumentation = {+ disable: vi.fn(),+ enable: vi.fn(),+ getConfig: () => ({ enabled: false }),+ setMeterProvider: vi.fn(),+ setTracerProvider: vi.fn(),+ };+ const runtime = registerOtelPipeline({+ pipeline: { instrumentations: [instrumentation], spanProcessors: [] },+ serviceName: "weather",+ });+ expect(instrumentation.enable).toHaveBeenCalledOnce();+ expect(instrumentation.disable).not.toHaveBeenCalled();++ await runtime.shutdown();+ expect(instrumentation.disable).toHaveBeenCalledOnce();+ });+it("does not export the private registration span", async () => {const exporter = new InMemorySpanExporter();const processor = new SimpleSpanProcessor(exporter);diff --git a/packages/eve/src/tracing/otel-registration.ts b/packages/eve/src/tracing/otel-registration.tsindex 96e22835d..3c8cc811b 100644--- a/packages/eve/src/tracing/otel-registration.ts+++ b/packages/eve/src/tracing/otel-registration.ts@@ -1,6 +1,12 @@import { createRequire } from "node:module";-import { context, propagation, trace, type Context } from "#compiled/@opentelemetry/api/index.js";+import {+ context,+ metrics,+ propagation,+ trace,+ type Context,+} from "#compiled/@opentelemetry/api/index.js";import {registerOTel,type Configuration,@@ -182,15 +188,27 @@ export function registerOtelPipeline(input: {throw new Error("The registered OpenTelemetry tracer provider has no lifecycle methods.");}optionalPeerTracerProxy?.setDelegate(provider);+ // `registerOTel` also registers a global meter provider when metric readers+ // are declared, but returns no handle to it. Capture it now so metrics get+ // the same flush and shutdown coverage as spans; without metric readers the+ // global is a no-op provider with no lifecycle methods.+ const meterProvider = runtimeMeterProvider();return {- forceFlush: () => provider.forceFlush!(),+ forceFlush: async () => {+ await Promise.all([provider.forceFlush!(), meterProvider.forceFlush?.()]);+ },idGenerator,samplesTrace: (traceId) => samplerAdmitsTrace(idGenerator, traceId),- shutdown: () => provider.shutdown!(),+ shutdown: async () => {+ // Stop auto-instrumentations first so nothing records into providers+ // that are about to shut down.+ disableInstrumentations(pipeline.instrumentations);+ await Promise.all([provider.shutdown!(), meterProvider.shutdown?.()]);+ },};}-/** Lifecycle retained from the tracer provider that owns every destination. */+/** Lifecycle retained from the providers that own every destination. */export interface RegisteredOtelPipeline {readonly forceFlush: () => Promise<void>;readonly idGenerator: AgentSpanIdGenerator;@@ -211,6 +229,11 @@ interface RuntimeTracerProvider {shutdown(): Promise<void>;}+interface RuntimeMeterProvider {+ forceFlush(): Promise<void>;+ shutdown(): Promise<void>;+}+interface ProxyTracerProvider {getDelegate(): unknown;}@@ -251,6 +274,33 @@ function runtimeTracerProvider(): Partial<RuntimeTracerProvider> {return (globalProvider.getDelegate?.() ?? globalProvider) as Partial<RuntimeTracerProvider>;}+function runtimeMeterProvider(): Partial<RuntimeMeterProvider> {+ const globalProvider = metrics.getMeterProvider() as {+ forceFlush?: unknown;+ shutdown?: unknown;+ } | null;+ return {+ forceFlush:+ typeof globalProvider?.forceFlush === "function"+ ? () => (globalProvider as RuntimeMeterProvider).forceFlush()+ : undefined,+ shutdown:+ typeof globalProvider?.shutdown === "function"+ ? () => (globalProvider as RuntimeMeterProvider).shutdown()+ : undefined,+ };+}++function disableInstrumentations(instrumentations: readonly unknown[] | undefined): void {+ for (const instrumentation of instrumentations ?? []) {+ const disable = (instrumentation as { disable?: unknown } | null)?.disable;+ if (typeof disable !== "function") continue;+ try {+ disable.call(instrumentation);+ } catch {}+ }+}+function globalTracerUses(idGenerator: AgentSpanIdGenerator): boolean {const spanId = idGenerator.allocateSpanId();const probe = idGenerator.withSpanId(spanId, () =>