0.0.3 • Published 3 years ago

otel-tools v0.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

OpenTelemetry Tools

Contains useful tools and utilities for instrumenting an application with OpenTelemetry.

Getting Started

npm install otel-tools

If you're just getting started with OpenTelemetry, here are some great resources:

API

TraceMethod decorator

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
  },
);