Skip to main content

OpenTelemetry NodeJS Instrumentation

Book meeting

Requirements

  • Node.js version 12 or newer
  • An app to add OpenTelemetry to

Instrumenting a sample Express application

For this tutorial, we’re going to create a very simple application: an express service that responds at http://localhost:9090/hello with "Hello World".

Steps to create a sample Express application:

  1. Git clone the sample app repo and change directory

    Sample nodejs application repo on GitHub.

    git clone https://github.com/SigNoz/sample-nodejs-app.git
    cd sample-nodejs-app
  2. Install the dependencies
    The required OpenTelemetry packages will get installed using this command. OpenTelemetry clients have two major components: the SDK and the API. Following are the OpenTelemetry packages used for the sample application:

    • @opentelemetry/api
    • @opentelemetry/auto-instrumentations-node
    • @opentelemetry/exporter-otlp-grpc
    • @opentelemetry/sdk-node

    npm i

    Note: auto-instrumentations-node is a meta-package from opentelemetry-js-contrib that provides a simple way to initialize multiple Node.js instrumentation.

  1. Start the Node application and start sending data to SigNoz

    The tracing.js file takes care of instantiating tracing for the application. You can have look at its content in the GitHub repo.

    Now you need to run your application with some environment variables for OpenTelemetry. Environment variables that need to be configured:

    a. IP of SigNoz backend - IP of the machine where SigNoz is installed. In case you have installed SigNoz on your local machine, you can use localhost

    b. service_name - the service you are monitoring (you can name it anything)

    You need to put these environment variables in the below command and run it at your terminal.

    OTEL_EXPORTER_OTLP_ENDPOINT="<IP of SigNoz>:4317" \
    OTEL_RESOURCE_ATTRIBUTES=service.name=<service_name> \
    node -r ./tracing.js index.js

    If you're running SigNoz on localhost, and want to name your service as node_app, the final command will be as follows:

    OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317" \
    OTEL_RESOURCE_ATTRIBUTES=service.name=node_app \
    node -r ./tracing.js index.js

note

Remember to allow incoming requests to port 4317 of machine where SigNoz backend is hosted

You can check your application running at http://localhost:9090/hello. You need to generate some load in order to see data reported on SigNoz dashboard. Refresh the endpoint for 10-20 times, and wait for 2-3 mins.

You should see your service node_app in the list of applications monitored on SigNoz dashboard as shown below.

nodejs-app-instrumentation

Troubleshooting your installation

Set an environment variable to run the OpenTelemetry launcher in debug mode, where it logs details about the configuration and emitted spans:

export OTEL_LOG_LEVEL=debug

The output may be very verbose with some benign errors. Early in the console output, look for logs about the configuration. Next, look for lines like the ones below, which are emitted when spans are emitted to SigNoz.

{
"traceId": "985b66d592a1299f7d12ebca56ca1fe3",
"parentId": "8d62a70aa335a227",
"name": "bar",
"id": "17ada85c3d55376a",
"kind": 0,
"timestamp": 1685674607399000,
"duration": 299,
"attributes": {},
"status": { "code": 0 },
"events": []
}
{
"traceId": "985b66d592a1299f7d12ebca56ca1fe3",
"name": "foo",
"id": "8d62a70aa335a227",
"kind": 0,
"timestamp": 1585130342183948,
"duration": 315,
"attributes": {
"name": "value"
},
"status": { "code": 0 },
"events": [
{
"name": "event in foo",
"time": [1585130342, 184213041]
}
]
}

Running short applications (Lambda/Serverless/etc) If your application exits quickly after startup, you may need to explicitly shutdown the tracer to ensure that all spans are flushed:

opentelemetry.trace.getTracer('your_tracer_name').getActiveSpanProcessor().shutdown()