This topic shows you how to instrument a Node.js application to perform tracing analysis.
Prerequisites
Jaeger dependencies are added to the package of the Node.js project.
"dependencies": {
"jaeger-client": "^3.12.0"
}
- Log on to Tracing Analysis console, in the Region-specific Information area, turn on View Token.
- In the Client area, click the desired tracing client.
- In the Related Information column in the table below, click the copy icon at the end of Endpoint information.
Background information
The workflow of reporting data directly without Agent is as shown in the following figure.
The workflow of reporting data through Agent is as shown in the following figure.
Procedure
Complete sample code based on Express
const express = require("express");
const initTracer = require("jaeger-client").initTracer;
const app = express();
const config = {
serviceName: 'node-service',
sampler: {
type: "const",
param: 1
},
reporter: {
collectorEndpoint: "<endpoint>"
},
};
const tracer = initTracer(config);
app.all('*', function (req, res, next) {
req.span = tracer.startSpan("say-hello");
next();
});
app.get("/api", function (req, res) {
const span = req.span;
span.log({event: "timestamp", value: Date.now()});
req.span.finish();
res.send({code: 200, msg: "success"});
});
app.listen(3000, '127.0.0.1', function () {
console.log('start');
});