MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/cpp-samples / main

Function main

pubsub-open-telemetry/unary_pull_subscriber.cc:23–65  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21#include <iostream>
22
23int main(int argc, char* argv[]) try {
24 if (argc != 4) {
25 std::cerr << "Usage: " << argv[0]
26 << " <project-id> <topic-id> <subscription-id>\n";
27 return 1;
28 }
29
30 std::string const project_id = argv[1];
31 std::string const topic_id = argv[2];
32 std::string const subscription_id = argv[3];
33
34 // Create a few namespace aliases to make the code easier to read.
35 namespace gc = ::google::cloud;
36 namespace otel = gc::otel;
37 namespace pubsub = gc::pubsub;
38
39 auto project = gc::Project(project_id);
40 auto configuration = otel::ConfigureBasicTracing(project);
41
42 // Publish a message with tracing enabled.
43 auto publisher = pubsub::Publisher(pubsub::MakePublisherConnection(
44 pubsub::Topic(project_id, topic_id),
45 gc::Options{}.set<gc::OpenTelemetryTracingOption>(true)));
46 // Block until the message is actually sent and throw on error.
47 auto id = publisher.Publish(pubsub::MessageBuilder().SetData("Hi!").Build())
48 .get()
49 .value();
50 std::cout << "Sent message with id: (" << id << ")\n";
51
52 // Receive a message using unary pull with tracing enabled.
53 auto subscriber = pubsub::Subscriber(pubsub::MakeSubscriberConnection(
54 pubsub::Subscription(project_id, subscription_id),
55 gc::Options{}.set<gc::OpenTelemetryTracingOption>(true)));
56
57 auto response = subscriber.Pull().value();
58 std::cout << "Received message " << response.message << "\n";
59 std::move(response.handler).ack();
60
61 return 0;
62} catch (google::cloud::Status const& status) {
63 std::cerr << "google::cloud::Status thrown: " << status << "\n";
64 return 1;
65}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected