Skip to main content

Getting Started with OpenTelemetry

Navios provides first-class OpenTelemetry integration through three packages:

  • @navios/otel - Core abstractions and shared functionality
  • @navios/otel-fastify - Fastify adapter integration
  • @navios/otel-bun - Bun adapter integration

Installation

For Fastify

# Core packages
yarn add @navios/otel @navios/otel-fastify @opentelemetry/api

# For OTLP export (Jaeger, Tempo, Grafana, etc.)
yarn add @opentelemetry/exporter-trace-otlp-http

# Optional: For metrics export
yarn add @opentelemetry/exporter-metrics-otlp-http

For Bun

# Core packages
yarn add @navios/otel @navios/otel-bun @opentelemetry/api

# For OTLP export
yarn add @opentelemetry/exporter-trace-otlp-http

Basic Setup

Fastify Adapter

import { NaviosFactory } from '@navios/core'
import { FastifyApplicationService } from '@navios/adapter-fastify'
import { defineOtelPlugin } from '@navios/otel-fastify'

const app = await NaviosFactory.create(FastifyApplicationService, {
module: AppModule,
})

app.usePlugin(defineOtelPlugin({
serviceName: 'my-api',
exporter: 'otlp',
exporterOptions: {
endpoint: 'http://localhost:4318/v1/traces',
},
autoInstrument: {
http: true,
handlers: true,
},
}))

await app.listen({ port: 3000 })

Bun Adapter

import { NaviosFactory } from '@navios/core'
import { BunApplicationService } from '@navios/adapter-bun'
import { defineOtelPlugin } from '@navios/otel-bun'

const app = await NaviosFactory.create(BunApplicationService, {
module: AppModule,
})

// defineOtelPlugin returns an array of staged plugins
for (const plugin of defineOtelPlugin({
serviceName: 'my-bun-api',
exporter: 'console', // For development
autoInstrument: {
http: true, // Enable HTTP tracing (default)
handlers: true, // Trace guard execution
},
ignoreRoutes: ['/health', '/metrics'], // Optional: skip tracing these
})) {
app.usePlugin(plugin)
}

await app.listen({ port: 3000 })
note

The Bun adapter uses staged plugins to register the traced controller adapter before the application initializes. This enables automatic HTTP request tracing with full controller and handler metadata.

Performance Considerations

Tracing adds overhead to every request. The amount depends on your configuration:

ConfigurationOverheadUse Case
HTTP spans only~0.1-0.5msProduction with basic observability
HTTP + handlers~0.5-1msProduction with detailed insights
Full instrumentation~1-3msDevelopment/debugging
With sampling (10%)MinimalHigh-traffic production

See Configuration Presets for ready-to-use configurations.

Configuration Options

OptionTypeDefaultDescription
serviceNamestringrequiredName of your service
serviceVersionstring-Version of your service
environmentstring-Deployment environment
exporter'otlp' | 'console' | 'none'requiredWhere to send traces
exporterOptions.endpointstring-OTLP endpoint URL
exporterOptions.headersRecord<string, string>-Headers for authentication
autoInstrument.httpbooleantrueTrace HTTP requests
autoInstrument.handlersbooleantrueTrace handler methods
autoInstrument.guardsbooleanfalseTrace guard execution
metrics.enabledbooleanfalseEnable metrics collection
includeNaviosAttributesbooleanfalseAdd navios.* attributes
sampling.rationumber1.0Sample ratio (0-1)
ignoreRoutesstring[][]Routes to skip tracing (supports * wildcards)

Next Steps