全部產品
Search
文件中心

Application Real-Time Monitoring Service:通過OpenTelemetry上報C++應用資料

更新時間:Aug 08, 2024

通過OpenTelemetry為應用埋點並上報鏈路資料至Managed Service for OpenTelemetry後,Managed Service for OpenTelemetry即可開始監控應用,您可以查看應用拓撲、調用鏈路、異常事務、慢事務和SQL分析等一系列監控資料。本文介紹如何使用OpenTelemetry為C++應用埋點並上報資料。

前提條件

擷取存取點資訊

  1. 登入ARMS控制台,在左側導覽列單擊接入中心

  2. 服務端應用地區單擊OpenTelemetry卡片。

  3. 在彈出的OpenTelemetry面板中選擇資料需要上報的地區。

    說明

    初次接入的地區將會自動進行資源初始化。

  4. 選擇串連方式上報方式,然後複製存取點資訊。

    • 串連方式:若您的服務部署在阿里雲上,且所屬地區與選擇的接入地區一致,推薦使用阿里雲內網方式,否則選擇公網方式。

    • 上報方式:根據用戶端支援的協議類型選擇HTTP或gRPC協議上報資料。

    image.png

樣本Demo

範例程式碼倉庫地址:opentelemetry-cpp-demo

環境要求

步驟一:環境準備

  1. (可選)運行C++鏡像。

    docker pull gcc # 這個鏡像用的系統是debian
    docker run -it --name otel-cpp-demo gcc bash
  2. 安裝必備的依賴。

    • protobuf

    • grpc

    apt-get update
    apt-get install sudo
    
    sudo apt-get install git cmake g++ libcurl4-openssl-dev
    # 安裝 protobuf
    sudo apt-get install protobuf-compiler libprotobuf-dev
    # 安裝 grpc
    sudo apt-get install -y libgrpc++-dev libgrpc-dev protobuf-compiler-grpc

步驟二:安裝OpenTelemetry C++ Library

git clone --recurse-submodules https://github.com/open-telemetry/opentelemetry-cpp

cd opentelemetry-cpp
mkdir build && cd build
cmake -DBUILD_TESTING=OFF -DWITH_OTLP_GRPC=ON -DWITH_OTLP_HTTP=ON ..

cmake --build . --target all

# 直接安裝到/usr/local(推薦)
cmake --install . 

# 或者安裝到指定路徑
# cmake --install . --prefix /opentelemetry-cpp-lib

步驟三:在專案中使用OpenTelemetry C++ Library

通過HTTP上報資料

  1. 建立專案。

    mkdir otel-http-export-demo
    cd otel otel-http-export-demo
  2. 編寫專案CMakeLists.txt檔案。

    cmake_minimum_required(VERSION 3.25.1) #version填cmake的版本,cmake --version
    project(otel-http-export-demo) # 括弧裡填專案名
    
    add_executable(otel-http-export-demo http_exporter.cc) # 專案名和main檔案
    
    find_package(opentelemetry-cpp CONFIG REQUIRED)
    find_package(protobuf)
    find_package(gRPC)
    find_package(CURL)
    find_package(nlohmann_json)
    
    include_directories("${OPENTELEMETRY_CPP_INCLUDE_DIRS}")
    
    target_link_libraries(
        opentelemetry_trace
        opentelemetry_common
        opentelemetry_http_client_curl
        opentelemetry_exporter_otlp_http
        opentelemetry_exporter_otlp_grpc
        opentelemetry_exporter_otlp_http_client
        opentelemetry_otlp_recordable
        opentelemetry_resources
    )
  3. 編寫Demo。

    測試代碼只包含一個http_exporter.cc檔案。

    請將${ServiceName}替換為您的應用程式名稱,將${HostName}替換為主機名稱。

    // Copyright The OpenTelemetry Authors
    // SPDX-License-Identifier: Apache-2.0
    
    #include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
    #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h"
    #include "opentelemetry/context/propagation/global_propagator.h"
    #include "opentelemetry/context/propagation/text_map_propagator.h"
    #include "opentelemetry/exporters/ostream/span_exporter_factory.h"
    #include "opentelemetry/nostd/shared_ptr.h"
    #include "opentelemetry/sdk/trace/simple_processor_factory.h"
    #include "opentelemetry/sdk/trace/tracer_context.h"
    #include "opentelemetry/sdk/trace/tracer_context_factory.h"
    #include "opentelemetry/sdk/trace/tracer_provider_factory.h"
    #include "opentelemetry/trace/propagation/http_trace_context.h"
    #include "opentelemetry/trace/provider.h"
    #include "opentelemetry/ext/http/client/http_client_factory.h"
    #include "opentelemetry/sdk/resource/semantic_conventions.h"
    #include "opentelemetry/sdk/common/global_log_handler.h"
    
    #include <string>
    
    
    namespace trace     = opentelemetry::trace;
    namespace trace_sdk = opentelemetry::sdk::trace;
    namespace otlp      = opentelemetry::exporter::otlp;
    namespace internal_log = opentelemetry::sdk::common::internal_log;
    namespace resource = opentelemetry::sdk::resource;
    
    namespace nostd = opentelemetry::nostd;
    
    namespace
    {
        opentelemetry::exporter::otlp::OtlpHttpExporterOptions opts;
        void InitTracer()
        {
            // 建立OTLP exporter
            auto exporter  = otlp::OtlpHttpExporterFactory::Create(opts);
            auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
    
            resource::ResourceAttributes attributes = {
                    {resource::SemanticConventions::kServiceName, "${ServiceName}"}, // 應用程式名稱
                    {resource::SemanticConventions::kHostName, "${HostName}"}
            };
            auto resource = opentelemetry::sdk::resource::Resource::Create(attributes);
    
            std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
                    trace_sdk::TracerProviderFactory::Create(std::move(processor), std::move(resource));
    
            // 設定Trace provider
            trace::Provider::SetTracerProvider(provider);
        }
    
        void CleanupTracer()
        {
            std::shared_ptr<opentelemetry::trace::TracerProvider> none;
            trace::Provider::SetTracerProvider(none);
        }
    
        nostd::shared_ptr<trace::Tracer> get_tracer()
        {
            auto provider = trace::Provider::GetTracerProvider();
            return provider->GetTracer("library name to trace", OPENTELEMETRY_SDK_VERSION);
        }
    
        void f1()
        {
            auto scoped_span = trace::Scope(get_tracer()->StartSpan("f1"));
        }
    
        void f2()
        {
            auto scoped_span = trace::Scope(get_tracer()->StartSpan("f2"));
    
            f1();
            f1();
        }
    
        void foo_library()
        {
            auto scoped_span = trace::Scope(get_tracer()->StartSpan("library"));
    
            f2();
        }
    }  // namespace
    
    /*
      Usage:
      - example_otlp_http
      - example_otlp_http <URL>
      - example_otlp_http <URL> <DEBUG>
      - example_otlp_http <URL> <DEBUG> <BIN>
      <DEBUG> = yes|no, to turn console debug on or off
      <BIN> = bin, to export in binary format
    */
    int main(int argc, char *argv[])
    {
        if (argc > 1)
        {
            opts.url = argv[1];
            if (argc > 2)
            {
                std::string debug  = argv[2];
                opts.console_debug = debug != "" && debug != "0" && debug != "no";
            }
    
            if (argc > 3)
            {
                std::string binary_mode = argv[3];
                if (binary_mode.size() >= 3 && binary_mode.substr(0, 3) == "bin")
                {
                    opts.content_type = otlp::HttpRequestContentType::kBinary;
                }
            }
        }
    
        if (opts.console_debug)
        {
            internal_log::GlobalLogHandler::SetLogLevel(internal_log::LogLevel::Debug);
        }
    
        InitTracer();
    
        foo_library();
    
        CleanupTracer();
    }
  4. 編譯專案。

    mkdir build && cd build && cmake .. && make
  5. 運行專案。

    ./otel-http-export-demo ${http-endpoint}

    請將${http-endpoint}替換為前提條件中擷取的HTTP存取點資訊。

    例如:

    ./otel-http-export-demo http://tracing-analysis-dc-hz.aliyuncs.com/adapt_xxxxx_xxxxx/api/otlp/traces

通過gRPC上報資料

  1. 建立專案。

    mkdir otel-grpc-export-demo
    cd otel otel-grpc-export-demo
  2. 編寫專案CMakeLists.txt檔案。

    cmake_minimum_required(VERSION 3.25.1) #version填cmake的版本,cmake --version
    project(otel-grpc-export-demo) # 括弧裡填專案名
    
    add_executable(otel-grpc-export-demo grpc_exporter.cc) # 專案名和main檔案
    
    find_package(opentelemetry-cpp CONFIG REQUIRED)
    find_package(protobuf)
    find_package(gRPC)
    find_package(CURL)
    find_package(nlohmann_json)
    
    include_directories("${OPENTELEMETRY_CPP_INCLUDE_DIRS}")
    
    target_link_libraries(
        otel-grpc-export-demo ${OPENTELEMETRY_CPP_LIBRARIES}
        opentelemetry_trace
        opentelemetry_common
        opentelemetry_http_client_curl
        opentelemetry_exporter_otlp_http
        opentelemetry_exporter_otlp_grpc
        opentelemetry_exporter_otlp_http_client
        opentelemetry_otlp_recordable
        opentelemetry_resources
    )
  3. 編寫Demo。

    測試代碼只包含一個grpc_exporter.cc檔案。

    請將${ServiceName}替換為您的應用程式名稱,將${HostName}替換為主機名稱。

    // Copyright The OpenTelemetry Authors
    // SPDX-License-Identifier: Apache-2.0
    
    #include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h"
    #include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
    #include "opentelemetry/context/propagation/global_propagator.h"
    #include "opentelemetry/context/propagation/text_map_propagator.h"
    #include "opentelemetry/exporters/ostream/span_exporter_factory.h"
    #include "opentelemetry/nostd/shared_ptr.h"
    #include "opentelemetry/sdk/trace/simple_processor_factory.h"
    #include "opentelemetry/sdk/trace/tracer_context.h"
    #include "opentelemetry/sdk/trace/tracer_context_factory.h"
    #include "opentelemetry/sdk/trace/tracer_provider_factory.h"
    #include "opentelemetry/trace/propagation/http_trace_context.h"
    #include "opentelemetry/trace/provider.h"
    #include "opentelemetry/ext/http/client/http_client_factory.h"
    #include "opentelemetry/sdk/resource/semantic_conventions.h"
    #include "opentelemetry/sdk/common/global_log_handler.h"
    
    #include <string>
    
    
    namespace trace     = opentelemetry::trace;
    namespace trace_sdk = opentelemetry::sdk::trace;
    namespace otlp      = opentelemetry::exporter::otlp;
    namespace internal_log = opentelemetry::sdk::common::internal_log;
    namespace resource = opentelemetry::sdk::resource;
    
    namespace nostd = opentelemetry::nostd;
    
    namespace
    {
        opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts;
        void InitTracer()
        {
            // 建立OTLP exporter
            auto exporter  = otlp::OtlpGrpcExporterFactory::Create(opts);
            auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
    
            resource::ResourceAttributes attributes = {
                    {resource::SemanticConventions::kServiceName, "${ServiceName} "}, // 應用程式名稱
                    {resource::SemanticConventions::kHostName, "${HostName}"}
            };
            auto resource = opentelemetry::sdk::resource::Resource::Create(attributes);
    
            std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
                    trace_sdk::TracerProviderFactory::Create(std::move(processor), std::move(resource));
    
            // 設定Trace provider
            trace::Provider::SetTracerProvider(provider);
        }
    
        void CleanupTracer()
        {
            std::shared_ptr<opentelemetry::trace::TracerProvider> none;
            trace::Provider::SetTracerProvider(none);
        }
    
        nostd::shared_ptr<trace::Tracer> get_tracer()
        {
            auto provider = trace::Provider::GetTracerProvider();
            return provider->GetTracer("library name to trace", OPENTELEMETRY_SDK_VERSION);
        }
    
        void f1()
        {
            auto scoped_span = trace::Scope(get_tracer()->StartSpan("f1"));
        }
    
        void f2()
        {
            auto scoped_span = trace::Scope(get_tracer()->StartSpan("f2"));
    
            f1();
            f1();
        }
    
        void foo_library()
        {
            auto scoped_span = trace::Scope(get_tracer()->StartSpan("library"));
    
            f2();
        }
    }  // namespace
    
    /*
      Usage:
      - example_otlp_grpc
      - example_otlp_grpc <URL> <TOKEN>
    */
    int main(int argc, char *argv[])
    {
        if (argc > 1)
        {
            opts.endpoint = argv[1];
            if (argc > 2)
            {
                opts.metadata.insert(std::pair<std::string, std::string>("authentication",argv[2]));
            }
    
            if (argc > 3)
            {
                opts.use_ssl_credentials         = true;
                opts.ssl_credentials_cacert_path = argv[3];
            }
        }
    
        InitTracer();
    
        foo_library();
    
        CleanupTracer();
    }
  4. 編譯專案。

    mkdir build && cd build && cmake .. && make
  5. 運行專案。

    ./otel-grpc-export-demo ${gRPC-endpoint} ${token}

    請將${gRPC-endpoint}${token}替換為前提條件中擷取的gRPC存取點和鑒權Token。

    例如:

    ./otel-grpc-export-demo http://tracing-analysis-dc-hz.aliyuncs.com:8090 xxxxx_xxxxxx

常見報錯

運行後提示:

./otel-grpc-export-demo: error while loading shared libraries: libopentelemetry_proto_grpc.so: cannot open shared object file: No such file or directory

執行以下命令修改環境變數。

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

/usr/local/lib是opentelemetry-cpp安裝的地址。

查看監控資料

登入ARMS控制台後,在應用監控 > 應用列表頁面選擇目標應用,查看鏈路資料。

說明

語言列顯示image表徵圖的應用為接入應用監控的應用,顯示-表徵圖的應用為接入可觀測鏈路 OpenTelemetry 版的應用。