Trace a C++ Application
Learn how to compile and run the C++ example with OpenTelemetry and export the traces to Cloud Trace This example uses the Google Cloud Pub/Sub C++ client to publish 5 messages and exports the traces to Cloud Trace.
Before you begin
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Pub/Sub and Trace APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. 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
Set up
Create a topic with the ID
my-topic
:gcloud pubsub topics create my-topic
Checkout the C++ sample source code:
git clone --depth 1 https://github.com/GoogleCloudPlatforms/cpp-samples
Publish messages
Compile and run the example:
cd cpp-samples/pubsub-open-telemetry bazel run //:quickstart -- $(gcloud config get project) my-topic
After running this example, you'll see the following lines printed to console.
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)
View traces
In the Google Cloud console, go to the Trace explorer page:
You can also find this page by using the search bar.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
Delete the topic created by the example:
gcloud pubsub topics delete my-topic
What's next
- Learn more about C++ and OpenTelemetry.
- Find more C++ examples.
- Learn more about Pub/Sub APIs.
- Try more C++ Pub/Sub OpenTelemetry Examples.