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
- 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
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.