Skip to content

alibabacloud-observability/cpp-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

通过OpenTelemetry上报C++应用数据

1. 下载 Opentelemetry C++

git clone --recurse-submodules https://github.com/open-telemetry/opentelemetry-cpp
  • 如果子module下载失败,执行以下命令
cd opentelemetry-cpp
git submodule update --init

2. 编译构建 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

# 请替换</path/to/opentelemetry-cpp-lib>,构建好的cmake targets会保存在该路径
cmake --install . --prefix </path/to/opentelemetry-cpp-lib>

3. 方法一:通过gRPC协议上报数据(otel-grpc-export-demo)

  1. 修改demo项目CMakeLists中的内容
  • 请替换 <your-cmake-version> 为您的cmake版本
  • 请替换 </path/to/opentelemetry-cpp-lib> 为之前的设置的路径
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)

# 关键内容
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})
# 关键内容
  1. 修改 grpc_exporter.cc
  • 替换 <gRPC-endpoint><gRPC-token>,请从 链路追踪控制台 -> 集群配置 -> 接入点信息 -> OpenTelemetry 中查看
  • 请将 <your-service-name> 替换为您的应用名
  • 请将 <your-host-name> 替换为您的主机名
  1. 编译demo项目
mkdir build && cd build && cmake .. && make 
  1. 运行
./otel-grpc-export-demo

4. 方法二:通过HTTP协议上报数据(otel-http-export-demo)

  1. 修改demo项目CMakeLists中的内容
  • 请替换 <your-cmake-version> 为您的cmake版本
  • 请替换 </path/to/opentelemetry-cpp-lib> 为之前的设置的路径
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)

# 关键内容
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})
# 关键内容
  1. 修改 http_exporter.cc
  • 替换 <http-endpoint>,请从 链路追踪控制台 -> 集群配置 -> 接入点信息 -> OpenTelemetry 中查看
  • 请将 <your-service-name> 替换为您的应用名
  • 请将 <your-host-name> 替换为您的主机名
  1. 编译demo项目
mkdir build && cd build && cmake .. && make 
  1. 运行
./otel-http-export-demo

Releases

No releases published

Packages

No packages published