From 126849385846731fdd38b14dbb16b61b7b21437f Mon Sep 17 00:00:00 2001 From: Anna Levenberg Date: Tue, 5 Dec 2023 17:37:51 -0500 Subject: [PATCH] respond to comments --- pubsub-open-telemetry/CMakeLists.txt | 7 ++- pubsub-open-telemetry/README.md | 72 --------------------- pubsub-open-telemetry/publisher.cc | 17 +++-- pubsub-open-telemetry/publisher.md | 77 +++++++++++++++++++++++ pubsub-open-telemetry/publisher_helper.cc | 15 +++++ pubsub-open-telemetry/publisher_helper.h | 9 ++- pubsub-open-telemetry/vcpkg.json | 3 + 7 files changed, 116 insertions(+), 84 deletions(-) create mode 100644 pubsub-open-telemetry/publisher.md diff --git a/pubsub-open-telemetry/CMakeLists.txt b/pubsub-open-telemetry/CMakeLists.txt index 7dca866..2173590 100644 --- a/pubsub-open-telemetry/CMakeLists.txt +++ b/pubsub-open-telemetry/CMakeLists.txt @@ -24,16 +24,17 @@ project(pubsub-open-telemetry CXX) find_package(google_cloud_cpp_pubsub CONFIG REQUIRED) find_package(google_cloud_cpp_opentelemetry CONFIG REQUIRED) find_package(Boost 1.66 REQUIRED COMPONENTS program_options) +find_package(opentelemetry-cpp CONFIG REQUIRED) -add_library(parse_args STATIC parse_args.cc parse_args.h) +add_library(parse_args parse_args.cc parse_args.h) target_compile_features(parse_args PUBLIC cxx_std_14) target_link_libraries( parse_args PUBLIC Boost::program_options google-cloud-cpp::pubsub google-cloud-cpp::opentelemetry) -add_library(publisher_helper STATIC publisher_helper.cc publisher_helper.h) +add_library(publisher_helper publisher_helper.cc publisher_helper.h) target_compile_features(publisher_helper PUBLIC cxx_std_14) -target_link_libraries(publisher_helper PUBLIC parse_args) +target_link_libraries(publisher_helper PUBLIC parse_args opentelemetry-cpp::api opentelemetry-cpp::sdk) add_executable(publisher publisher.cc) target_compile_features(publisher PRIVATE cxx_std_14) diff --git a/pubsub-open-telemetry/README.md b/pubsub-open-telemetry/README.md index fd688ae..5953f1d 100644 --- a/pubsub-open-telemetry/README.md +++ b/pubsub-open-telemetry/README.md @@ -21,22 +21,6 @@ To find the traces, navigate to the Cloud Trace UI. For an overview of the Cloud Trace UI, see: [View traces overview]. -### Publisher - -The publisher application lets the user configure a tracing enabled Pub/Sub Publisher client to see how different configuration settings change the produced telemetry data. - -#### Example traces - -To find the traces, navigate to the Cloud Trace UI. - -##### Publish trace - -![Screenshot of the publish span in the Cloud Trace UI running publisher.](assets/publish_span.png) - -##### Create trace - -![Screenshot of the create span in the Cloud Trace UI running publisher.](assets/create_span.png) - ## Prerequisites ### 1. Create a project in the Google Cloud Platform Console @@ -113,54 +97,6 @@ cmake --build .build .build/quickstart [project-name] [topic-id] ``` -#### Run basic publisher examples -```shell -.build/publisher [project-name] [topic-id] -.build/publisher [project-name] [topic-id] -n 1000 -.build/publisher [project-name] [topic-id] --message-size 0 -.build/publisher [project-name] [topic-id] --tracing-rate 0.01 -n 10 -``` - -#### Flow control example -```shell -.build/publisher [project-name] [topic-id] -n 5 --max-pending-messages 2 --publisher-action reject -.build/publisher [project-name] [topic-id] -n 5 --max-pending-messages 2 --publisher-action block -.build/publisher [project-name] [topic-id] -n 5 --max-pending-messages 2 --publisher-action ignore -.build/publisher [project-name] [topic-id] -n 5 --message-size 10 --max-batch-bytes 60 --publisher-action block -``` - -#### Batching example -```shell -.build/publisher [project-name] [topic-id] -n 5 --max-batch-messages 2 --max-hold-time 100 -.build/publisher [project-name] [topic-id] -n 5 --message-size 10 --max-batch-bytes 60 --max-hold-time 1000 -``` - -#### To see all options - -```shell -.build/publisher --help -Usage: .build/publisher -A simple publisher application with Open Telemetery enabled: - -h [ --help ] produce help message - --project-id arg the name of the Google Cloud project - --topic-id arg the name of the Google Cloud topic - --tracing-rate arg (=1) otel::BasicTracingRateOption value - --max-queue-size arg (=0) If set to 0, uses the default tracing - configuration. - -n [ --message-count ] arg (=1) the number of messages to publish - --message-size arg (=1) the desired message payload size - --enable-ordering-keys arg (=0) If set to true, the messages will be sent - with ordering keys. There will be 3 possible - ordering keys and they will be set randomly - --max-pending-messages arg pubsub::MaxPendingMessagesOption value - --max-pending-bytes arg pubsub::MaxPendingBytesOption value - --publisher-action arg pubsub::FullPublisherAction value - (block|ignore|reject) - --max-hold-time arg pubsub::MaxHoldTimeOption value in us - --max-batch-bytes arg pubsub::MaxBatchBytesOption value - --max-batch-messages arg pubsub::MaxBatchMessagesOption value -``` - ## Build and run using Bazel ### 1. Download or clone this repo @@ -183,14 +119,6 @@ bazel build //:quickstart bazel run //:quickstart [project-name] [topic-id] ``` -#### Run basic publisher examples -```shell -bazel run //:publisher [project-name] [topic-id] -bazel run //:publisher -- [project-name] [topic-id] -n 1000 -bazel run //:publisher -- [project-name] [topic-id] --message_size 0 -bazel run //:publisher -- [project-name] [topic-id] --tracing-rate 0.01 -n 10 -``` - #### Run with a local version of google-cloud-cpp ```shell diff --git a/pubsub-open-telemetry/publisher.cc b/pubsub-open-telemetry/publisher.cc index 20d8975..020f33b 100644 --- a/pubsub-open-telemetry/publisher.cc +++ b/pubsub-open-telemetry/publisher.cc @@ -26,16 +26,18 @@ // Create a few namespace aliases to make the code easier to read. namespace gc = ::google::cloud; namespace otel = gc::otel; +namespace trace_sdk = ::opentelemetry::sdk::trace; +namespace trace = ::opentelemetry::trace; -void ConfigureCloudTrace(ParseResult const& args) { +void ConfigureCloudTraceTracer(ParseResult const& args) { auto exporter = otel::MakeTraceExporter(gc::Project(args.project_id)); - opentelemetry::sdk::trace::BatchSpanProcessorOptions span_options; + trace_sdk::BatchSpanProcessorOptions span_options; span_options.max_queue_size = args.max_queue_size; - auto processor = opentelemetry::sdk::trace::BatchSpanProcessorFactory::Create( + auto processor = trace_sdk::BatchSpanProcessorFactory::Create( std::move(exporter), span_options); - auto provider = opentelemetry::sdk::trace::TracerProviderFactory::Create( + auto provider = trace_sdk::TracerProviderFactory::Create( std::move(processor)); - opentelemetry::trace::Provider::SetTracerProvider(std::move(provider)); + trace::Provider::SetTracerProvider(std::move(provider)); } int main(int argc, char* argv[]) try { @@ -46,12 +48,15 @@ int main(int argc, char* argv[]) try { std::cout << "Using project `" << args.project_id << "` and topic `" << args.topic_id << "`\n"; - ConfigureCloudTrace(args); + ConfigureCloudTraceTracer(args); auto publisher = CreatePublisher(args); Publish(publisher, args); + std::cout << "Cleaning up...\n"; + Cleanup(); + return 0; } catch (google::cloud::Status const& status) { std::cerr << "google::cloud::Status thrown: " << status << "\n"; diff --git a/pubsub-open-telemetry/publisher.md b/pubsub-open-telemetry/publisher.md new file mode 100644 index 0000000..c398db0 --- /dev/null +++ b/pubsub-open-telemetry/publisher.md @@ -0,0 +1,77 @@ +# Publisher + +The publisher application lets the user configure a tracing enabled Pub/Sub Publisher client to see how different configuration settings change the produced telemetry data. + +## Example traces + +### Cloud Trace + +To find the traces, navigate to the Cloud Trace UI. + +#### Publish trace + +![Screenshot of the publish span in the Cloud Trace UI running publisher.](assets/publish_span.png) + +#### Create trace + +![Screenshot of the create span in the Cloud Trace UI running publisher.](assets/create_span.png) + +## Build and run + +For setup instructions, refer to the [README.md](README.md). + +### Using CMake and Vcpkg + +#### Run basic publisher examples +```shell +.build/publisher [project-name] [topic-id] +.build/publisher [project-name] [topic-id] -n 1000 +.build/publisher [project-name] [topic-id] --message-size 0 +.build/publisher [project-name] [topic-id] --tracing-rate 0.01 -n 10 +``` + +#### Flow control example +```shell +.build/publisher [project-name] [topic-id] -n 5 --max-pending-messages 2 --publisher-action reject +.build/publisher [project-name] [topic-id] -n 5 --max-pending-messages 2 --publisher-action block +.build/publisher [project-name] [topic-id] -n 5 --max-pending-messages 2 --publisher-action ignore +.build/publisher [project-name] [topic-id] -n 5 --message-size 10 --max-batch-bytes 60 --publisher-action block +``` + +#### Batching example +```shell +.build/publisher [project-name] [topic-id] -n 5 --max-batch-messages 2 --max-hold-time 100 +.build/publisher [project-name] [topic-id] -n 5 --message-size 10 --max-batch-bytes 60 --max-hold-time 1000 +``` + +#### To see all options + +```shell +.build/publisher --help +Usage: .build/publisher +A simple publisher application with Open Telemetery enabled: + -h [ --help ] produce help message + --project-id arg the name of the Google Cloud project + --topic-id arg the name of the Google Cloud topic + --tracing-rate arg (=1) otel::BasicTracingRateOption value + --max-queue-size arg (=2048) set the max queue size for open telemetery + -n [ --message-count ] arg (=1) the number of messages to publish + --message-size arg (=1) the desired message payload size + --max-pending-messages arg pubsub::MaxPendingMessagesOption value + --max-pending-bytes arg pubsub::MaxPendingBytesOption value + --publisher-action arg pubsub::FullPublisherAction value + (block|ignore|reject) + --max-hold-time arg pubsub::MaxHoldTimeOption value in us + --max-batch-bytes arg pubsub::MaxBatchBytesOption value + --max-batch-messages arg pubsub::MaxBatchMessagesOption value +``` + +### Using Bazel + +#### Run basic publisher examples +```shell +bazel run //:publisher [project-name] [topic-id] +bazel run //:publisher -- [project-name] [topic-id] -n 1000 +bazel run //:publisher -- [project-name] [topic-id] --message_size 0 +bazel run //:publisher -- [project-name] [topic-id] --tracing-rate 0.01 -n 10 +``` diff --git a/pubsub-open-telemetry/publisher_helper.cc b/pubsub-open-telemetry/publisher_helper.cc index 0f69814..0b9280b 100644 --- a/pubsub-open-telemetry/publisher_helper.cc +++ b/pubsub-open-telemetry/publisher_helper.cc @@ -13,10 +13,15 @@ // limitations under the License. #include "publisher_helper.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/trace/provider.h" #include #include namespace gc = ::google::cloud; +namespace pubsub = gc::pubsub; +namespace trace_sdk = ::opentelemetry::sdk::trace; +namespace trace = ::opentelemetry::trace; namespace { @@ -60,3 +65,13 @@ void Publish(pubsub::Publisher& publisher, ParseResult const& args) { } std::cout << "Message(s) published\n"; } + +void Cleanup(){ + auto provider = trace::Provider::GetTracerProvider(); + if (provider) { + static_cast(provider.get())->ForceFlush(); + } + + std::shared_ptr none; + trace::Provider::SetTracerProvider(none); +} \ No newline at end of file diff --git a/pubsub-open-telemetry/publisher_helper.h b/pubsub-open-telemetry/publisher_helper.h index ae4c808..6b5b082 100644 --- a/pubsub-open-telemetry/publisher_helper.h +++ b/pubsub-open-telemetry/publisher_helper.h @@ -18,10 +18,13 @@ #include "google/cloud/pubsub/publisher.h" #include "parse_args.h" -namespace pubsub = ::google::cloud::pubsub; +// Create a publisher using the configuration set in `args`. +::google::cloud::pubsub::Publisher CreatePublisher(ParseResult const& args); -pubsub::Publisher CreatePublisher(ParseResult const& args); +// Publish message(s) using the `publisher` set in `args`. +void Publish(::google::cloud::pubsub::Publisher& publisher, ParseResult const& args); -void Publish(pubsub::Publisher& publisher, ParseResult const& args); +// Wait for the traces to be exported before exiting the program. +void Cleanup(); #endif // CPP_SAMPLES_PUBSUB_OPEN_TELEMETRY_PUBLISHER_H diff --git a/pubsub-open-telemetry/vcpkg.json b/pubsub-open-telemetry/vcpkg.json index b7d5de3..fa80626 100644 --- a/pubsub-open-telemetry/vcpkg.json +++ b/pubsub-open-telemetry/vcpkg.json @@ -11,6 +11,9 @@ "opentelemetry" ] }, + { + "name": "opentelemetry-cpp" + }, "boost-program-options" ] }