DEV Community

Jonathan Mourtada
Jonathan Mourtada

Posted on • Originally published at mourtada.se on

1 1

OpenTelemetry tracing with nodejs and express

In this post I’ll go through a simple example on how to setup OpenTelemetry tracing in a nodejs express application.

Hello world express

// index.js
const express = require("express");

const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.send("Hello World!");
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

Enter fullscreen mode Exit fullscreen mode

Needed packages

First we need to add the OpenTelemetry api and sdk package

yarn add @opentelemetry/api @opentelemetry/sdk-node

Enter fullscreen mode Exit fullscreen mode

To instrument express, incoming and outgoing http requests we use the express and http instrumentation libraries.

yarn add @opentelemetry/instrumentation-http @opentelemetry/instrumentation-express

Enter fullscreen mode Exit fullscreen mode

And lastly we add the otlp grpc tracing exporter.

yarn add @opentelemetry/exporter-trace-otlp-grpc

Enter fullscreen mode Exit fullscreen mode

Setup tracing

// tracing.js
const OpenTelemetry = require("@opentelemetry/sdk-node");
const Resources = require("@opentelemetry/resources");
const SemanticConventions = require("@opentelemetry/semantic-conventions");
const InstrumentationHttp = require("@opentelemetry/instrumentation-http");
const InstrumentationExpress = require("@opentelemetry/instrumentation-express");
const ExporterTraceOtlpGrpc = require("@opentelemetry/exporter-trace-otlp-grpc");

const sdk = new OpenTelemetry.NodeSDK({
  resource: new Resources.Resource({
    [SemanticConventions.SemanticResourceAttributes.SERVICE_NAME]: "my-service",
  }),
  traceExporter: new ExporterTraceOtlpGrpc.OTLPTraceExporter({
    // url is optional and can be omitted - default is localhost:4317
    url: process.env["OTEL_EXPORTER_OTLP_ENDPOINT"] || undefined,
  }),
  instrumentations: [
    new InstrumentationHttp.HttpInstrumentation(),
    new InstrumentationExpress.ExpressInstrumentation(),
  ],
});

sdk.start();

Enter fullscreen mode Exit fullscreen mode

Run the express server

To configure and start the tracing sdk we can use nodes -r/--require flag to preload our tracing module.

node -r ./tracing.js index.js

Enter fullscreen mode Exit fullscreen mode

Now every request will be traced and exported to the configured otlp receiver.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay