Socket
Socket
Sign inDemoInstall

@opentelemetry/sdk-node

Package Overview
Dependencies
29
Maintainers
2
Versions
211
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @opentelemetry/sdk-node

OpenTelemetry SDK for Node.js


Version published
Weekly downloads
1.1M
increased by0.79%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

OpenTelemetry SDK for Node.js

NPM Published Version Apache License

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

This package provides the full OpenTelemetry SDK for Node.js including tracing and metrics.

Quick Start

To get started you need to install @opentelemetry/sdk-node, a metrics and/or tracing exporter, and any appropriate instrumentation for the node modules used by your application.

Installation

$ # Install the SDK
$ npm install @opentelemetry/sdk-node

$ # Install exporters and plugins
$ npm install \
    @opentelemetry/exporter-jaeger \ # add tracing exporters as needed
    @opentelemetry/exporter-prometheus \ # add metrics exporters as needed
    @opentelemetry/instrumentation-http # add instrumentations as needed

$ # or install all officially supported core and contrib plugins
$ npm install @opentelemetry/auto-instrumentations-node

Note: this example is for Node.js. See examples/opentelemetry-web for a browser example.

Initialize the SDK

Before any other module in your application is loaded, you must initialize the SDK. If you fail to initialize the SDK or initialize it too late, no-op implementations will be provided to any library which acquires a tracer or meter from the API.

This example uses Jaeger and Prometheus, but exporters exist for other tracing backends.

const opentelemetry = require("@opentelemetry/sdk-node");
const { JaegerExporter } = require("@opentelemetry/exporter-jaeger");
const { PrometheusExporter } = require("@opentelemetry/exporter-prometheus");
const {
  getNodeAutoInstrumentations,
} = require("@opentelemetry/auto-instrumentations-node");

const jaegerExporter = new JaegerExporter();
const prometheusExporter = new PrometheusExporter({ startServer: true });

const sdk = new opentelemetry.NodeSDK({
  // Optional - if omitted, the tracing SDK will not be initialized
  traceExporter: jaegerExporter,
  // Optional - If omitted, the metrics SDK will not be initialized
  metricReader: prometheusExporter,
  // Optional - you can use the metapackage or load each instrumentation individually
  instrumentations: [getNodeAutoInstrumentations()],
  // See the Configuration section below for additional  configuration options
});

// You can optionally detect resources asynchronously from the environment.
// Detected resources are merged with the resources provided in the SDK configuration.
sdk.start().then(() => {
  // Resources have been detected and SDK is started
});

// You can also use the shutdown method to gracefully shut down the SDK before process shutdown
// or on some operating system signal.
const process = require("process");
process.on("SIGTERM", () => {
  sdk
    .shutdown()
    .then(
      () => console.log("SDK shut down successfully"),
      (err) => console.log("Error shutting down SDK", err)
    )
    .finally(() => process.exit(0));
});

Configuration

Below is a full list of configuration options which may be passed into the NodeSDK constructor;

autoDetectResources

Detect resources automatically from the environment using the default resource detectors. Default true.

contextManager

Use a custom context manager. Default: AsyncHooksContextManager

textMapPropagator

Use a custom propagator. Default: CompositePropagator using W3C Trace Context and Baggage

metricReader

Add a MetricReader that will be passed to the MeterProvider. If metricReader is not configured, the metrics SDK will not be initialized and registered.

views

A list of views to be passed to the MeterProvider. Accepts an array of View-instances. This parameter can be used to configure explicit bucket sizes of histogram metrics.

instrumentations

Configure instrumentations. By default none of the instrumentation is enabled, if you want to enable them you can use either metapackage or configure each instrumentation individually.

resource

Configure a resource. Resources may also be detected by using the autoDetectResources method of the SDK.

resourceDetectors

Configure resource detectors. By default, the resource detectors are [envDetector, processDetector]. NOTE: In order to enable the detection, the parameter autoDetectResources has to be true.

sampler

Configure a custom sampler. By default, all traces will be sampled.

spanProcessor

traceExporter

Configure a trace exporter. If an exporter OR span processor is not configured, the tracing SDK will not be initialized and registered. If an exporter is configured, it will be used with a BatchSpanProcessor.

spanLimits

Configure tracing parameters. These are the same trace parameters used to configure a tracer.

serviceName

Configure the service name.

License

Apache 2.0 - See LICENSE for more information.

Keywords

FAQs

Last updated on 16 Sep 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc