From 205c805bf5331afdfe78754a97662fe7f6db4bfe Mon Sep 17 00:00:00 2001 From: Anna Levenberg Date: Fri, 5 Jan 2024 15:48:14 -0500 Subject: [PATCH] impl(pubsub-otel): add unary pull example (#301) * impl(pubsub-otel): add unary pull example * Update pubsub-open-telemetry/unary_pull_subscriber.cc Co-authored-by: Carlos O'Ryan * Update pubsub-open-telemetry/unary_pull_subscriber.cc Co-authored-by: Carlos O'Ryan --------- Co-authored-by: Carlos O'Ryan --- pubsub-open-telemetry/BUILD.bazel | 9 ++ pubsub-open-telemetry/CMakeLists.txt | 6 + pubsub-open-telemetry/subscriber.md | 109 ++++++++++++++++++ .../unary_pull_subscriber.cc | 65 +++++++++++ 4 files changed, 189 insertions(+) create mode 100644 pubsub-open-telemetry/subscriber.md create mode 100644 pubsub-open-telemetry/unary_pull_subscriber.cc diff --git a/pubsub-open-telemetry/BUILD.bazel b/pubsub-open-telemetry/BUILD.bazel index 93735cc..f85c9e0 100644 --- a/pubsub-open-telemetry/BUILD.bazel +++ b/pubsub-open-telemetry/BUILD.bazel @@ -67,3 +67,12 @@ cc_binary( "@google_cloud_cpp//:pubsub", ], ) + +cc_binary( + name = "unary_pull_subscriber", + srcs = ["unary_pull_subscriber.cc"], + deps = [ + "@google_cloud_cpp//:opentelemetry", + "@google_cloud_cpp//:pubsub", + ], +) diff --git a/pubsub-open-telemetry/CMakeLists.txt b/pubsub-open-telemetry/CMakeLists.txt index d2f83cf..ca3dcdb 100644 --- a/pubsub-open-telemetry/CMakeLists.txt +++ b/pubsub-open-telemetry/CMakeLists.txt @@ -63,3 +63,9 @@ add_executable(quickstart quickstart.cc) target_compile_features(quickstart PRIVATE cxx_std_14) target_link_libraries(quickstart PRIVATE google-cloud-cpp::pubsub google-cloud-cpp::opentelemetry) + +add_executable(unary_pull_subscriber unary_pull_subscriber.cc) +target_compile_features(unary_pull_subscriber PRIVATE cxx_std_14) +target_link_libraries( + unary_pull_subscriber PRIVATE google-cloud-cpp::pubsub + google-cloud-cpp::opentelemetry) diff --git a/pubsub-open-telemetry/subscriber.md b/pubsub-open-telemetry/subscriber.md new file mode 100644 index 0000000..135921f --- /dev/null +++ b/pubsub-open-telemetry/subscriber.md @@ -0,0 +1,109 @@ +# Subscriber + +To try receiving a message using unary pull, run the `unary_pull_subscriber` +application. It publishes a message to a topic and then pulls the same message +from a subscription, and then exports the spans to cloud trace. + +**Note**: OTel ABI 2.0 adds the ability to add links after span creation. If an +application is compiled with OTel ABI 2.0, it will produce different telemetery +data. We currently do not support OTel ABI 2.0 with CMake. + +For setup instructions, refer to the [README.md](README.md). + +## Example traces + +To find the traces, navigate to the Cloud Trace UI. + + + +### Setup + +### Create the Cloud Pub/Sub subscription attached to a topic + +If you don't already have them, create a topic and a subscription with pull +delivery. + +Export the following environment variables: + +```sh +export=GOOGLE_CLOUD_PROJECT=[PROJECT-ID] +export=GOOGLE_CLOUD_SUBSCRIPTION=[SUBSCRIPTION-ID] +export=GOOGLE_CLOUD_TOPIC=[TOPIC-ID] +``` + +Use the CLI to create the resources: + +```sh +gcloud pubsub topics create "--project=${GOOGLE_CLOUD_PROJECT}" ${GOOGLE_CLOUD_TOPIC} +gcloud pubsub subscriptions create "--project=${GOOGLE_CLOUD_PROJECT}" "--topic=${GOOGLE_CLOUD_TOPIC}" ${GOOGLE_CLOUD_SUBSCRIPTION} +``` + +### Publish a message + +Make sure you publish a message with tracing enabled. If not, the traces will +not be linked. + +## Build and run + +### Using CMake and Vcpkg + +```sh +cd cpp-samples/pubsub-open-telemetry +cmake -S . -B .build -DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake -G Ninja +cmake --build .build --target unary_pull_subscriber +``` + +#### Run the subscriber with the unary pull + +```shell +.build/unary_pull_subscriber ${GOOGLE_CLOUD_PROJECT} ${GOOGLE_CLOUD_TOPIC} ${GOOGLE_CLOUD_SUBSCRIPTION} +``` + +## Build and run using Bazel + +### 1. Download or clone this repo + +```shell +git clone https://github.com/GoogleCloudPlatform/cpp-samples +``` + +### 2. Compile and run these examples + +```shell +cd cpp-samples/pubsub-open-telemetry +bazel run //:unary_pull_subscriber -- ${GOOGLE_CLOUD_PROJECT} ${GOOGLE_CLOUD_TOPIC} ${GOOGLE_CLOUD_SUBSCRIPTION} +``` + +#### Run the example with otel ABI 2.0 + +```shell +bazel run --config=otel2 //:unary_pull_subscriber -- ${GOOGLE_CLOUD_PROJECT} ${GOOGLE_CLOUD_TOPIC} ${GOOGLE_CLOUD_SUBSCRIPTION} +``` diff --git a/pubsub-open-telemetry/unary_pull_subscriber.cc b/pubsub-open-telemetry/unary_pull_subscriber.cc new file mode 100644 index 0000000..eec91d2 --- /dev/null +++ b/pubsub-open-telemetry/unary_pull_subscriber.cc @@ -0,0 +1,65 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "google/cloud/opentelemetry/configure_basic_tracing.h" +#include "google/cloud/opentelemetry_options.h" +#include "google/cloud/pubsub/message.h" +#include "google/cloud/pubsub/publisher.h" +#include "google/cloud/pubsub/subscriber.h" +#include "google/cloud/pubsub/subscription.h" +#include + +int main(int argc, char* argv[]) try { + if (argc != 4) { + std::cerr << "Usage: " << argv[0] + << " \n"; + return 1; + } + + std::string const project_id = argv[1]; + std::string const topic_id = argv[2]; + std::string const subscription_id = argv[3]; + + // Create a few namespace aliases to make the code easier to read. + namespace gc = ::google::cloud; + namespace otel = gc::otel; + namespace pubsub = gc::pubsub; + + auto project = gc::Project(project_id); + auto configuration = otel::ConfigureBasicTracing(project); + + // Publish a message with tracing enabled. + auto publisher = pubsub::Publisher(pubsub::MakePublisherConnection( + pubsub::Topic(project_id, topic_id), + gc::Options{}.set(true))); + // Block until the message is actually sent and throw on error. + auto id = publisher.Publish(pubsub::MessageBuilder().SetData("Hi!").Build()) + .get() + .value(); + std::cout << "Sent message with id: (" << id << ")\n"; + + // Receive a message using unary pull with tracing enabled. + auto subscriber = pubsub::Subscriber(pubsub::MakeSubscriberConnection( + pubsub::Subscription(project_id, subscription_id), + gc::Options{}.set(true))); + + auto response = subscriber.Pull().value(); + std::cout << "Received message " << response->message << "\n"; + std::move(response->handler).ack(); + + return 0; +} catch (google::cloud::Status const& status) { + std::cerr << "google::cloud::Status thrown: " << status << "\n"; + return 1; +}