Updated on 2026-01-30 GMT+08:00

Reporting Node.js Application Data Using OpenTelemetry

Huawei Cloud APM is compatible with OpenTelemetry and can directly receive the trace data reported using the OpenTelemetry SDK or Agent. This section describes how to use OpenTelemetry to connect a Node.js application to APM and report trace data.

Constraint

Node.js 14 or later must be used.

Environment Preparation

  1. Prepare the node and npm tools.
  2. Run the installation command:
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
    \. "$HOME/.nvm/nvm.sh"
    nvm install 18
  3. Verify the environment:
    node -v  
    npm -v

Demo

Use the Express framework to implement the dice rolling API.

  1. Initialize the project:
    npm init -y
  2. Install project dependencies:
    npm install express
  3. Compile the service code. Create an app.js file:
    const express = require('express');
    const PORT = parseInt("8080");
    const app = express();
    
    app.get('/rolldice', (req, res) => {
      res.send(getRandomNumber(1, 6).toString());
    });
    
    function getRandomNumber(min, max) {
      return Math.floor(Math.random() * (max - min + 1) + min);
    }
    
    app.listen(PORT, () => {
      console.log(`Listening for requests on http://localhost:${PORT}`);
    });
  4. Run the demo application:
    node app.js & 
    curl http://localhost:8080/rolldice

Non-Intrusive Installation

  1. Install the dependencies:

    npm install @opentelemetry/sdk-node \
      @opentelemetry/api \
      @opentelemetry/auto-instrumentations-node \
      @opentelemetry/sdk-metrics \
      @opentelemetry/sdk-trace-node

  2. Interconnect with OpenTelemetry:

    export OTEL_TRACES_EXPORTER="otlp" 
    export OTEL_EXPORTER_OTLP_PROTOCOL='grpc' 
    export OTEL_EXPORTER_OTLP_ENDPOINT="Access address"
    export OTEL_NODE_RESOURCE_DETECTORS="env,host,os" 
    export OTEL_SERVICE_NAME="Application name.Component name.Environment name"
    export OTEL_EXPORTER_OTLP_HEADERS="Authentication= Authentication information"
    export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"

  3. Run the startup command:

    node app.js &
    curl http://localhost:8080/rolldice

  4. Log in to the APM console.
  5. Click on the left and choose Management & Governance > Application Performance Management.
  6. In the navigation pane, choose Link Trace > Metrics.
  7. In the tree on the left, locate an environment and then go to the URL tab page to view the URL calling data.

  8. In the navigation pane, choose Link Trace > Tracing.