Code Monkey home page Code Monkey logo

cpp-demo's Introduction

通过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

cpp-demo's People

Contributors

coldilocks avatar

Stargazers

俞航 avatar

Watchers

zongze wu avatar StabilityMan avatar Carpela avatar imp20052094 avatar Ziqi Zhao avatar Alibaba OSS avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.