Socket
Socket
Sign inDemoInstall

@opentelemetry/sdk-trace-node

Package Overview
Dependencies
11
Maintainers
3
Versions
53
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @opentelemetry/sdk-trace-node

OpenTelemetry Node SDK provides automatic telemetry (tracing, metrics, etc) for Node.js applications


Version published
Weekly downloads
2.2M
increased by0.88%
Maintainers
3
Created
Weekly downloads
 

Package description

What is @opentelemetry/sdk-trace-node?

The @opentelemetry/sdk-trace-node package is part of the OpenTelemetry JavaScript ecosystem and provides a Node.js SDK for tracing. It allows developers to collect and export trace data in their Node.js applications to analyze application performance and troubleshoot issues. The SDK supports various exporters and can be configured to send trace data to different backends for monitoring.

What are @opentelemetry/sdk-trace-node's main functionalities?

Initializing Tracing

This code initializes the Node.js tracing provider and sets up a simple span processor with a console exporter, which will print the trace data to the console.

const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { ConsoleSpanExporter } = require('@opentelemetry/exporter-console');

const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();

Configuring Exporters

This code snippet demonstrates how to configure the NodeTracerProvider to use the Jaeger exporter, which sends trace data to a Jaeger backend for analysis.

const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');

const provider = new NodeTracerProvider();
const jaegerExporter = new JaegerExporter({
  serviceName: 'my-service',
});
provider.addSpanProcessor(new SimpleSpanProcessor(jaegerExporter));
provider.register();

Automatic Instrumentation

This code sets up automatic instrumentation for Node.js applications, which automatically collects trace data from supported libraries without manual instrumentation.

const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');

const provider = new NodeTracerProvider();
provider.register();

require('@opentelemetry/instrumentation').registerInstrumentations({
  instrumentations: [getNodeAutoInstrumentations()],
});

Other packages similar to @opentelemetry/sdk-trace-node

Readme

Source

OpenTelemetry Node SDK

NPM Published Version Apache License

This module provides automated instrumentation and tracing for Node.js applications.

For manual instrumentation see the @opentelemetry/sdk-trace-base package.

How auto instrumentation works

This package exposes a NodeTracerProvider. For loading instrumentations please use registerInstrumentations function from opentelemetry-instrumentation

OpenTelemetry comes with a growing number of instrumentation plugins for well known modules (see supported modules) and an API to create custom instrumentation (see the instrumentation developer guide).

Please note: This module does not bundle any plugins. They need to be installed separately.

This is done by wrapping all tracing-relevant functions.

This instrumentation code will automatically

  • extract a trace-context identifier from inbound requests to allow distributed tracing (if applicable)
  • make sure that this current trace-context is propagated while the transaction traverses an application (see context document in @opentelemetry/api for an in-depth explanation)
  • add this trace-context identifier to outbound requests to allow continuing the distributed trace on the next hop (if applicable)
  • create and end spans

Creating custom spans on top of auto-instrumentation

Additionally to automated instrumentation, NodeTracerProvider exposes the same API as @opentelemetry/sdk-trace-base, allowing creating custom spans if needed.

Installation

npm install --save @opentelemetry/api
npm install --save @opentelemetry/sdk-trace-node

# Install instrumentation plugins
npm install --save @opentelemetry/instrumentation-http
# and for example one additional
npm install --save instrumentation-graphql

Usage

The following code will configure the NodeTracerProvider to instrument http (and any other installed supported modules) using @opentelemetry/plugin-http.

const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');

// Create and configure NodeTracerProvider
const provider = new NodeTracerProvider();

// Initialize the provider
provider.register();

// register and load instrumentation and old plugins - old plugins will be loaded automatically as previously
// but instrumentations needs to be added
registerInstrumentations({
});

// Your application code - http will automatically be instrumented if
// @opentelemetry/plugin-http is present
const http = require('http');

Instrumentation configuration

In the following example:

  • the express instrumentation is enabled
  • the http instrumentation has a custom config for a requestHook
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');

const provider = new NodeTracerProvider();
provider.register();

// register and load instrumentation and old plugins - old plugins will be loaded automatically as previously
// but instrumentations needs to be added
registerInstrumentations({
  instrumentations: [
    new ExpressInstrumentation(),
    new HttpInstrumentation({
        requestHook: (span, request) => {
          span.setAttribute("custom request hook attribute", "request");
        },
    }),
  ],
});


Examples

See how to automatically instrument http and gRPC / grpc-js using node-sdk.

License

Apache 2.0 - See LICENSE for more information.

Keywords

FAQs

Last updated on 24 Apr 2024

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