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:
- A Node development environment with a package manager such as NPM or Yarn.
- A Node application to instrument using a recent version of Node.
Install the SDK
Install OpenTelemetry SDK:
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:
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 successfully instrumented your application and are producing telemetry data, run the application. The console output should print references to metrics and logs.
node app.js
Next steps
- Create a free Grafana Cloud account
- Configure your telemetry data destination:
- Grafana Cloud OTLP endpoint: for a quick local development and testing setup
- OpenTelemetry Collector: for a robust and scalable production setup
- Observe your services in Application Observability