Opentelemetry module for the server framework nitro. Compatible with Nuxt.
npm install nitro-opentelemetrysh
⨠Auto-detect
npx nypm install nitro-opentelemetry
npm
npm install nitro-opentelemetry
yarn
yarn add nitro-opentelemetry
pnpm
pnpm add nitro-opentelemetry
bun
bun install nitro-opentelemetry
deno
deno install npm:nitro-opentelemetry
`
Add the module:
`ts
//https://nitro.unjs.io/config
export default defineNitroConfig({
modules: [
'nitro-opentelemetry'
]
});
`
This module is also compatible with Nuxt
`ts
export default defineNuxtConfig({
modules: [
'nitro-opentelemetry'
]
})
`
Build-time
$3
You can configure the module with the otel property in your nitro.config.ts
- preset:
- Option to configure the preset that will be used with OTEL
$3
nitro-opentelemetry provides a list of presets. Theses presets are files that initialize the SDK for OTEL (for example the NodeSDK).
You can provide your own by setting:
`
{
name: 'custom',
filePath: 'path_to_your_file'
}
`
on the preset option in the otel config.
Runtime
$3
A span is created for each event.
It is attached to the event object with the span property. The context associated to the span is also available within H3Event
``ts
interface H3Event {
otel: {
span: Span
ctx: Context
}
}
``
You can manipulate this span until it ends. The span will be ended in afterResponse (nitro hook) with the endTime set in beforeReponse (nitro hook).
In case of an error, if there is an event associated to the error, the span will record the exception and end. If not, nitro-opentelemetry will create a span from the ROOT_CONTEXT and end it.
$3
Here are the available hooks.
`ts
interface NitroRuntimeHooks {
'otel:span:name': (context: { event: H3Event, name: undefined|string }) => void
'otel:span:end': (context: { event: H3Event, span: Span }) => void
}
`
- otel:span:name
- Called when a span is created.
- otel:span:end
- Called before ending a Span. This can happen in the afterResponse hook or in the error hook.
- otel:recordException:before
- Called before recording and exception within the error hook of nitro. Can be used to not record an exception if you don't want to use a SpanProcessor.
$3
defineTracedEventHandler
- Wrap your event handler with the span assigned to your event. This avoid loosing the context for opentelemetry.
The main renderer and error renderer doesn't need to be wrapped with defineTracedEventHandler
Development
corepack enable
- Install dependencies using pnpm install
- Run interactive tests using pnpm dev`