Setup
@navios/otel adds OpenTelemetry tracing to a Navios application with one plugin, which is the same on every adapter. The plugin:
- starts the OpenTelemetry SDK and shuts it down with the application;
- creates a server span for every HTTP request that matches a route;
- creates child spans for the methods of services marked with
@Traced; - provides the tracer and the meter through dependency injection.
This page covers the plugin, its configuration and the request spans. Tracing services and custom spans covers the rest.
Install
@opentelemetry/api 1.10 is a peer dependency. The OpenTelemetry SDK 3 packages the plugin runs on are installed with it, and the exporters must be of the matching 0.3xx versions. The two OTLP exporters are optional and are loaded only when exporter is 'otlp'.
Add the plugin
app.usePlugin(otelPlugin(options)) before init() does the same. Registering the plugin twice sets it up once.
Configuration
Exporters
With 'otlp', exporterOptions.endpoint is the full URL of the traces endpoint, for example http://localhost:4318/v1/traces, and exporterOptions.headers is sent with every export, for example for authentication. When the exporter package is not installed, init() fails with a message naming it.
To send spans somewhere this package has no exporter for, pass a span processor of your own. The processor classes come from @opentelemetry/sdk-trace:
HTTP request spans
The plugin registers one interceptor, OtelHttpInterceptor from @navios/otel/http, around every route.
Two limits follow from how interceptors work:
- A request that matches no route does not reach an interceptor and gets no span.
- Guards and the handler are not traced separately; the request span covers them together. Use
@Tracedon services for finer spans.
The request span in a handler
The server span is stored on the request context under the RequestSpan key:
Use ctx.find(), not ctx.get(): the key is not set for ignored routes or when autoInstrument.http is false.
Context propagation
For each request the plugin starts the server span and runs the rest of the pipeline inside context.with(...) from @opentelemetry/api, with that span as the active span. Each traced service method does the same with its own span.
context.with() only has an effect when an OpenTelemetry context manager is registered. The context manager keeps the active span across await, timers and callbacks; in Node.js and Bun it is built on AsyncLocalStorage. With registerGlobally: true, the default, the plugin registers the SDK's context manager together with the W3C trace-context and baggage propagators, so this works without further setup:
With registerGlobally: false the plugin still creates its own tracer provider and exports its spans, but registers nothing process-wide. Use it when the process already runs an OpenTelemetry SDK. Unless something else has registered a context manager and a propagator:
trace.getActiveSpan()returnsundefined;- an incoming
traceparentheader is not read; - the span of a
@Tracedmethod has no parent and starts a new trace.
The asyncContext option of the application is a separate AsyncLocalStorage that holds the request context, described in Request context. This package does not need it. When it is on and no context manager is registered, the plugin uses it as a fallback: a @Traced method called during a request becomes a child of the request span, and getCurrentSpan() returns the request span. Spans nested deeper than that are still parented to the request span, not to each other.
Metrics
The plugin records no metrics of its own. metrics: { enabled: true } creates a meter provider, and what you record through MeterToken is exported the same way as spans:
MeterToken is always provided. Without metrics.enabled it is a no-op meter, so code that records metrics does not need to check.
Shutdown
app.close() flushes buffered spans and metrics and shuts the SDK down. Close the application when the process is asked to stop, otherwise the last batch of spans is lost:
Application setup describes shutdown hooks and the order of close().
To export without shutting down, for example at the end of a job, inject OtelSetupService and call forceFlush().