Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


NodeJS Express Opentelemetry

15 Aug 2022 »

https://www.aspecto.io/blog/opentelemetry-collector-guide/

https://opentelemetry.io/docs/instrumentation/js/getting-started/nodejs/

docker run -dit --name rocky-nodejs rockylinux:8 bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
nvm install v16.16.0
npx express-generator otel-collector-sender
cd otel-collector-sender

npm install @opentelemetry/sdk-node @opentelemetry/api opentelemetry-instrumentation-express @opentelemetry/exporter-trace-otlp-http

/* tracing.js */
// Require dependencies
const opentelemetry = require("@opentelemetry/sdk-node");
const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
const { ExpressInstrumentation } = require('opentelemetry-instrumentation-express');
// For troubleshooting, set the log level to DiagLogLevel.DEBUG
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);
const exporter = new OTLPTraceExporter({
 // optional - url default value is http://localhost:55681/v1/traces
 url: 'http://localhost:4318/v1/traces',
 // optional - collection of custom headers to be sent with each request, empty by default
 headers: {},
});
const sdk = new opentelemetry.NodeSDK({
 traceExporter: exporter,
 instrumentations: [new ExpressInstrumentation()]
});
sdk.start()

inside package.json start

PORT=9001 node -r ./tracing.js ./bin/www

check further

context is not shared