@setu-ts/telemetry-plugin — trace requests and application work
Add it: setu add telemetry adds it and prints the registration for setu.config.ts.
TelemetryPlugin provides CAPABILITIES.TELEMETRY (ITelemetryService); without an exporter it
registers a no-op service.
Export sampled request traces
Put this plugin in the config’s plugins array:
import { TelemetryPlugin } from '@setu-ts/telemetry-plugin';
export const telemetry = TelemetryPlugin({
serviceName: 'orders-api',
exporter: 'otlp',
endpoint: 'http://localhost:4318/v1/traces',
spanProcessor: 'batch',
sampling: { type: 'traceidratio', ratio: 0.1 },
});
Add a span inside a module route
import { CAPABILITIES } from '@setu-ts/common';
import type { IRouterApi, ITelemetryService } from '@setu-ts/common';
export function registerOrderRoutes(router: IRouterApi): void {
router.post('/orders', {
handler: async (ctx) => {
const telemetry = ctx.services.get<ITelemetryService>(CAPABILITIES.TELEMETRY);
return await telemetry.withSpan('create-order', async (span) => {
span.setAttribute('order.id', 'o-1');
span.setStatus('ok');
return ctx.response.status(201).json({ id: 'o-1' });
});
},
});
}
Behaviour the types do not show
- No exporter means no SDK load; callbacks still run and return their result.
withSpanends the span in a finally path; a thrown error is rethrown after marking failure.serviceNameis required with an exporter; OTLP also needsendpoint.serviceVersiondefaults to'1.0.0';spanProcessordefaults to'simple'.- Request middleware runs at priority 30 and propagates W3C traceparent; query strings are omitted from the recorded URL by default.
Other options
exporter: 'console'writes spans locally;middleware: falsedisables request spans.instrumentationsenableshttp,fetch,ioredis,amqpliborkafkajson Node only.parentContextorroot: truecontrols span ancestry;contextPropagation: falsedisables activation.diagnosticsretains minimized observations only for explicitly approved operations.