This project demonstrates OpenTelemetry instrumentation in a Node.js application with a custom OTLP collector for receiving telemetry data.
dice-server.ts- Express server with dice rolling endpointsdice-lib.ts- Business logic with OpenTelemetry spanscollector-server.js- Mock OTLP collector to receive traces and metricsinstrumentation.ts- OpenTelemetry SDK configuration
- Node.js (version 14 or higher)
- npm
Install the required dependencies:
npm installThe collector server receives and logs telemetry data from your instrumented application.
npm run collectorThis will start the collector on http://localhost:4318 and you should see:
Collector rodando em http://localhost:4318
In a new terminal, start the instrumented dice rolling server:
npm startThis will start the dice server on http://localhost:8080 and you should see:
Listening for requests on http://localhost:8080
Note: The --require ./instrumentation.ts flag is important as it loads the OpenTelemetry instrumentation before your application starts.
Make a request to roll some dice:
curl "http://localhost:8080/rolldice?rolls=3"You should see:
- The dice results returned as JSON
- Console output in the dice server terminal showing the rolled numbers
- Detailed telemetry data logged in the collector server terminal, including:
- Trace spans showing the execution flow
- Events and attributes from the dice rolling operations
- Parent-child relationships between spans
GET /rolldice?rolls=<number>- Roll dice the specified number of times
Example:
curl "http://localhost:8080/rolldice?rolls=5"POST /v1/traces- Receives OTLP trace dataPOST /v1/metrics- Receives OTLP metrics dataGET /v1/metrics- Health check endpoint
When you make requests, you'll see spans in the collector logs with the following structure:
- rollTheDice - Root span for the entire operation
- rollOnce:0, rollOnce:1, etc. - Child spans for each individual dice roll
- logNumber - Nested spans within each roll that include:
- Events:
number-rolled,number-resolved - Attributes:
lastRolledwith the dice value
- Events:
- "req.body is undefined": Make sure the collector server is running and has the JSON parsing middleware
- No telemetry data: Ensure you're using
--require ./instrumentation.tswhen starting the dice server - Connection errors: Verify both servers are running on the correct ports (4318 for collector, 8080 for dice server)
- The collector server uses
JSON.stringify(req.body, null, 2)to format the telemetry data for easy reading - The instrumentation uses HTTP/JSON exporters instead of protobuf for easier debugging
- All spans are properly nested using
startActiveSpanfor automatic parent-child relationships