@opentelemetry/instrumentation
Base class for node which OpenTelemetry instrumentation modules extend
Last updated 21 days ago by dyladan .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @opentelemetry/instrumentation 
SYNC missed versions from official npm registry.

OpenTelemetry Instrumentation for web and node

NPM Published Version Apache License

Note: This is an experimental package under active development. New releases may include breaking changes.

Installation

Note: Much of OpenTelemetry JS documentation is written assuming the compiled application is run as CommonJS. For more details on ECMAScript Modules vs CommonJS, refer to esm-support.

npm install --save @opentelemetry/instrumentation

Usage in Node

import {
  InstrumentationBase,
  InstrumentationConfig,
  InstrumentationNodeModuleDefinition,
  InstrumentationNodeModuleFile,
} from '@opentelemetry/instrumentation';

import type * as module_name_to_be_patched from 'module_name_to_be_patched';

export class MyInstrumentation extends InstrumentationBase {
  constructor(config: InstrumentationConfig = {}) {
    super('MyInstrumentation', VERSION, config);
  }

  /**
   * Init method will be called when the plugin is constructed.
   * It returns an `InstrumentationNodeModuleDefinition` which describes
   *   the node module to be instrumented and patched.
   * It may also return a list of `InstrumentationNodeModuleDefinition`s if
   *   the plugin should patch multiple modules or versions.
   */
  protected init() {
    const module = new InstrumentationNodeModuleDefinition(
      'module_name_to_be_patched',
      ['1.*'],
       this._onPatchMain,
       this._onUnPatchMain,
    );
    // in case you need to patch additional files - this is optional
    module.files.push(this._addPatchingMethod());

    return module;
    // you can also define more modules then just return an array of modules
    // return [module1, module2, ....]
  }

  private _onPatchMain(moduleExports: typeof module_name_to_be_patched) {
    this._wrap(
      moduleExports,
      'mainMethodName',
      this._patchMainMethodName()
    );
    return moduleExports;
  }

  private _onUnPatchMain(moduleExports: typeof module_name_to_be_patched) {
    this._unwrap(moduleExports, 'mainMethodName');
  }

  private _addPatchingMethod(): InstrumentationNodeModuleFile {
    const file = new InstrumentationNodeModuleFile(
      'module_name_to_be_patched/src/some_file.js',
      this._onPatchMethodName,
      this._onUnPatchMethodName,
    );
    return file;
  }

  private _onPatchMethodName(moduleExports: typeof module_name_to_be_patched) {
    this._wrap(
      moduleExports,
      'methodName',
      this._patchMethodName()
    );
    return moduleExports;
  }

  private _onUnPatchMethodName(moduleExports: typeof module_name_to_be_patched) {
    this._unwrap(moduleExports, 'methodName');
  }

  private _patchMethodName(): (original) => any {
    const plugin = this;
    return function methodName(original) {
      return function patchMethodName(this: any): PromiseOrValue<module_name_to_be_patched.methodName> {
        console.log('methodName', arguments);
        return original.apply(this, arguments);
      };
    };
  }

  private _patchMainMethodName(): (original) => any {
    const plugin = this;
    return function mainMethodName(original) {
      return function patchMainMethodName(this: any): PromiseOrValue<module_name_to_be_patched.mainMethodName> {
        console.log('mainMethodName', arguments);
        return original.apply(this, arguments);
      };
    };
  }
}

// Later, but before the module to instrument is required

const myInstrumentation = new MyInstrumentation();
myInstrumentation.setTracerProvider(provider); // this is optional, only if global TracerProvider shouldn't be used
myInstrumentation.setMeterProvider(meterProvider); // this is optional
myInstrumentation.enable();
// or use Auto Loader

Usage in Web

import {
  InstrumentationBase,
  InstrumentationConfig,
} from '@opentelemetry/instrumentation';

import { Instrumentation } from '@opentelemetry/instrumentation';

export class MyInstrumentation extends InstrumentationBase {
  constructor(config: InstrumentationConfig = {}) {
    super('MyInstrumentation', VERSION, config);
  }

  private _patchOpen() {
    return (original: OpenFunction): OpenFunction => {
      const plugin = this;
      return function patchOpen(this: XMLHttpRequest, ...args): void {
        console.log('open', arguments);
        return original.apply(this, args);
      };
    };
  }

  public enable() {
    this._wrap(XMLHttpRequest.prototype, 'open', this._patchOpen());
  }
  public disable() {
    this._unwrap(XMLHttpRequest.prototype, 'open');
  }
}

// Later

const myInstrumentation = new MyInstrumentation();
myInstrumentation.setTracerProvider(provider); // this is optional, only if global TracerProvider shouldn't be used
myInstrumentation.setMeterProvider(meterProvider); // this is optional, only if global MeterProvider shouldn't be used
myInstrumentation.enable();
// or use Auto Loader

AutoLoader

NODE - Auto Loader

const { B3Propagator } = require('@opentelemetry/propagator-b3');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');

const tracerProvider = new NodeTracerProvider();

tracerProvider.register({
  propagator: new B3Propagator(),
});

registerInstrumentations({
  instrumentations: [
    new HttpInstrumentation(),
  ],
  //tracerProvider: tracerProvider, // optional, only if global TracerProvider shouldn't be used
  //meterProvider: meterProvider, // optional, only if global MeterProvider shouldn't be used
});

WEB - Auto Loader

const { B3Propagator } = require('@opentelemetry/propagator-b3');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { XMLHttpRequestInstrumentation } = require('@opentelemetry/instrumentation-xml-http-request');
const { WebTracerProvider } = require('@opentelemetry/sdk-trace-web');

const tracerProvider = new WebTracerProvider();

tracerProvider.register({
  propagator: new B3Propagator(),
});

registerInstrumentations({
  instrumentations: [
    new XMLHttpRequestInstrumentation({
      ignoreUrls: [/localhost/],
      propagateTraceHeaderCorsUrls: [
        'http://localhost:8090',
      ],
    }),
  ],
  //tracerProvider: tracerProvider, // optional, only if global TracerProvider shouldn't be used
  //meterProvider: meterProvider, // optional, only if global MeterProvider shouldn't be used
});

Selection of the used TracerProvider/MeterProvider

The registerInstrumentations() API allows to specify which TracerProvider and/or MeterProvider to use by the given options object. If nothing is specified the global registered provider is used. Usually this is what most users want therefore it's recommended to keep this default.

There might be use case where someone has the need for more providers within an application. Please note that special care must be takes in such setups to avoid leaking information from one provider to the other because there are a lot places where e.g. the global ContextManager or Propagator is used.

Instrumentation for ECMAScript Modules (ESM) in Node.js (experimental)

Node.js uses a different module loader for ECMAScript Modules (ESM) vs. CommonJS (CJS). A require() call will cause Node.js to use the CommonJS module loader. An import ... statement or import() call will cause Node.js to use the ECMAScript module loader.

If your application is written in JavaScript as ESM, or it must compile to ESM from TypeScript, then a loader hook is required to properly patch instrumentation. The custom hook for ESM instrumentation is --experimental-loader=@opentelemetry/instrumentation/hook.mjs. This flag must be passed to the node binary, which is often done as a startup command and/or in the NODE_OPTIONS environment variable.

For more details on ECMAScript Modules vs CommonJS, refer to esm-support.

Limitations

Instrumentations for external modules (e.g. express, mongodb,...) hooks the require call or import statement. Therefore following conditions need to be met that this mechanism can work:

  • Instrumentations are registered before the module to instrument is requireed (CJS only)
  • modules are not included in a bundle. Tools like esbuild, webpack, ... usually have some mechanism to exclude specific modules from bundling

License

Apache 2.0 - See LICENSE for more information.

Useful links

Current Tags

  • 0.25.1-alpha.22                                ...           canary (3 years ago)
  • 0.57.0                                ...           latest (7 days ago)
  • 0.34.0                                ...           next (2 years ago)

131 Versions

  • 0.57.0                                ...           7 days ago
  • 0.56.0                                ...           21 days ago
  • 0.55.0                                ...           a month ago
  • 0.54.2                                ...           2 months ago
  • 0.54.1                                ...           2 months ago
  • 0.54.0                                ...           2 months ago
  • 0.53.0                                ...           4 months ago
  • 0.52.1                                ...           6 months ago
  • 0.52.0                                ...           7 months ago
  • 0.51.1                                ...           8 months ago
  • 0.51.0                                ...           8 months ago
  • 0.50.0                                ...           9 months ago
  • 0.49.1                                ...           10 months ago
  • 0.49.0                                ...           10 months ago
  • 0.48.0                                ...           a year ago
  • 0.47.0                                ...           a year ago
  • 0.46.0                                ...           a year ago
  • 0.45.1                                ...           a year ago
  • 0.45.0                                ...           a year ago
  • 0.44.0                                ...           a year ago
  • 0.43.0                                ...           a year ago
  • 0.42.0                                ...           a year ago
  • 0.41.2                                ...           a year ago
  • 0.41.1                                ...           a year ago
  • 0.41.0                                ...           a year ago
  • 0.40.0                                ...           2 years ago
  • 0.39.1                                ...           2 years ago
  • 0.39.0                                ...           2 years ago
  • 0.38.0                                ...           2 years ago
  • 0.37.0                                ...           2 years ago
  • 0.36.1                                ...           2 years ago
  • 0.36.0                                ...           2 years ago
  • 0.35.1                                ...           2 years ago
  • 0.35.0                                ...           2 years ago
  • 0.34.0                                ...           2 years ago
  • 0.33.0                                ...           2 years ago
  • 0.32.0                                ...           2 years ago
  • 0.31.0                                ...           2 years ago
  • 0.30.0                                ...           2 years ago
  • 0.29.2                                ...           3 years ago
  • 0.29.1                                ...           3 years ago
  • 0.29.0                                ...           3 years ago
  • 0.28.0                                ...           3 years ago
  • 0.27.0                                ...           3 years ago
  • 0.26.0                                ...           3 years ago
  • 0.25.1-alpha.22                                ...           3 years ago
  • 0.25.1-alpha.13                                ...           3 years ago
  • 0.25.1-alpha.4                                ...           3 years ago
  • 0.25.0                                ...           3 years ago
  • 0.25.1-alpha.21                                ...           3 years ago
  • 0.24.1-alpha.19                                ...           3 years ago
  • 0.24.1-alpha.6                                ...           3 years ago
  • 0.24.1-alpha.5                                ...           3 years ago
  • 0.24.1-alpha.4                                ...           3 years ago
  • 0.24.1-alpha.1                                ...           3 years ago
  • 0.24.0                                ...           3 years ago
  • 0.24.1-alpha.31                                ...           3 years ago
  • 0.23.1-alpha.28                                ...           3 years ago
  • 0.23.1-alpha.23                                ...           3 years ago
  • 0.23.1-alpha.15                                ...           3 years ago
  • 0.23.1-alpha.12                                ...           3 years ago
  • 0.23.1-alpha.3                                ...           3 years ago
  • 0.23.1-alpha.18                                ...           3 years ago
  • 0.23.0                                ...           3 years ago
  • 0.23.1-alpha.17                                ...           3 years ago
  • 0.22.1-alpha.16                                ...           3 years ago
  • 0.22.1-alpha.15                                ...           3 years ago
  • 0.22.1-alpha.13                                ...           3 years ago
  • 0.22.0                                ...           4 years ago
  • 0.22.1-alpha.5                                ...           4 years ago
  • 0.21.1-alpha.1                                ...           4 years ago
  • 0.21.0                                ...           4 years ago
  • 0.21.1-alpha.7                                ...           4 years ago
  • 0.20.1-alpha.5                                ...           4 years ago
  • 0.20.1-alpha.4                                ...           4 years ago
  • 0.20.1-alpha.3                                ...           4 years ago
  • 0.20.0                                ...           4 years ago
  • 0.20.1-alpha.49                                ...           4 years ago
  • 0.19.1-alpha.43                                ...           4 years ago
  • 0.19.1-alpha.41                                ...           4 years ago
  • 0.19.1-alpha.31                                ...           4 years ago
  • 0.19.1-alpha.11                                ...           4 years ago
  • 0.19.1-alpha.42                                ...           4 years ago
  • 0.19.1-alpha.40                                ...           4 years ago
  • 0.19.1-alpha.39                                ...           4 years ago
  • 0.19.1-alpha.37                                ...           4 years ago
  • 0.19.0                                ...           4 years ago
  • 0.19.1-alpha.36                                ...           4 years ago
  • 0.18.3-alpha.35                                ...           4 years ago
  • 0.18.3-alpha.33                                ...           4 years ago
  • 0.18.3-alpha.25                                ...           4 years ago
  • 0.18.3-alpha.20                                ...           4 years ago
  • 0.18.3-alpha.16                                ...           4 years ago
  • 0.18.3-alpha.14                                ...           4 years ago
  • 0.18.3-alpha.7                                ...           4 years ago
  • 0.18.3-alpha.5                                ...           4 years ago
  • 0.18.3-alpha.4                                ...           4 years ago
  • 0.18.3-alpha.1                                ...           4 years ago
  • 0.18.2                                ...           4 years ago
  • 0.18.3-alpha.2                                ...           4 years ago
  • 0.18.2-alpha.1                                ...           4 years ago
  • 0.18.1                                ...           4 years ago
  • 0.18.2-alpha.81                                ...           4 years ago
  • 0.18.1-alpha.24                                ...           4 years ago
  • 0.18.1-alpha.21                                ...           4 years ago
  • 0.18.1-alpha.20                                ...           4 years ago
  • 0.18.1-alpha.11                                ...           4 years ago
  • 0.18.0                                ...           4 years ago
  • 0.18.1-alpha.12                                ...           4 years ago
  • 0.17.1-alpha.8                                ...           4 years ago
  • 0.17.1-alpha.5                                ...           4 years ago
  • 0.17.1-alpha.21                                ...           4 years ago
  • 0.17.0                                ...           4 years ago
  • 0.16.1-alpha.20                                ...           4 years ago
  • 0.16.1-alpha.15                                ...           4 years ago
  • 0.16.1-alpha.14                                ...           4 years ago
  • 0.16.1-alpha.11                                ...           4 years ago
  • 0.16.1-alpha.6                                ...           4 years ago
  • 0.16.0                                ...           4 years ago
  • 0.15.0                                ...           4 years ago
  • 0.14.0                                ...           4 years ago
  • 0.13.0                                ...           4 years ago
  • 0.12.1-alpha.7                                ...           4 years ago
  • 0.12.1-alpha.3                                ...           4 years ago
  • 0.12.0                                ...           4 years ago
  • 0.12.1-alpha.54                                ...           4 years ago
  • 0.11.1-alpha.53                                ...           4 years ago
  • 0.11.1-alpha.44                                ...           4 years ago
  • 0.11.1-alpha.41                                ...           4 years ago
  • 0.11.1-alpha.36                                ...           4 years ago
  • 0.11.1-alpha.35                                ...           4 years ago
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (6)
Dev Dependencies (28)

© 2010 - cnpmjs.org x YWFE | Home | YWFE