C++ 애플리케이션 추적

OpenTelemetry를 사용하여 C++ 예시를 컴파일하고 실행하는 방법을 알아보고 trace를 Cloud Trace로 내보내는 방법을 보여줍니다. 이 예시에서는 Google Cloud Pub/Sub C++ 클라이언트를 사용하여 메시지 5개를 게시하고 Cloud Trace로 trace를 내보냅니다.

시작하기 전에

  1. Google Cloud 계정에 로그인합니다. Google Cloud를 처음 사용하는 경우 계정을 만들고 Google 제품의 실제 성능을 평가해 보세요. 신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
  2. Google Cloud Console의 프로젝트 선택기 페이지에서 Google Cloud 프로젝트를 선택하거나 만듭니다.

    프로젝트 선택기로 이동

  3. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

  4. API Pub/Sub and Trace 사용 설정

    API 사용 설정

  5. Google Cloud CLI를 설치합니다.
  6. gcloud CLI를 초기화하려면 다음 명령어를 실행합니다.

    gcloud init
  7. Google Cloud Console의 프로젝트 선택기 페이지에서 Google Cloud 프로젝트를 선택하거나 만듭니다.

    프로젝트 선택기로 이동

  8. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

  9. API Pub/Sub and Trace 사용 설정

    API 사용 설정

  10. Google Cloud CLI를 설치합니다.
  11. gcloud CLI를 초기화하려면 다음 명령어를 실행합니다.

    gcloud init

설정

  1. ID가 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 보기

Google Cloud 콘솔 검색창에 Trace 개요를 입력합니다. 검색 결과에서 제품 및 페이지 섹션으로 이동한 후 Trace에 대한 개요 페이지를 선택합니다.

Trace 개요로 이동

삭제

이 페이지에서 사용한 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 다음 단계를 수행합니다.

  1. 예시에서 만든 주제를 삭제합니다.

    gcloud pubsub topics delete my-topic
    

다음 단계