OpenTelemetry Node SDK
This module provides automated instrumentation and tracing for Node.js applications.
For manual instrumentation see the
@opentelemetry/tracing package.
How auto instrumentation works
This package exposes a NodeTracerProvider
.
For loading plugins / instrumentations please use registerInstrumentations
function from opentelemetry-instrumentation
OpenTelemetry comes with a growing number of instrumentation plugins for well know 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 @opentelemetry/context-base 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/tracing, allowing creating custom spans if needed.
Installation
npm install --save @opentelemetry/api
npm install --save @opentelemetry/node
npm install --save @opentelemetry/instrumentation-http
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/node');
const provider = new NodeTracerProvider();
provider.register();
registerInstrumentations({
tracerProvider: provider,
});
const http = require('http');
Instrumentation / Plugin configuration
User supplied plugin configuration is merged with the default plugin
configuration. Furthermore, custom plugins that are configured are implicitly
enabled just as default plugins are.
In the following example:
- the default express plugin is disabled
- the http plugin has a custom config for a
requestHook
- the customPlugin is loaded from the user supplied path
- all default plugins are still loaded if installed.
const { GraphQLInstrumentation } = require('@opentelemetry/instrumentation-graphql');
const provider = new NodeTracerProvider();
registerInstrumentations({
tracerProvider: provider,
instrumentations: [
new GraphQLInstrumentation(),
{
plugins: {
express: {
enabled: false,
},
http: {
requestHook: (span, request) => {
span.setAttribute("custom request hook attribute", "request");
},
},
customPlugin: {
path: "/path/to/custom/module",
},
},
}
],
});
Disable Plugins with Environment Variables
Plugins can be disabled without modifying and redeploying code.
OTEL_NO_PATCH_MODULES
accepts a
comma separated list of module names to disabled specific plugins.
The names should match what you use to require
the module into your application.
For example, OTEL_NO_PATCH_MODULES=pg,https
will disable the postgres plugin and the https plugin. To disable all plugins, set the environment variable to *
.
Examples
See how to automatically instrument http and gRPC / grpc-js using node-sdk.
Useful links
License
Apache 2.0 - See LICENSE for more information.