Contains useful tools and utilities for instrumenting an application with OpenTelemetry.
npm install otel-tools
If you're just getting started with OpenTelemetry, here are some great resources:
- OpenTelemetry API for JavaScript Documentation
- OpenTelemetry Node.JS Getting Started Guide
- otel-node-basics: Example project to learn about how to setup OpenTelemetry with Lightstep
Can be used to annotate synchronous and asynchronous class methods.
A span will be started for the decorated method and ended when the method finishes or resolves.
In case an error occurs, the error info will be attached to the span.
By default the name will be ClassName.methodName
(e.g. NoteService.findAllNotes
), but it can also be specified passing the name
option.
import { TraceMethod, getActiveSpan } from 'otel-tools';
const tracer = trace.getTracer('notes-api');
export class NoteService {
private db: DB;
constructor() {
this.db = new DB();
}
@TraceMethod({ tracer })
async findAllNotes() {
return await this.db.query('select * from notes');
}
@TraceMethod({ tracer, name: 'NoteService.findById' })
async findOneNote(id: number) {
return await this.db.query('select * from notes where id=:id', id);
}
}
A simple wrapper is available in case you need to annotate functions which are not class methods.
import { traceMethod } from 'otel-tools';
const asyncTracedFunction = traceMethod(
{ tracer },
async function asyncFunction(data: Record<string, string>): Promise<any> {
// do something cool
},
);