On-demand workshop

Send Your First Trace from a Node.js API

You instrument an Express app with OpenTelemetry and the monoscope Express SDK, then open its first request, trace and captured bodies in monoscope.

Before you start

  • Node.js and npm, with an Express app you can change and restart (or a new empty folder)
  • A monoscope account. The free plan includes 10k events a day and needs no credit card.
  • macOS or Linux for the monoscope CLI check

By the end you'll have

  • Got a project API key from Settings → API Keys
  • Installed @monoscopetech/express with OpenTelemetry auto-instrumentation
  • Pointed the OTLP exporter at monoscope with four environment variables
  • Opened a request in Explorer, with its trace and its request and response bodies

A trace is the record of one request as it moves through your code: the route it hit, the calls it made, how long each took and where it failed. In this workshop you add OpenTelemetry and the monoscope Express middleware to a small API, send it a few requests, and open the exact request in monoscope with its headers and bodies.

1. Get Your API Key

Sign up at app.monoscope.tech. monoscope creates your first project and opens the project setup. Step 4 of the setup, Instrument your apps or servers, shows your API key.

To see the key later, or to make a new one, open Settings → API Keys, click New Key, enter a Key title such as orders-dev, and click Create key. Copy the key. You need it in step 3.

Use a different API key for each environment, for example development, staging and production. Then you can turn off one key without an effect on the others.

Details: Get your API key.

2. Install the Packages

In your Express project, install the monoscope Express SDK, the OpenTelemetry API and the auto-instrumentation package:

npm install --save express @monoscopetech/express @opentelemetry/api @opentelemetry/auto-instrumentations-node

The auto-instrumentation package records the OpenTelemetry spans. The monoscope middleware adds a span for each request with its request and response details, including bodies.

3. Configure the Exporter

OpenTelemetry reads its configuration from environment variables. Set these in the shell that starts your app, and replace YOUR_API_KEY with the key from step 1:

export OTEL_EXPORTER_OTLP_ENDPOINT="http://otelcol.monoscope.tech:4317"
export OTEL_EXPORTER_OTLP_PROTOCOL="grpc"
export OTEL_SERVICE_NAME="orders-service"
export OTEL_RESOURCE_ATTRIBUTES="x-api-key=YOUR_API_KEY"
export OTEL_NODE_DISABLED_INSTRUMENTATIONS=net,connect,dns,fs

OTEL_SERVICE_NAME is the name your service has in monoscope. The last line turns off the net, connect, dns and fs instrumentations.

Import order matters: @opentelemetry/auto-instrumentations-node/register reads the environment variables when it is imported. Set the OTEL_* variables before you start the app. If you keep them in a .env file instead, install dotenv and import dotenv/config first.

4. Add the Middleware

Create server.mjs. The register import must come first, before Express loads:

import "@opentelemetry/auto-instrumentations-node/register";
import express from "express";
import { Monoscope } from "@monoscopetech/express";

const app = express();
app.use(express.json());

const monoscopeClient = Monoscope.NewClient({
  captureRequestBody: true,
  captureResponseBody: true,
  redactRequestBody: ["$.password"],
});

app.use(monoscopeClient.middleware);

app.post("/orders", (req, res) => {
  res.status(201).json({ id: "ord_8812", items: req.body.items });
});

app.get("/orders/:id", (req, res) => {
  throw new Error(`Order ${req.params.id} not found`);
});

// After all controllers, before any other error middleware
app.use(monoscopeClient.errorMiddleware);

app.listen(3000, () => {
  console.log("orders-service listening on port 3000");
});

captureRequestBody and captureResponseBody are false by default. With them on, monoscope keeps the bodies, and redactRequestBody removes the fields you list before they leave your server. errorMiddleware reports uncaught errors and links each one to its request.

Details: Express configuration options and reporting errors.

5. Send Some Requests

Start the app in the shell where you set the variables, then call both routes from a second terminal:

node server.mjs
curl -X POST http://localhost:3000/orders \
  -H "Content-Type: application/json" \
  -d '{"items":["sku_123"],"password":"hunter2"}'

curl http://localhost:3000/orders/ord_404

The first request succeeds. The second throws, so it produces an error span.

6. Find the Request in monoscope

Open Explorer in the sidebar. Select orders-service in the All services picker, or type this query in the query bar and press Enter:

resource.service.name == "orders-service"

Click the POST /orders request. Req Body shows the body with password redacted, and Res Body shows the response. Click View trace to see the spans of the request in a waterfall. Then open the GET /orders/:id request to read the error and its stack trace.

Details: Inspect an event and Traces.

7. If Nothing Arrives

Send a test event with the monoscope CLI. If the CLI event shows in Explorer but your app’s requests do not, the problem is in the app setup.

curl https://monoscopehq.com/install.sh | sh
monoscope auth login
monoscope send-event -m "Hello from monoscope"

Search for "Hello from monoscope" in Explorer. If no events arrive after a minute:

  1. Make sure that OTEL_RESOURCE_ATTRIBUTES contains your API key, with no spaces.
  2. Make sure that your network allows outgoing traffic to otelcol.monoscope.tech on port 4317 (gRPC) or 4318 (HTTP).
  3. Run monoscope auth status to see which project the CLI sends to.

Details: Check that events arrive and integration troubleshooting.

What You Built

Your Express API now sends a trace for every request to monoscope, with the request and response bodies kept and the password field redacted before it leaves your server. Uncaught errors are linked to the request that caused them, so the next failure arrives with the exact payload that triggered it.

Next Steps

Know what happened. Prove it.