git clone --recurse-submodules https://github.com/open-telemetry/opentelemetry-cpp
- 如果子module下载失败,执行以下命令
cd opentelemetry-cpp
git submodule update --init
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>
- 修改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})
# 关键内容
- 修改 grpc_exporter.cc
- 替换
<gRPC-endpoint>
和<gRPC-token>
,请从 链路追踪控制台 -> 集群配置 -> 接入点信息 -> OpenTelemetry 中查看 - 请将
<your-service-name>
替换为您的应用名 - 请将
<your-host-name>
替换为您的主机名
- 编译demo项目
mkdir build && cd build && cmake .. && make
- 运行
./otel-grpc-export-demo
- 修改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})
# 关键内容
- 修改 http_exporter.cc
- 替换
<http-endpoint>
,请从 链路追踪控制台 -> 集群配置 -> 接入点信息 -> OpenTelemetry 中查看 - 请将
<your-service-name>
替换为您的应用名 - 请将
<your-host-name>
替换为您的主机名
- 编译demo项目
mkdir build && cd build && cmake .. && make
- 运行
./otel-http-export-demo