@midwayjs/otel
TypeScript icon, indicating that this package has built-in type declarations

3.15.11 • Public • Published

midwayjs open-telemetry module

PRs Welcome

this is a sub package for midway.

Install

$ npm install --save @opentelemetry/api
$ npm install --save @opentelemetry/sdk-node
$ npm install --save @opentelemetry/auto-instrumentations-node

Install component if you want decorator support.

$ npm i @midwayjs/otel@3 --save

Enable open-telemetry

open-telemetry must be loaded at the first of user code.

you can add at bootstrap.js.

const process = require('process');
const { NodeSDK, node, resources } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { Bootstrap } = require('@midwayjs/bootstrap');

// configure the SDK to export telemetry data to the console
// enable all auto-instrumentations from the meta package
const traceExporter = new node.ConsoleSpanExporter();
const sdk = new NodeSDK({
  resource: new resources.Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: 'my-service',
  }),
  traceExporter,
  instrumentations: [getNodeAutoInstrumentations()]
});

// initialize the SDK and register with the OpenTelemetry API
// this enables the API to record telemetry
sdk.start()

// gracefully shut down the SDK on process exit
process.on('SIGTERM', () => {
  sdk.shutdown()
    .then(() => console.log('Tracing terminated'))
    .catch((error) => console.log('Error terminating tracing', error))
    .finally(() => process.exit(0));
});

Bootstrap
  .configure(/**/)
  .run();

You can find more information at opentelemetry-js

Decorator Support

Enable component first.

import { Configuration } from '@midwayjs/core';
import * as koa from '@midwayjs/koa';
import * as otel from '@midwayjs/otel';

@Configuration({
  imports: [
    koa,
    otel
  ]
})
export class MainConfiguration {
}

Otel component add a @Trace decorator for method.

export class UserService {

  @Trace('user.get')
  async getUser() {
    // ...
  }
}

it will create a new span in current trace.

License

MIT

Package Sidebar

Install

npm i @midwayjs/otel

Weekly Downloads

117

Version

3.15.11

License

MIT

Unpacked Size

13 kB

Total Files

12

Last publish

Collaborators

  • mariodu
  • czy88840616
  • legendecas
  • echosoar
  • lellansin
  • lxxyx
  • stone-jin