Instrument a Node.js application
For most use cases Grafana Labs recommends Beyla, eBPF network-level auto-instrumentation, which is easy to set up and supports all languages and frameworks.
If you need process-level telemetry for Node.js, follow this documentation to set up the upstream OpenTelemetry SDK for JavaScript for Application Observability.
Install the SDK
Before you begin ensure you have a Node.js development environment with a package manager such as NPM or Yarn, and a Node.js application to instrument.
Run the following command in the project folder:
npm install --save @opentelemetry/api
npm install --save @opentelemetry/auto-instrumentations-node
Instrument your application
Configure and export the following environment variables before starting your application to enable auto-instrumentation:
export OTEL_TRACES_EXPORTER="otlp"
export OTEL_METRICS_EXPORTER="otlp"
export OTEL_LOGS_EXPORTER="otlp"
export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"
# To setup your own Service Instance ID
export OTEL_RESOURCE_ATTRIBUTES="service.instance.id=my-service-instance-id"
export OTEL_NODE_RESOURCE_DETECTORS="env,host,os"
# To have a Service Instance ID generated using a random UUID
# Requires auto-instrumentations-node version 0.46.0+
export OTEL_NODE_RESOURCE_DETECTORS="env,host,os,serviceinstance"
Test your instrumentation
To test if you’ve successfully instrumented your application, run your application, generate some traffic, and you should see metrics and logs outputted to the console.
Next steps
- Create a free Grafana Cloud account.
- For a local development and testing, send data to the Grafana Cloud OTLP endpoint.
- For production, set up an OpenTelemetry Collector.
- Observe your services in Application Observability.