מעקב אחר אפליקציית C++‎

במאמר הזה נסביר איך לקמפל ולהריץ את הדוגמה של C++‎ באמצעות OpenTelemetry ולייצא את העקבות אל Cloud Trace. בדוגמה הזו נעשה שימוש בלקוח Google Cloud Pub/Sub C++‎ כדי לפרסם 5 הודעות ולייצא את העקבות אל Cloud Trace.

לפני שמתחילים

  1. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Enable the Pub/Sub and Trace APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  4. התקינו את ה-CLI של Google Cloud.

  5. הגדירו שה-CLI של gcloud ישתמש בזהות המאוחדת שלכם.

    איך נכנסים ל-CLI של gcloud באמצעות הזהות המאוחדת?

  6. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init

הגדרה

  1. תכין לי נושא עם המזהה my-topic:

    gcloud pubsub topics create my-topic
    
  2. בודקים את קוד המקור לדוגמה של C++‎:

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

פרסום הודעות

// 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. קומפילציה והרצה של הדוגמה:

    cd cpp-samples/pubsub-open-telemetry
    bazel run //:quickstart -- $(gcloud config get project) my-topic
    
  2. אחרי שמריצים את הדוגמה הזו, השורות הבאות מודפסות במסוף:

    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)
    

צפייה בפרטי ההעברה

נכנסים לדף Trace explorer במסוף Google Cloud :

כניסה אל Trace explorer

אפשר גם להשתמש בסרגל החיפוש כדי למצוא את הדף הזה.

הסרת המשאבים

כדי לא לצבור חיובים לחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, פועלים לפי השלבים הבאים:

  1. כדי למחוק את הנושא שנוצר בדוגמה:

    gcloud pubsub topics delete my-topic
    

המאמרים הבאים