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.
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 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.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloudinit
-
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. 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.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloudinit
Set up
Create a topic with the ID
my-topic:gcloudpubsubtopicscreatemy-topicCheckout the C++ sample source code:
gitclone--depth1https://github.com/GoogleCloudPlatforms/cpp-samples
Publish messages
// Create a few namespace aliases to make the code easier to read.
namespacegc=::google::cloud;
namespaceotel=gc::otel;
namespacepubsub=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.
autoproject=gc::Project(project_id);
autoconfiguration=otel::ConfigureBasicTracing(project);
autopublisher=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(inti=0;i < 5;i++){
autoid=publisher.Publish(pubsub::MessageBuilder().SetData("Hi!").Build())
.then([](gc::future<gc::StatusOr<std::string>>f){
autoid=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();Compile and run the example:
cdcpp-samples/pubsub-open-telemetry bazelrun//:quickstart--$(gcloudconfiggetproject)my-topicAfter running this example, you'll see the following lines printed to console.
Sentmessagewithid:(9095112996778043) Sentmessagewithid:(9095112996778044) Sentmessagewithid:(9095112996778045) Sentmessagewithid:(9095112996778046) Sentmessagewithid:(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:
gcloudpubsubtopicsdeletemy-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.