入门指南

欢迎使用OpenTelemetry C++入门指南!本指南将为您提供安装、仪表化、配置和导出OpenTelemetry数据的基本步骤。

您可以使用CMakeBazel构建OpenTelemetry C++。下面的入门指南将使用CMake,并提供最基本的步骤,以便您拥有一个可工作的示例应用程序(一个HTTP服务器和HTTP客户端)。要了解更多详细信息,请阅读这些说明

先决条件

您可以在Windows、macOS或Linux上构建OpenTelemetry C++。首先,您需要安装一些依赖项:

sudo apt-get install git cmake g++ libcurl4-openssl-dev
sudo yum install git cmake g++ libcurl-devel
sudo apk add git cmake g++ make curl-dev
xcode-select —install
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install git cmake

构建

获取opentelemetry-cpp源代码:

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

导航到上面克隆的存储库,并创建CMake构建配置:

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

创建构建配置后,构建CMake目标http_clienthttp_server

cmake --build . --target http_client http_server

如果一切顺利,您应该在./examples/http目录下找到http_serverhttp_client二进制文件:

$ ls ./examples/http
CMakeFiles  Makefile  cmake_install.cmake  http_client  http_server

运行应用程序

开启两个终端,首先在第一个终端中启动HTTP服务器:

$ ./examples/http/http_server
Server is running..Press ctrl-c to exit..

然后在另一个终端中运行HTTP客户端:

./examples/http/http_client

您应该看到类似于以下内容的客户端输出:

{
  name          : /helloworld
  trace_id      : 05eec7a55d3544434265dad89d7fe96f
  span_id       : 45fb62c58c907f05
  tracestate    :
  parent_span_id: 0000000000000000
  start         : 1665577080650384378
  duration      : 1640298
  description   :
  span kind     : Client
  status        : Unset
  attributes    :
    http.header.Date: Wed, 12 Oct 2022 12:18:00 GMT
    http.header.Content-Length: 0
    http.status_code: 200
    http.method: GET
    .header.Host: localhost
    http.header.Content-Type: text/plain
    http.header.Connection: keep-alive
    .scheme: http
    http.url: http://localhost:8800/helloworld
  events        :
  links         :
  resources     :
    service.name: unknown_service
    telemetry.sdk.version: 1.6.1
    telemetry.sdk.name: opentelemetry
    telemetry.sdk.language: cpp
  instr-lib     : http-client
}

同时,服务器应该在控制台上输出追踪信息:

{
  name          : /helloworld
  trace_id      : 05eec7a55d3544434265dad89d7fe96f
  span_id       : 8df967d8547813fe
  tracestate    :
  parent_span_id: 45fb62c58c907f05
  start         : 1665577080651459495
  duration      : 46331
  description   :
  span kind     : Server
  status        : Unset
  attributes    :
    http.header.Traceparent: 00-05eec7a55d3544434265dad89d7fe96f-45fb62c58c907f05-01
    http.header.Accept: */*
    http.request_content_length: 0
    http.header.Host: localhost:8800
    http.scheme: http
    http.client_ip: 127.0.0.1:49466
    http.method: GET
    net.host.port: 8800
    net.host.name: localhost
  events        :
    {
      name          : Processing request
      timestamp     : 1665577080651472827
      attributes    :
    }
  links         :
  resources     :
    service.name: unknown_service
    telemetry.sdk.version: 1.6.1
    telemetry.sdk.name: opentelemetry
    telemetry.sdk.language: cpp
  instr-lib     : http-server
}

接下来要做什么

使用手动方式为自动生成的仪表化添加自定义代码库的详细信息。这将为您提供自定义的可观察性数据。

您还需要配置合适的导出器,将遥测数据导出到一个或多个遥测后端。

如果您想探索更复杂的示例,请查看OpenTelemetry演示,其中包括基于C++的Currency Service

最后修改 December 10, 2023: translate (a4350d6e)