| 22 | #include <vector> |
| 23 | |
| 24 | int main(int argc, char* argv[]) try { |
| 25 | if (argc != 3) { |
| 26 | std::cerr << "Usage: " << argv[0] << " <project-id> <topic-id>\n"; |
| 27 | return 1; |
| 28 | } |
| 29 | std::string const project_id = argv[1]; |
| 30 | std::string const topic_id = argv[2]; |
| 31 | |
| 32 | //! [START pubsub_publish_otel_tracing] |
| 33 | // Create a few namespace aliases to make the code easier to read. |
| 34 | namespace gc = ::google::cloud; |
| 35 | namespace otel = gc::otel; |
| 36 | namespace pubsub = gc::pubsub; |
| 37 | |
| 38 | // This example uses a simple wrapper to export (upload) OTel tracing data |
| 39 | // to Google Cloud Trace. More complex applications may use different |
| 40 | // authentication, or configure their own OTel exporter. |
| 41 | auto project = gc::Project(project_id); |
| 42 | auto configuration = otel::ConfigureBasicTracing(project); |
| 43 | |
| 44 | auto publisher = pubsub::Publisher(pubsub::MakePublisherConnection( |
| 45 | pubsub::Topic(project_id, topic_id), |
| 46 | // Configure this publisher to enable OTel tracing. Some applications may |
| 47 | // chose to disable tracing in some publishers or to dynamically enable |
| 48 | // this option based on their own configuration. |
| 49 | gc::Options{}.set<gc::OpenTelemetryTracingOption>(true))); |
| 50 | |
| 51 | // After this point, use the Cloud Pub/Sub C++ client library as usual. |
| 52 | // In this example, we will send a few messages and configure a callback |
| 53 | // action for each one. |
| 54 | std::vector<gc::future<void>> ids; |
| 55 | for (int i = 0; i < 5; i++) { |
| 56 | auto id = publisher.Publish(pubsub::MessageBuilder().SetData("Hi!").Build()) |
| 57 | .then([](gc::future<gc::StatusOr<std::string>> f) { |
| 58 | auto id = f.get(); |
| 59 | if (!id) { |
| 60 | std::cout << "Error in publish: " << id.status() << "\n"; |
| 61 | return; |
| 62 | } |
| 63 | std::cout << "Sent message with id: (" << *id << ")\n"; |
| 64 | }); |
| 65 | ids.push_back(std::move(id)); |
| 66 | } |
| 67 | // Block until the messages are actually sent. |
| 68 | for (auto& id : ids) id.get(); |
| 69 | //! [END pubsub_publish_otel_tracing] |
| 70 | |
| 71 | return 0; |
| 72 | } catch (google::cloud::Status const& status) { |
| 73 | std::cerr << "google::cloud::Status thrown: " << status << "\n"; |
| 74 | return 1; |
| 75 | } |
nothing calls this directly
no outgoing calls
no test coverage detected