Skip to content

Telemetry adapters

None of these is a dependency. Each adapter takes the vendor module you already have and describes only the parts it calls, so installing this package never installs a vendor SDK.

telemetry.ts
// telemetry.ts
import * as otel from '@opentelemetry/api';
import {
openTelemetryTracing,
prometheusMetrics,
sentryReporter,
type JobsTelemetry,
type PromClientLike,
} from '@noctcore/jobs';
import * as Sentry from '@sentry/node';
import * as promClient from 'prom-client';
export const telemetry: JobsTelemetry = {
// jobs_job_duration_seconds, jobs_job_failures_total, jobs_sweep_failures_total,
// jobs_sweep_last_success_timestamp_seconds
// In 0.1.0, PromClientLike types `registers` more loosely than prom-client 15
// does, so the module needs a cast to compile. The adapter itself works.
metrics: prometheusMetrics(promClient as unknown as PromClientLike),
tracing: openTelemetryTracing(otel),
errors: sentryReporter(Sentry),
};
export async function flushTelemetry(): Promise<void> {
await Sentry.flush(2_000);
}

Pass it as JobsModule.forRoot({ redisUrl, telemetry }).

prometheusMetrics(promClient, { prefix?, registers?, buckets? }) registers:

Metric Type Labels
jobs_job_duration_seconds histogram queue, job, ok
jobs_job_failures_total counter queue, job, final
jobs_sweep_failures_total counter sweep
jobs_sweep_last_success_timestamp_seconds gauge sweep

The default buckets reach five minutes, since jobs may take minutes. Alert on final failures and on the age of a sweep’s last success; a retried failure that later succeeded is not an incident.

openTelemetryTracing(otel) carries the producer’s trace into the worker: add() injects the W3C trace headers into the job’s context envelope, and the worker extracts them and wraps each attempt in a span. Exceptions are recorded on the span.

sentryReporter(Sentry) calls captureException once per job when it fails for good (never on a retry a later attempt may absorb) and on every failed sweep, with the queue, job name, job id and attempt as tags and extras.

JobsMetrics, JobsTracing and ErrorReporter are small interfaces; every metrics method is optional. Implement them to send the same signals anywhere else.

A worker runs outside the request that enqueued its job, so the request id and similar fields are lost unless they travel with the job. A JobContextHook carries them:

jobs.config.ts
// jobs.config.ts
import { AsyncLocalStorage } from 'node:async_hooks';
import type { JobsModuleOptions } from '@noctcore/jobs';
import { telemetry } from './telemetry';
export const als = new AsyncLocalStorage<{ requestId: string }>();
export function jobsOptions(redisUrl: string): JobsModuleOptions {
return {
redisUrl,
telemetry,
context: {
// On add(): what to carry. Ids only, never secrets; at most 32 string fields.
capture: () => ({ requestId: als.getStore()?.requestId ?? '' }),
// In the worker: re-enter it around handle().
run: (ctx, fn) => als.run({ requestId: ctx['requestId'] ?? '' }, fn),
},
};
}

capture() runs on add(); its fields, with the trace headers, are stored under the reserved _jobs key in the job data. In the worker the envelope is validated, removed before handle sees the data, exposed as job.context, and run() re-enters it around the handler. It holds at most 32 string fields; put ids in it, never secrets. A malformed envelope coming back from Redis is dropped whole.