Trace un'applicazione C++

Scopri come compilare ed eseguire l'esempio C++ con OpenTelemetry ed esportare le tracce in Cloud Trace. In questo esempio viene utilizzato il client C++ Pub/Sub di Google Cloud per pubblicare 5 messaggi ed esportare le tracce in Cloud Trace.

Prima di iniziare

  1. Accedi al tuo account Google Cloud. Se non conosci Google Cloud, crea un account per valutare le prestazioni dei nostri prodotti in scenari reali. I nuovi clienti ricevono anche 300 $di crediti gratuiti per l'esecuzione, il test e il deployment dei carichi di lavoro.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Assicurati che la fatturazione sia attivata per il tuo progetto Google Cloud.

  4. Abilita le API Pub/Sub and Trace.

    Abilita le API

  5. Installa Google Cloud CLI.
  6. Per initialize gcloud CLI, esegui questo comando:

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  8. Assicurati che la fatturazione sia attivata per il tuo progetto Google Cloud.

  9. Abilita le API Pub/Sub and Trace.

    Abilita le API

  10. Installa Google Cloud CLI.
  11. Per initialize gcloud CLI, esegui questo comando:

    gcloud init

Impostazione

  1. Crea un argomento con ID my-topic:

    gcloud pubsub topics create my-topic
    
  2. Verifica il codice sorgente di esempio C++:

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

pubblica dei messaggi

// 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. Compila ed esegui l'esempio:

    cd cpp-samples/pubsub-open-telemetry
    bazel run //:quickstart -- $(gcloud config get project) my-topic
    
  2. Dopo aver eseguito questo esempio, vedrai le seguenti righe stampate nella 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)
    

Visualizzazione delle tracce

Nella barra di ricerca della console Google Cloud, inserisci Trace Overview (Panoramica di Trace). Nei risultati di ricerca, vai alla sezione Prodotti e pagine e seleziona la pagina Panoramica per Trace:

Vai a Panoramica di Trace

Esegui la pulizia

Per evitare che al tuo account Google Cloud vengano addebitati costi relativi alle risorse utilizzate in questa pagina, segui questi passaggi.

  1. Elimina l'argomento creato dall'esempio:

    gcloud pubsub topics delete my-topic
    

Passaggi successivi