Rastrear um aplicativo em C++

Aprender a compilar e executar o exemplo em C++ com o OpenTelemetry e exportar os traces para o Cloud Trace Esse exemplo usa o cliente C++ do Google Cloud Pub/Sub para publicar cinco mensagens e exportar os traces para o Cloud Trace.

Antes de começar

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verifique se a cobrança está ativada para o seu projeto do Google Cloud.

  4. Enable the Pub/Sub and Trace APIs.

    Enable the APIs

  5. Install the Google Cloud CLI.
  6. To initialize the gcloud CLI, run the following command:

    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. Verifique se a cobrança está ativada para o seu projeto do Google Cloud.

  9. Enable the Pub/Sub and Trace APIs.

    Enable the APIs

  10. Install the Google Cloud CLI.
  11. To initialize the gcloud CLI, run the following command:

    gcloud init

Configurar

  1. Crie um tópico com o ID my-topic:

    gcloud pubsub topics create my-topic
    
  2. Confira o código-fonte de exemplo em C++:

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

Publique mensagens

// 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 e execute o exemplo:

    cd cpp-samples/pubsub-open-telemetry
    bazel run //:quickstart -- $(gcloud config get project) my-topic
    
  2. Depois de executar este exemplo, você verá as seguintes linhas impressas no 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)
    

Como visualizar os traces

Na barra de pesquisa do console do Google Cloud, digite Trace Overview. Nos resultados da pesquisa, acesse a seção Produtos e páginas e selecione a página Visão geral para o Trace:

Acesse Visão geral do trace

Limpar

Para evitar cobranças na sua conta do Google Cloud pelos recursos usados nesta página, siga estas etapas.

  1. Exclua o tópico criado pelo exemplo:

    gcloud pubsub topics delete my-topic
    

A seguir