OpenTelemetry SDK for Node.js
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
$
$ npm install @opentelemetry/sdk-node
$
$ npm install \
@opentelemetry/exporter-jaeger \
@opentelemetry/exporter-prometheus
@opentelemetry/instrumentation-http
$
$ npm install @opentelemetry/auto-instrumentations-node
Note: this example is for Node.js. See examples/tracer-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({
traceExporter: jaegerExporter,
metricExporter: prometheusExporter,
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start().then(() => {
});
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
metricProcessor
Use a custom processor for metrics. Default: UngroupedProcessor
metricExporter
Configure a metric exporter. If an exporter is not configured, the metrics SDK will not be initialized and registered.
metricInterval
Configure an interval for metrics export in ms. Default: 60,000 (60 seconds)
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.
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.
views
Configure views of your instruments and accepts an array of View-instances. The parameter can be used to configure the explicit bucket sizes of histogram metrics.
serviceName
Configure the service name.
Useful links
License
Apache 2.0 - See LICENSE for more information.