Instrument an engine with OpenTelemetry
Use this pattern when operators need invocation traces and RED metrics (rate,
errors, duration) in the OpenTelemetry backend they already run. The engine
attaches @invokta/otel to its onEvent hook; the host registers the
OpenTelemetry SDK and exporter. Capabilities, schemas, and adapters do not
change.
This recipe follows the repository’s observability-engine example, whose
provider-backed composition root is instrumented this way.
Build the boundary
Section titled “Build the boundary”-
Install the instrumentation and the OpenTelemetry API
Terminal window npm install @invokta/otel @opentelemetry/apiThe API is a peer dependency, so the engine and the host’s SDK share one instance.
-
Attach the handler at the composition root
import { createEngine, type EngineEvent } from "@invokta/core";import { createOpenTelemetryEventHandler } from "@invokta/otel";export interface ObservabilityEngineOptions {readonly onEvent?: (event: EngineEvent) => void | Promise<void>;}export function createObservabilityEngine(dependencies: ObservabilityDependencies,options: ObservabilityEngineOptions = {},) {return createEngine({name: "observability-engine",version: "0.1.0",capabilities: {"observability.collect-incident-context":createCollectIncidentContext(dependencies),},...(options.onEvent === undefined ? {} : { onEvent: options.onEvent }),});}export function createProviderBackedObservabilityEngine() {// ... construct the Sentry, Datadog, and New Relic connectorsreturn createObservabilityEngine(ports, {onEvent: createOpenTelemetryEventHandler(),});}Without options the handler reports to the global tracer and meter providers. Until a host registers an SDK, the OpenTelemetry API records nothing.
-
Register an SDK in the host
The host owns the SDK, exporter, sampling, and resource. This
instrumentation.mjsprints spans and metrics to the console:import { context, metrics, trace } from "@opentelemetry/api";import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks";import {ConsoleMetricExporter,MeterProvider,PeriodicExportingMetricReader,} from "@opentelemetry/sdk-metrics";import {ConsoleSpanExporter,SimpleSpanProcessor,TracerProvider,} from "@opentelemetry/sdk-trace";const tracerProvider = new TracerProvider({spanProcessors: [new SimpleSpanProcessor({ exporter: new ConsoleSpanExporter() }),],});const meterProvider = new MeterProvider({readers: [new PeriodicExportingMetricReader({exporter: new ConsoleMetricExporter(),}),],});context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable(),);trace.setGlobalTracerProvider(tracerProvider);metrics.setGlobalMeterProvider(meterProvider);// Flush before a short-lived CLI or direct process exits.process.once("beforeExit", () => {void Promise.all([tracerProvider.shutdown(), meterProvider.shutdown()]);});Preload it so the providers exist before the first invocation:
Terminal window node --import ./instrumentation.mjs dist/mcp-http.jsReplace the console exporters with OTLP exporters, or start
@opentelemetry/sdk-node, for a production backend. -
Nest a capability’s own spans when needed
The invocation span is not active inside
run. Inject the handler’scontextForand run the work inside it:import { context, trace } from "@opentelemetry/api";const telemetry = createOpenTelemetryEventHandler();const tracer = trace.getTracer("observability-engine");async run({ input, context: execution }) {return context.with(telemetry.contextFor(execution.requestId), () =>tracer.startActiveSpan("incident.collect", async (span) => {try {return await collect(input, execution.signal);} finally {span.end();}}),);}Pass the same
telemetryvalue toonEventsocontextForsees its pending spans. Keep request IDs unique among concurrent invocations: while two running invocations share one,contextForresolves no span and their spans end without an outcome.
Run it
Section titled “Run it”From the repository root, build the workspace and run the example’s instrumentation tests. They use the OpenTelemetry SDK with in-memory exporters, a local provider stub, and no credentials:
yarn buildyarn vitest run examples/observability-engine/test/opentelemetry-instrumentation.test.tsThe tests export spans named invoke observability.collect-incident-context
with these attributes for a successful CLI-sourced invocation:
{ "invokta.capability.id": "observability.collect-incident-context", "invokta.execution.source": "cli", "invokta.request.id": "incident-1", "invokta.invocation.outcome": "completed"}With the provider credentials from the example README exported, preload your SDK module in front of any entry point:
node --import ./instrumentation.mjs \ examples/observability-engine/dist/cli.js \ run observability.collect-incident-context \ --input '{"service":"checkout-api","from":"2026-07-28T12:00:00.000Z","to":"2026-07-28T13:00:00.000Z"}'Verify it
Section titled “Verify it”The example tests prove successful and failed spans, sanitized error codes, and the provider-backed composition root reporting to a globally registered SDK:
yarn workspace @invokta/example-observability testyarn workspace @invokta/example-observability typecheckyarn workspace @invokta/example-observability buildThe package tests cover span timing and parentage, contextFor nesting,
eviction, reused request IDs, bounded attributes, and contained
instrumentation failures:
yarn vitest run packages/otelRead the @invokta/otel reference for every attribute,
metric, and limit, and the complete
observability-engine
example for the instrumented composition root and its tests.