Menu
Grafana Cloud

Instrument a Node application

Follow this article to install the upstream OpenTelemetry distribution for Node and auto-instrument your application for Grafana Cloud Application Observability.

Alternatively, if you need a instrumentation solution for Node supported by Grafana, use Grafana Beyla.

Before you begin

The is the first step to get telemetry data into Application Observability, you need:

  1. A Node development environment with a package manager such as NPM or Yarn.
  2. A Node application to instrument using a recent version of Node.

Install the SDK

Install OpenTelemetry SDK:

sh
npm install --save @opentelemetry/api
npm install --save @opentelemetry/auto-instrumentations-node

Instrument your application

Export the following environment variables before running your application to configure auto-instrumentation:

sh
export OTEL_TRACES_EXPORTER="otlp"
export OTEL_TRACES_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 successfully instrumented your application and are producing telemetry data, run the application. The console output should print references to metrics and logs.

sh
node app.js

Next steps

  1. Create a free Grafana Cloud account
  2. Configure your telemetry data destination:
    1. Grafana Cloud OTLP endpoint: for a quick local development and testing setup
    2. OpenTelemetry Collector: for a robust and scalable production setup
  3. Observe your services in Application Observability

Resources

  1. JavaScript OpenTelemetry documentation
  2. @opentelemetry/api on NPM
  3. opentelemetry-js on GitHub