opentelemetry-js/experimental/packages/opentelemetry-sdk-node
Daniel Dyla 0f8aa7924f
Update `next` branch (#4294)
* chore: track package-lock.json (#4238)

* chore: track package-lock.json

* Pin to old versions for node 14

* Use version range

* Remove unused cached directories

* Temporarily disable other tests

* Temporarily enable only api test

* Enable only some packages

* Test only api packages

* Test trace exporters

* Fix line ordering

* Test all packages except otlp exporters

* Add trace http exporter

* Add trace proto exporter

* Test all but grpc exporters

* chore: use npm workspaces and degrade lerna to v6

* chore: get rid of lerna bootstrap

* chore: use npx

* chore: allow install scripts to setup buf

* chore: fix w3c-integration-test cache key

* chore: fix cache key

* chore: disable resource compat test

* chore: fix node_modules assumptions

* chore: fix hoisted karma issue

* chore: fix markdown linter complaints

* chore: lock @grpc/grpc-js to v1.8.21

* Break caches

* chore: remove cache

* chore: fixup inline commands

---------

Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>

* docs: fixed link to benchmark results (#4233)

Co-authored-by: Chengzhong Wu <legendecas@gmail.com>

* chore(deps): update all patch versions (#4215)

* fix: otlp json encoding (#4220)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* fix: remove duplicate export star from version.ts (#4225)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* docs: fix sdk-node config instructions (#4249)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* feat(api): publish api esnext target (#4231)

* chore: release API 1.7.0/Core 1.18.0/Experimental 0.45.0 (#4254)

* fix(sdk-metrics): hand-roll MetricAdvice type as older API versions do not include it (#4260)

* chore: prepare release 1.18.1/0.45.1 (#4261)

* chore: no need for 'packages' in "lerna.json" (#4264)

* Benchmark tests for trace OTLP transform and BatchSpanProcessor (#4218)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* chore: type reference on zone.js (#4257)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* docs: add docker-compose to run prometheus for the experimental example (#4268)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* fix(sdk-logs): avoid map attribute set when count limit exceeded (#4195)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

* chore(deps): update dependency chromedriver to v119 [security] (#4280)

* chore(deps): update actions/setup-node action to v4 (#4236)

* fix(sdk-trace-base): processor onStart called with a span having empty attributes (#4277)

Co-authored-by: artahmetaj <artahmetaj@yahoo.com>

* Update fetch instrumentation to be runtime agnostic (#4063)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>

---------

Co-authored-by: Chengzhong Wu <legendecas@gmail.com>
Co-authored-by: Martin Kuba <martin@martinkuba.com>
Co-authored-by: Mend Renovate <bot@renovateapp.com>
Co-authored-by: Siim Kallas <siimkallas@gmail.com>
Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
Co-authored-by: David Luna <david.luna@elastic.co>
Co-authored-by: Dinko Osrecki <dinko.osrecki@emarsys.com>
Co-authored-by: Trent Mick <trentm@gmail.com>
Co-authored-by: François <32224751+Lp-Francois@users.noreply.github.com>
Co-authored-by: Hyun Oh <hyunnoh01@gmail.com>
Co-authored-by: André Cruz <andremiguelcruz@msn.com>
Co-authored-by: artahmetaj <artahmetaj@yahoo.com>
Co-authored-by: drewcorlin1 <82601620+drewcorlin1@users.noreply.github.com>
2023-11-15 11:15:51 -05:00
..
src chore(sdk-node): deprecate methods in favor of constructor options (#3996) 2023-08-16 14:38:03 +02:00
test feat(sdk-node): logs support added (#3969) 2023-07-10 18:55:53 +02:00
.eslintignore chore: move sdk-node to experimental (#2473) 2021-09-14 20:41:51 +02:00
.eslintrc.js chore(deps): update dependency eslint to v8.43.0 (#3929) 2023-07-06 15:14:56 +02:00
LICENSE chore: move sdk-node to experimental (#2473) 2021-09-14 20:41:51 +02:00
README.md Update `next` branch (#4294) 2023-11-15 11:15:51 -05:00
package.json Update `next` branch (#4294) 2023-11-15 11:15:51 -05:00
tsconfig.json chore: apply update-ts-configs (#3987) 2023-07-13 12:58:25 +02:00

README.md

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. As shown in the installation instructions, exporters passed to the SDK must be installed alongside @opentelemetry/sdk-node.

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 be initialized from environment variables
  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
});

sdk.start();

// 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 is configured, it will be used with a BatchSpanProcessor. If an exporter OR span processor is not configured programatically, this package will auto setup the default otlp exporter with http/protobuf protocol with a BatchSpanProcessor.

spanLimits

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

serviceName

Configure the service name.

Disable the SDK from the environment

Disable the SDK by setting the OTEL_SDK_DISABLED environment variable to true.

Configure log level from the environment

Set the log level by setting the OTEL_LOG_LEVEL environment variable to enums:

  • NONE,
  • ERROR,
  • WARN,
  • INFO,
  • DEBUG,
  • VERBOSE,
  • ALL.

The default level is INFO.

Configure Trace Exporter from environment

This is an alternative to programmatically configuring an exporter or span processor. This package will auto setup the default otlp exporter with http/protobuf protocol if traceExporter or spanProcessor hasn't been passed into the NodeSDK constructor.

Exporters

Environment variable Description
OTEL_TRACES_EXPORTER List of exporters to be used for tracing, separated by commas. Options include otlp, jaeger, zipkin, and none. Default is otlp. none means no autoconfigured exporter.

OTLP Exporter

Environment variable Description
OTEL_EXPORTER_OTLP_PROTOCOL The transport protocol to use on OTLP trace, metric, and log requests. Options include grpc, http/protobuf, and http/json. Default is http/protobuf.
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL The transport protocol to use on OTLP trace requests. Options include grpc, http/protobuf, and http/json. Default is http/protobuf.
OTEL_EXPORTER_OTLP_METRICS_PROTOCOL The transport protocol to use on OTLP metric requests. Options include grpc, http/protobuf, and http/json. Default is http/protobuf.

Additionally, you can specify other applicable environment variables that apply to each exporter such as the following:

License

Apache 2.0 - See LICENSE for more information.