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"
  }

  1. Log on to Tracing Analysis console, in the Region-specific Information area, turn on View Token.
  2. In the Client area, click the desired tracing client.
  3. 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.

Report Tracing Data Directly

The workflow of reporting data through Agent is as shown in the following figure.

Report Tracing Data By Agent

Procedure

  1. Set parameters for initialization.
    Notice Replace <endpoint> with the corresponding endpoint in the corresponding region that is displayed on the Overview page. For more information about how to obtain access point information, see Obtain access point information.
    const initTracer = require("jaeger-client").initTracer;
    
    const config = {
        serviceName: 'node-service',
        sampler: {
            type: "const",
            param: 1
        },
        reporter: {
            collectorEndpoint: "<endpoint>"
        },
    };
  2. Create a Tracer object.
    const tracer = initTracer(config);
  3. 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();
  4. 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');
});