跟踪 C++ 应用
了解如何使用 OpenTelemetry 编译和运行 C++ 示例,并将跟踪记录导出到 Cloud Trace。此示例使用 Google Cloud Pub/Sub C++ 客户端发布了 5 条消息,并将跟踪记录导出到 Cloud Trace。
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Pub/Sub and Trace APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Pub/Sub and Trace APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
设置
创建 ID 为
my-topic
的主题:gcloud pubsub topics create my-topic
查看 C++ 示例源代码:
git clone --depth 1 https://github.com/GoogleCloudPlatforms/cpp-samples
发布消息
编译并运行示例:
cd cpp-samples/pubsub-open-telemetry bazel run //:quickstart -- $(gcloud config get project) my-topic
运行此示例后,您会看到控制台输出了以下几行内容。
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 的概览页面:
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
删除示例创建的主题:
gcloud pubsub topics delete my-topic
后续步骤
- 详细了解 C++ 和 OpenTelemetry。
- 查找更多 C++ 示例。
- 详细了解 Pub/Sub API。
- 试用更多 C++ Pub/Sub OpenTelemetry 示例。