This topic describes how to report the data of a Node.js application to Tracing Analysis.
Prerequisites
Jaeger dependencies are added to the package of the Node.js project.
"dependencies": {
"jaeger-client": "^3.12.0"
}
Background information
Procedure
Set parameters for initialization.
ImportantReplace
<endpoint>
with the endpoint of the region where the client resides. You can obtain the endpoint on the Overview page in the Tracing Analysis console. For more information, see the Prerequisites topic.const initTracer = require("jaeger-client").initTracer; const config = { serviceName: 'node-service', sampler: { type: "const", param: 1 }, reporter: { collectorEndpoint: "<endpoint>" }, };
Create a Tracer object.
const tracer = initTracer(config);
Create a span.
const span = tracer.startSpan("say-hello"); // Optional. Set one or more tags. span.setTag("tagKey-01", "tagValue-01"); // Optional. Set one or more logs. span.log({event: "timestamp", value: Date.now()}); // Finish the span. span.finish();
Log on to the Tracing Analysis console and view the trace.
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');
});