Skip to main content

Node.js and OpenTelemetry

Build integration and end-to-end tests in minutes, instead of days, using OpenTelemetry and Tracetest.

Node.js App with Tracetest​

This is a quick start on how to configure a Node.js app to use OpenTelemetry instrumentation with traces, and Tracetest for enhancing your E2E and integration tests with trace-based testing.

Prerequisites​

You can run this example with Docker, or locally with Node.js installed on your machine.

Project Structure​

The project contains Tracetest Agent and a Node.js app.

1. Tracetest Agent​

Install and run Tracetest Agent locally.

tracetest start

Once started, Tracetest Agent will:

  • Expose OTLP ports 4317 (gRPC) and 4318 (HTTP) for trace ingestion.
  • Be able to trigger test runs in the environment where it is running.
  • Be able to connect to a trace data store that is not accessible outside of your environment.

2. Node.js App​

The Node.js app is a simple Express app, contained in the app.js file.

The OpenTelemetry tracing is contained in the tracing.otel.grpc.js or tracing.otel.http.js files. Traces will be sent to Tracetest Agent.

Here's the content of the tracing.otel.grpc.js file:

const opentelemetry = require('@opentelemetry/sdk-node')
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node')
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');

const sdk = new opentelemetry.NodeSDK({
// OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is passed into "new OTLPTraceExporter" automatically
traceExporter: new OTLPTraceExporter(),
instrumentations: [getNodeAutoInstrumentations()],
})
sdk.start()

Choosing the tracing.otel.grpc.js file will send traces to Tracetest Agent's GRPC.

Configure it an environment variable:

  • Running in Docker: OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://host.docker.internal:4317
  • Running locally: OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4317

Enabling the tracer is done by preloading the trace file.

node -r ./tracing.otel.grpc.js app.js

In the package.json you will see two npm scripts for running the respective tracers alongside the app.js.

"scripts": {
"with-grpc-tracer":"node -r ./tracing.otel.grpc.js app.js",
"with-http-tracer":"node -r ./tracing.otel.http.js app.js"
},

To start the server, run this command:

npm run with-grpc-tracer
# or
npm run with-http-tracer

Run the Node.js App with Docker Compose​

The docker-compose.yaml file and Dockerfile in the root directory are for the Node.js app.

As you can see, the Dockerfile uses the command above.

FROM node:slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "run", "with-grpc-tracer" ]

And, the docker-compose.yaml contains just one service for the Node.js app.

version: '3'
services:
app:
image: quick-start-nodejs
extra_hosts:
- "host.docker.internal:host-gateway"
build: .
ports:
- "8080:8080"
environment:
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://host.docker.internal:4317

To start it, run this command:

docker compose build # optional if you haven't already built the image
docker compose up

This will start the Node.js app and send the traces to Tracetest Agent.

Run the Node.js App Locally​

Install Node.js and npm in your local development environment.

Install the npm modules, export the OTLP endpoint, and run the Node.js app.

npm i 
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4317
npm run with-grpc-tracer

This will start the Node.js app and send the traces to Tracetest Agent.

Run Tracetest Tests​

Open Tracetest and start creating tests! Make sure to use the http://localhost:8080/ URL in your test creation.

Learn More​

Feel free to check out our examples in GitHub and join our Slack Community for more info!