跟踪 C++ 应用

了解如何使用 OpenTelemetry 编译和运行 C++ 示例,并将跟踪记录导出到 Cloud Trace。此示例使用 Google Cloud Pub/Sub C++ 客户端发布了 5 条消息,并将跟踪记录导出到 Cloud Trace。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Pub/Sub and Trace API。

    启用 API

  5. 安装 Google Cloud CLI。
  6. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  7. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  8. 确保您的 Google Cloud 项目已启用结算功能

  9. 启用 Pub/Sub and Trace API。

    启用 API

  10. 安装 Google Cloud CLI。
  11. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init

设置

  1. 创建 ID 为 my-topic 的主题:

    gcloud pubsub topics create my-topic
    
  2. 查看 C++ 示例源代码:

    git clone --depth 1 https://github.com/GoogleCloudPlatforms/cpp-samples
    

发布消息

// Create a few namespace aliases to make the code easier to read.
namespace gc = ::google::cloud;
namespace otel = gc::otel;
namespace pubsub = gc::pubsub;

// This example uses a simple wrapper to export (upload) OTel tracing data
// to Google Cloud Trace. More complex applications may use different
// authentication, or configure their own OTel exporter.
auto project = gc::Project(project_id);
auto configuration = otel::ConfigureBasicTracing(project);

auto publisher = pubsub::Publisher(pubsub::MakePublisherConnection(
    pubsub::Topic(project_id, topic_id),
    // Configure this publisher to enable OTel tracing. Some applications may
    // chose to disable tracing in some publishers or to dynamically enable
    // this option based on their own configuration.
    gc::Options{}.set<gc::OpenTelemetryTracingOption>(true)));

// After this point, use the Cloud Pub/Sub C++ client library as usual.
// In this example, we will send a few messages and configure a callback
// action for each one.
std::vector<gc::future<void>> ids;
for (int i = 0; i < 5; i++) {
  auto id = publisher.Publish(pubsub::MessageBuilder().SetData("Hi!").Build())
                .then([](gc::future<gc::StatusOr<std::string>> f) {
                  auto id = f.get();
                  if (!id) {
                    std::cout << "Error in publish: " << id.status() << "\n";
                    return;
                  }
                  std::cout << "Sent message with id: (" << *id << ")\n";
                });
  ids.push_back(std::move(id));
}
// Block until the messages  are actually sent.
for (auto& id : ids) id.get();
  1. 编译并运行示例:

    cd cpp-samples/pubsub-open-telemetry
    bazel run //:quickstart -- $(gcloud config get project) my-topic
    
  2. 运行此示例后,您会看到控制台输出了以下几行内容。

    Sent message with id: (9095112996778043)
    Sent message with id: (9095112996778044)
    Sent message with id: (9095112996778045)
    Sent message with id: (9095112996778046)
    Sent message with id: (9095112996778047)
    

查看跟踪记录

在 Google Cloud 控制台搜索栏中,输入 Trace 概览。在搜索结果中,转到产品和页面部分,然后选择 Trace概览页面:

转到 Trace 概览

清理

为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

  1. 删除示例创建的主题:

    gcloud pubsub topics delete my-topic
    

后续步骤