Skip to content

Commit

Permalink
impl(pubsub-otel): add unary pull example (#301)
Browse files Browse the repository at this point in the history
* impl(pubsub-otel): add unary pull example

* Update pubsub-open-telemetry/unary_pull_subscriber.cc

Co-authored-by: Carlos O'Ryan <coryan@google.com>

* Update pubsub-open-telemetry/unary_pull_subscriber.cc

Co-authored-by: Carlos O'Ryan <coryan@google.com>

---------

Co-authored-by: Carlos O'Ryan <coryan@google.com>
  • Loading branch information
alevenberg and coryan committed Jan 5, 2024
1 parent c61db90 commit 205c805
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pubsub-open-telemetry/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
6 changes: 6 additions & 0 deletions pubsub-open-telemetry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
109 changes: 109 additions & 0 deletions pubsub-open-telemetry/subscriber.md
Original file line number Diff line number Diff line change
@@ -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.

<!--- TODO(#299): Add screenshots
#### With OTel ABI 1.0
#### Receive trace
![Screenshot of the receive span in the Cloud Trace UI.](assets/receive_span.png)
#### Settle trace
![Screenshot of the settle span in the Cloud Trace UI.](assets/settle_span.png)
#### Extend trace
![Screenshot of the extend span in the Cloud Trace UI.](assets/extend_span.png)
#### With OTel ABI 2.0
#### Receive trace
![Screenshot of the receive span in the Cloud Trace UI.](assets/otel2/receive_span.png)
#### Settle trace
![Screenshot of the settle span in the Cloud Trace UI.](assets/otel2/settle_span.png)
#### Extend trace
![Screenshot of the extend span in the Cloud Trace UI.](assets/otel2/extend_span.png)
-->

### 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}
```
65 changes: 65 additions & 0 deletions pubsub-open-telemetry/unary_pull_subscriber.cc
Original file line number Diff line number Diff line change
@@ -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 <iostream>

int main(int argc, char* argv[]) try {
if (argc != 4) {
std::cerr << "Usage: " << argv[0]
<< " <project-id> <topic-id> <subscription-id>\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<gc::OpenTelemetryTracingOption>(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<gc::OpenTelemetryTracingOption>(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;
}

0 comments on commit 205c805

Please sign in to comment.