# Trace a C++ Application

Learn how to compile and run the C++ example with
[OpenTelemetry](https://opentelemetry.io/)
and export the traces to [Cloud Trace](https://docs.cloud.google.com/trace/docs/overview)
This example uses the Google Cloud Pub/Sub C++ client to publish 5 messages and
exports the traces to Cloud Trace.

## Before you begin

<br />

## Set up

1. Create a topic with the ID `my-topic`:

       gcloud pubsub topics create my-topic

2. Checkout the C++ sample source code:

       git clone --depth 1 https://github.com/GoogleCloudPlatforms/cpp-samples

## Publish messages

    // Create a few namespace aliases to make the code easier to read.
    namespace gc = ::google::cloud;
    namespace otel = gc::otel;
    namespace pubsub = 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.
    auto project = gc::Project(project_id);
    auto configuration = otel::ConfigureBasicTracing(project);

    auto publisher = 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 (int i = 0; i < 5; i++) {
      auto id = publisher.Publish(pubsub::MessageBuilder().SetData("Hi!").Build())
                    .then([](gc::future<gc::StatusOr<std::string>> f) {
                      auto id = 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();

1. Compile and run the example:

       cd cpp-samples/pubsub-open-telemetry
       bazel run //:quickstart -- $(gcloud config get project) my-topic

2. 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
![](https://docs.cloud.google.com/static/stackdriver/images/trace-explorer-icon.png)
**Trace explorer** page:

[Go to **Trace explorer**](https://console.cloud.google.com/traces/explorer)

<br />

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.

1. Delete the topic created by the example:

       gcloud pubsub topics delete my-topic

## What's next

- Learn more about [C++ and OpenTelemetry](https://docs.cloud.google.com/trace/docs/setup/cpp-ot).
- Find more [C++ examples](https://docs.cloud.google.com/docs/samples/?language=cpp).
- Learn more about [Pub/Sub APIs](https://docs.cloud.google.com/pubsub/docs/reference/service_apis_overview).
- Try more [C++ Pub/Sub OpenTelemetry Examples](https://github.com/GoogleCloudPlatform/cpp-samples/tree/main/pubsub-open-telemetry).