Before you can view the trace data of your application in the Tracing Analysis console, you need to use a client to report the trace data to Tracing Analysis. This topic describes how to use OpenTelemetry to report the data of C++ applications.

Prerequisites

To obtain an endpoint of OpenTelemetry, perform the following steps:
  1. Log on to the Tracing Analysis 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 OpenTelemetry.
    Obtain an endpoint of OpenTelemetry in the Related Information column of the table in the lower part. Endpoint of OpenTelemetry
    Note If your application is deployed in an Alibaba Cloud production environment, use an access point of Alibaba Cloud VPC. Otherwise, use a public endpoint.

Sample code

You can obtain the sample code on the cpp-demo page.

Environment requirements

You can obtain the supported C++ versions and development platforms on the opentelemetry-cpp page.

Step 1: Download and compile OpenTelemetry C++

  1. Run the following command to download OpenTelemetry C++:
    git clone --recurse-submodules https://github.com/open-telemetry/opentelemetry-cpp
    Note If a submodule fails to be downloaded, run the following commands:
    cd opentelemetry-cpp
    git submodule update --init
  2. Run the following commands to compile and build OpenTelemetry C++:
    cd opentelemetry-cpp
    mkdir build && cd build && cmake -DCMAKE_CXX_STANDARD=14 -DBUILD_TESTING=OFF -DWITH_OTLP=ON -DWITH_OTLP_HTTP=ON ..
    cmake --build . --target all
    cmake --install . --prefix </path/to/opentelemetry-cpp-lib> # Replace </path/to/opentelemetry-cpp-lib> with the path in which you want to build OpenTelemetry C++. The created CMake targets are stored in this path. 

Step 2: Report data

Method 1: Report data over gRPC

  1. Modify the content in the CMakeLists file of the demo project.
    • Replace <your-cmake-version> with your CMake version.
    • Replace </path/to/opentelemetry-cpp-lib> with the path in which OpenTelemetry C++ is built.
    cmake_minimum_required(VERSION <your-cmake-version>)
    project(otel-grpc-export-demo)
    
    set(CMAKE_CXX_STANDARD 14)
    
    add_executable(otel-grpc-export-demo grpc_exporter.cc)
    
    # Key content
    find_package(</path/to/opentelemetry-cpp-lib>/lib/cmake/opentelemetry-cpp/opentelemetry-cpp CONFIG REQUIRED)
    find_package(</path/to/opentelemetry-cpp-lib>/lib/cmake/nlohmann_json/nlohmann_json CONFIG REQUIRED)
    find_package(protobuf REQUIRED)
    find_package(gRPC REQUIRED)
    find_package(CURL REQUIRED)
    
    target_include_directories(otel-grpc-export-demo PRIVATE ${OPENTELEMETRY_CPP_INCLUDE_DIRS})
    target_link_libraries(otel-grpc-export-demo PRIVATE ${OPENTELEMETRY_CPP_LIBRARIES})
    # Key content
  2. Modify the grpc_exporter.cc file:
    • Replace <gRPC-endpoint> and <gRPC-token> with the endpoint and token that you obtain. For more information about how to obtain the endpoint and token, see the "Prerequisites" section in this topic.
    • Replace <your-service-name> with the name of your application.
    • Replace <your-host-name> with your hostname.
  3. Compile the demo project.
    mkdir build && cd build && cmake .. && make
  4. Run the demo project.
    ./otel-grpc-export-demo

Method 2: Report data over HTTP

  1. Modify the content in the CMakeLists file of the demo project.
    • Replace <your-cmake-version> with your CMake version.
    • Replace </path/to/opentelemetry-cpp-lib> with the path in which OpenTelemetry C++ is built.
    cmake_minimum_required(VERSION <your-cmake-version>)
    project(otel-http-export-demo)
    
    set(CMAKE_CXX_STANDARD 14)
    
    add_executable(otel-http-export-demo http_exporter.cc)
    
    # Key content
    find_package(</path/to/opentelemetry-cpp-lib>/lib/cmake/opentelemetry-cpp/opentelemetry-cpp CONFIG REQUIRED)
    find_package(</path/to/opentelemetry-cpp-lib>/lib/cmake/nlohmann_json/nlohmann_json CONFIG REQUIRED)
    find_package(protobuf REQUIRED)
    find_package(gRPC REQUIRED)
    find_package(CURL REQUIRED)
    
    target_include_directories(otel-http-export-demo PRIVATE ${OPENTELEMETRY_CPP_INCLUDE_DIRS})
    target_link_libraries(otel-http-export-demo PRIVATE ${OPENTELEMETRY_CPP_LIBRARIES})
    # Key content
  2. Modify the http_exporter.cc file:
    • Replace <http-endpoint> with the endpoint that you obtain. For more information about how to obtain the endpoint, see the "Prerequisites" section in this topic.
    • Replace <your-service-name> with the name of your application.
    • Replace <your-host-name> with your hostname.
  3. Compile the demo project.
    mkdir build && cd build && cmake .. && make
  4. Run the demo project.
    ./otel-http-export-demo

View the monitoring data

On the Applications page in the Tracing Analysis console, click the name of the application. On the page that appears, view the trace data.