| 22 | #include <iostream> |
| 23 | |
| 24 | int main(int argc, char* argv[]) try { |
| 25 | if (argc != 4) { |
| 26 | std::cerr << "Usage: " << argv[0] |
| 27 | << " <project-id> <topic-id> <subscription-id>\n"; |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | std::string const project_id = argv[1]; |
| 32 | std::string const topic_id = argv[2]; |
| 33 | std::string const subscription_id = argv[3]; |
| 34 | |
| 35 | // Create a few namespace aliases to make the code easier to read. |
| 36 | namespace gc = ::google::cloud; |
| 37 | namespace otel = gc::otel; |
| 38 | namespace pubsub = gc::pubsub; |
| 39 | |
| 40 | auto constexpr kWaitTimeout = std::chrono::seconds(30); |
| 41 | |
| 42 | auto project = gc::Project(project_id); |
| 43 | auto configuration = otel::ConfigureBasicTracing(project); |
| 44 | |
| 45 | // Publish a message with tracing enabled. |
| 46 | auto publisher = pubsub::Publisher(pubsub::MakePublisherConnection( |
| 47 | pubsub::Topic(project_id, topic_id), |
| 48 | gc::Options{}.set<gc::OpenTelemetryTracingOption>(true))); |
| 49 | // Block until the message is actually sent and throw on error. |
| 50 | auto id = publisher.Publish(pubsub::MessageBuilder().SetData("Hi!").Build()) |
| 51 | .get() |
| 52 | .value(); |
| 53 | std::cout << "Sent message with id: (" << id << ")\n"; |
| 54 | |
| 55 | // Receive a message using streaming pull with tracing enabled. |
| 56 | auto subscriber = pubsub::Subscriber(pubsub::MakeSubscriberConnection( |
| 57 | pubsub::Subscription(project_id, subscription_id), |
| 58 | gc::Options{}.set<gc::OpenTelemetryTracingOption>(true))); |
| 59 | |
| 60 | auto session = |
| 61 | subscriber.Subscribe([&](pubsub::Message const& m, pubsub::AckHandler h) { |
| 62 | std::cout << "Received message " << m << "\n"; |
| 63 | std::move(h).ack(); |
| 64 | }); |
| 65 | |
| 66 | std::cout << "Waiting for messages on " + subscription_id + "...\n"; |
| 67 | |
| 68 | // Blocks until the timeout is reached. |
| 69 | auto result = session.wait_for(kWaitTimeout); |
| 70 | if (result == std::future_status::timeout) { |
| 71 | std::cout << "timeout reached, ending session\n"; |
| 72 | session.cancel(); |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } catch (google::cloud::Status const& status) { |
| 77 | std::cerr << "google::cloud::Status thrown: " << status << "\n"; |
| 78 | return 1; |
| 79 | } |
| 80 | //! [END pubsub_subscribe_otel_tracing] |
nothing calls this directly
no outgoing calls
no test coverage detected