OpenTelemetry Support
Using OpenTelemetry SDKs
OTel For Node.js
6min
in this guide, we will walk you through the process of setting up and using opentelemetry in node js you will learn how to instrument a simple application to produce traces, metrics, and logs step 1 prerequisites before diving into opentelemetry, be sure that you have the following installed node js typescript , if you are going to use typescript step 2 example application for this tutorial, we will be using a basic express application however, opentelemetry javascript is compatible with other web frameworks like koa and nest js as well feel free to adapt the instructions to your preferred framework step 3 installation set up an empty package json in a new directory npm init y 2\ install express dependencies npm install express npm install typescript \\ ts node \\ @types/node \\ express \\ @types/express 3\ create and launch an http server by creating a file named app js ( app ts , if you are using typescript) and adding the following code to it / 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}`); }); 4\ run the application using the following command then open http //localhost 8080/rolldice http //localhost 8080/rolldice in your web browser $ node app js listening for requests on http //localhost 8080 $ npx ts node app ts listening for requests on http //localhost 8080 step 4 instrumentation to install the instrumentation packages, we will use the auto instrumentations node package it automatically creates spans corresponding to code called in libraries 1\ use the following command to install the node sdk auto instrumentaion andexporter packages npm install @opentelemetry/sdk node \\ @opentelemetry/api \\ @opentelemetry/auto instrumentations node \\ @opentelemetry/sdk metrics \\ @opentelemetry/exporter trace otlp http \\ @opentelemetry/exporter metrics otlp http 2\ the instrumentation setup and configuration are required to run before the application code we will use the require flag for this task 3\ create a file named instrumentation js ( instrumentation ts if you are using typescript) and add the following setup code to it / instrumentation js / const { nodesdk } = require('@opentelemetry/sdk node'); const { getnodeautoinstrumentations, } = require('@opentelemetry/auto instrumentations node'); const { periodicexportingmetricreader } = require('@opentelemetry/sdk metrics'); const { otlpmetricexporter, } = require('@opentelemetry/exporter metrics otlp http'); const { otlptraceexporter, } = require('@opentelemetry/exporter trace otlp http'); const sdk = new nodesdk({ traceexporter new otlptraceexporter({ url `https //otel kloudmate com 4318/v1/traces`, headers { authorization '\<your secret)key>', }, }), metricreader new periodicexportingmetricreader({ exporter new otlpmetricexporter({ url `https //otel kloudmate com 4318/v1/metrics`, headers { authorization '\<your secret key>', }, }), }), instrumentations \[getnodeautoinstrumentations()], }); sdk start(); / instrumentation ts / import { nodesdk } from '@opentelemetry/sdk node'; import { getnodeautoinstrumentations } from '@opentelemetry/auto instrumentations node'; import { periodicexportingmetricreader } from '@opentelemetry/sdk metrics'; import { otlpmetricexporter } from '@opentelemetry/exporter metrics otlp http'; import { otlptraceexporter } from '@opentelemetry/exporter trace otlp http'; const sdk = new nodesdk({ traceexporter new otlptraceexporter({ url `https //otel kloudmate com 4318/v1/traces`, headers { authorization '\<your secret)key>', }, }), metricreader new periodicexportingmetricreader({ exporter new otlpmetricexporter({ url `https //otel kloudmate com 4318/v1/metrics`, headers { authorization '\<your secret key>', }, }), }), instrumentations \[getnodeautoinstrumentations()], }); sdk start(); the exporter url mentioned above, along with the kloudmate api key as the authorization header will send the data to kloudmate step 5 run the instrumented app 1\ run the instrumented application and use the require flag to load the instrumentation before the application code $ node require /instrumentation js app js listening for requests on http //localhost 8080$ npx ts node require /instrumentation ts app ts listening for requests on http //localhost 8080 2\ open http //localhost 8080/rolldice in your web browser you should see spans on the traces page in your kloudmate account source url for the example application https //opentelemetry io/docs/instrumentation/js/getting started/nodejs/