All Products
Search
Document Center

Managed Service for OpenTelemetry:Report Node.js application data

Last Updated:Jan 11, 2024

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

To obtain an endpoint of Jaeger or Zipkin, perform the following steps:

  1. Log on to the Managed Service for OpenTelemetry console.

  2. In the left-side navigation pane, click Cluster Configurations. Then, click the Access point information tab.

  3. In the top navigation bar, select a region. In the Cluster Information section, turn on Show Token.

  4. 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.

    Jaeger/Zipkin接入点信息

    Note

    If your application is deployed in an Alibaba Cloud production environment, use a VPC 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. 不通过Jaeger Agent而直接上报

  • The following figure shows how to report data by using the Jaeger agent.

    通过Jaeger Agent上报数据

Procedure

  1. Set parameters for initialization.

    Important

    Replace <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>"
        },
    };
  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');
});