追蹤 C++ 應用程式
瞭解如何使用 OpenTelemetry 編譯及執行 C++ 範例,並將追蹤記錄匯出至 Cloud Trace。這個範例會使用 Google Cloud Pub/Sub C++ 用戶端發布 5 則訊息,並將追蹤記錄匯出至 Cloud Trace。
事前準備
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Cloud de Confiance project.
Enable the Pub/Sub and Trace APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
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)
查看追蹤記錄
前往 Cloud de Confiance 控制台的
「Trace Explorer」頁面:
前往「Trace explorer」(Trace 探索工具)
您也可以透過搜尋列找到這個頁面。
清除所用資源
為了避免系統向您的 Cloud de Confiance 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
刪除範例建立的主題:
gcloud pubsub topics delete my-topic
後續步驟
- 進一步瞭解 C++ 和 OpenTelemetry。
- 如需更多 C++ 範例,請參閱這篇文章。
- 進一步瞭解 Pub/Sub API。
- 試試更多 C++ Pub/Sub OpenTelemetry 範例。