A helper library standardizing diagnostic_channel
channels for emitting telemetry events, inspired by Erlang/Elixir's Telemetry library.
This package requires Node.js 19.9.0
or later, or ^18.19.0
, as that's
when TracingChannel was introduced.
npm install telemetry_channel
pnpm add telemetry_channel
yarn add telemetry_channel
bun add telemetry_channel
import diagnostics_channel from 'diagnostics_channel'
import { telemetryChannel, type TelemetryEvents } from 'telemetry_channel'
const eventChannel = telemetryChannel('user.newUser')
const logTelemetry = (message: TelemetryEvents['end']) => {
if (message.error) {
logger.error('New user creation failed', { error: message.error })
return
}
logger.info('New user created', { duration: message.duration / 1_000_000 })
}
// Just uses `diagnostic_channel`.tracingChannel under the hood
diagnosticChannel.subscribe('tracing:user.newUser:end', logTelemetry)
const newUser = (attrs: UserAttributes) => {
...
}
// Uses the same APIs as `diagnostic_channel`
const newUser = eventChannel.traceSync((attrs: UserAttributes) => {
...
}, { referrer: 'google' }, null, request.parsedBody)
Under the hood, this is just a wrapper around diagnostics_channel
's TracingChannel
that provides monotonic timing data to the event context. These are the fields
and what they correspond to:
result
: Provided byTracingChannel
, the result of the function if it succeeds. Not available in thestart
orerror
events.error
: Provided byTracingChannel
, the error that occurred if the function fails. Not available in thestart
event.startMonotonicTime
: The result ofprocess.hrtime.bigint()
at the start of the function.endMonotonicTime
: The result ofprocess.hrtime.bigint()
at the end of the function.duration
: The difference betweenendMonotonicTime
andstartMonotonicTime
.
- Add
traceSync
- Add
tracePromise
- Add
traceCallback
- Publish to npm