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"
}
To obtain an endpoint of Jaeger or Zipkin, perform the following steps:
- Log on to the Tracing Analysis console.
- In the left-side navigation pane, click Cluster Configurations. Then, click the Access point information tab.
- In the top navigation bar, select a region. In the Cluster Information section, turn on Show Token.
- In the Client section, click Jaeger or Zipkin.
Obtain an endpoint of Jaeger or Zipkin in the Related Information column of the table in the lower part.
Note If your application is deployed in an Alibaba Cloud production environment, use a private endpoint. Otherwise, use a public endpoint. Generally, use the endpoint of v2 for Zipkin. Use the endpoint of v1 only if you know Zipkin well.
Background information
How is data reported?
- The following figure shows how to report data without using the Jaeger agent.
- The following figure shows how to report data by using the Jaeger agent.
Procedure
- Set parameters for initialization.Important Replace
<endpoint>
with the endpoint of the region where the client resides. You can obtain the endppoint 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');
});