APM
APM Instrumentation
Node.js
8 min
this guide walks you through the process of instrumenting a node js application using zero code opentelemetry for collecting traces, metrics, logs, and monitoring using kloudmate zero code instrumentation enables you to capture telemetry from many popular libraries and frameworks without modifying your application code step 1 prerequisites before you begin, ensure you have the following installed node js typescript (if you will use typescript) step 2 example application for demonstration, we will be using a basic express application you can adapt this to other frameworks like koa or nest js if needed 1\ set up an empty package json in a new directory npm init y 2\ install express and dependencies javascript npm install express typescript npm install typescript ts node @types/node express @types/express 3\ create and run an http server create a file named app js (or app ts if you're using typescript) and add the following code to set up a simple http server / app js / const express = require('express'); const port = parseint(process env port || '8080'); const app = express(); function getrandomnumber(min, max) { return math floor(math random() (max min) + min); } app get('/rolldice', (req, res) => { res send(getrandomnumber(1, 6) tostring()); }); app listen(port, () => { console log(`listening for requests on http //localhost ${port}`); });/ app ts / import express, { express } from 'express'; const port number = parseint(process env port || '8080'); const app express = express(); function getrandomnumber(min number, max number) { return math floor(math random() (max min) + min); } app get('/rolldice', (req, res) => { res send(getrandomnumber(1, 6) tostring()); }); app listen(port, () => { console log(`listening for requests on http //localhost ${port}`); }); step 3 configure environment variables set these variables in your shell environment before starting the app export otel traces exporter="otlp" export otel metrics exporter="otlp" export otel exporter otlp endpoint="https //otel kloudmate com 4318" export otel exporter otlp headers="authorization=your private key" export otel node resource detectors="env,host,os" export otel node disabled instrumentations="fs,dns,net" export otel service name="your service name" export node options=" require @opentelemetry/auto instrumentations node/register" replace your private key with your actual kloudmate private key and set your service name accordingly step 4 run the application $ node app js listening for requests on http //localhost 8080$ npx ts node app ts listening for requests on http //localhost 8080 open http //localhost 8080/rolldice http //localhost 8080/rolldice in your web browser to verify now you should see spans on the traces page in your kloudmate account node js metrics name metrics nodejs eventloop delay min event loop minimum delay nodejs eventloop delay max event loop maximum delay nodejs eventloop delay mean event loop mean delay nodejs eventloop delay stddev event loop standard deviation delay nodejs eventloop delay p50 event loop 50 percentile delay nodejs eventloop delay p90 event loop 90 percentile delay nodejs eventloop delay p99 event loop 99 percentile delay nodejs eventloop utilization event loop utilization nodejs eventloop time cumulative duration of time the event loop has been in each state nodejs eventloop state the state of event loop time source url for the example application https //opentelemetry io/docs/zero code/js/ for instructions on instrumenting the frontend with opentelemetry, refer to the node otel demo repository , which demonstrates how to instrument a node js frontend application using opentelemetry to send telemetry data to kloudmate