diff --git a/pubsub-open-telemetry/CMakeLists.txt b/pubsub-open-telemetry/CMakeLists.txt index d817a65f..207b1b05 100644 --- a/pubsub-open-telemetry/CMakeLists.txt +++ b/pubsub-open-telemetry/CMakeLists.txt @@ -41,6 +41,13 @@ add_executable(publisher publisher.cc) target_compile_features(publisher PRIVATE cxx_std_14) target_link_libraries(publisher PRIVATE publisher_helper parse_args) +add_executable(publisher_zipkin publisher_zipkin.cc) +target_compile_features(publisher_zipkin PRIVATE cxx_std_14) +target_link_libraries( + publisher_zipkin + PRIVATE publisher_helper parse_args + opentelemetry-cpp::opentelemetry_exporter_zipkin_trace) + add_executable(quickstart quickstart.cc) target_compile_features(quickstart PRIVATE cxx_std_14) target_link_libraries(quickstart PRIVATE google-cloud-cpp::pubsub diff --git a/pubsub-open-telemetry/publisher.md b/pubsub-open-telemetry/publisher.md index c52af1ff..dd979a84 100644 --- a/pubsub-open-telemetry/publisher.md +++ b/pubsub-open-telemetry/publisher.md @@ -4,24 +4,24 @@ 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 +For setup instructions, refer to the [README.md](README.md). + +## Cloud Trace -### Cloud Trace +### 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) +![Screenshot of the publish span in the Cloud Trace UI.](assets/publish_span.png) #### Create trace -![Screenshot of the create span in the Cloud Trace UI running publisher.](assets/create_span.png) +![Screenshot of the create span in the Cloud Trace UI.](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 @@ -29,7 +29,6 @@ For setup instructions, refer to the [README.md](README.md). ```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 ``` @@ -71,13 +70,54 @@ A simple publisher application with Open Telemetery enabled: --max-batch-messages arg pubsub::MaxBatchMessagesOption value ``` -### Using Bazel +## Zipkin + +Zipkin exporter is only supported by CMake at the moment. + +### Setup + +If you do not already, have one create a local Zipkin instance. + +#### (optional) Create a local Zipkin instance. + +To run Zipkin on the host `http://localhost:9411` + +```shell +docker run -d -p 9411:9411 openzipkin/zipkin +``` + +To kill the instance + +```shell +docker container ls +docker rm -f openzipkin/zipkin +``` + + + +## Build and run + +### Using CMake and Vcpkg + +#### Run the publisher with Zipkin + +```sh +cd cpp-samples/pubsub-open-telemetry +cmake -DWITH_ZIPKIN=ON -S . -B .build -DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake -G Ninja +cmake --build .build +``` #### 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 +.build/publisher_zipkin [project-name] [topic-id] +.build/publisher_zipkin [project-name] [topic-id] -n 1000 +.build/publisher_zipkin [project-name] [topic-id] --tracing-rate 0.01 -n 10 ``` diff --git a/pubsub-open-telemetry/publisher_zipkin.cc b/pubsub-open-telemetry/publisher_zipkin.cc new file mode 100644 index 00000000..6a52607e --- /dev/null +++ b/pubsub-open-telemetry/publisher_zipkin.cc @@ -0,0 +1,69 @@ +// Copyright 2023 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/trace_exporter.h" +#include "google/cloud/pubsub/publisher.h" +#include "opentelemetry/exporters/zipkin/zipkin_exporter_factory.h" +#include "opentelemetry/exporters/zipkin/zipkin_exporter_options.h" +#include "parse_args.h" +#include "publisher_helper.h" +#include +#include +#include +#include +#include +#include + +// 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; +namespace zipkin = opentelemetry::exporter::zipkin; + +void ConfigureZipkinTracer(ParseResult const& args) { + auto exporter = zipkin::ZipkinExporterFactory::Create(); + + trace_sdk::BatchSpanProcessorOptions span_options; + span_options.max_queue_size = args.max_queue_size; + auto processor = trace_sdk::BatchSpanProcessorFactory::Create( + std::move(exporter), span_options); + auto provider = + trace_sdk::TracerProviderFactory::Create(std::move(processor)); + + trace::Provider::SetTracerProvider(std::move(provider)); +} + +int main(int argc, char* argv[]) try { + auto args = ParseArguments(argc, argv); + if (args.project_id.empty() && args.topic_id.empty()) { + return 1; + } + std::cout << "Using project `" << args.project_id << "` and topic `" + << args.topic_id << "`\n"; + + // Automatically call `Cleanup()` before returning from `main()`. + std::shared_ptr cleanup(nullptr, [](void*) { Cleanup(); }); + + ConfigureZipkinTracer(args); + + auto publisher = CreatePublisher(args); + + Publish(publisher, args); + + return 0; +} catch (google::cloud::Status const& status) { + std::cerr << "google::cloud::Status thrown: " << status << "\n"; + return 1; +} diff --git a/pubsub-open-telemetry/vcpkg.json b/pubsub-open-telemetry/vcpkg.json index fa80626b..43a57acd 100644 --- a/pubsub-open-telemetry/vcpkg.json +++ b/pubsub-open-telemetry/vcpkg.json @@ -12,7 +12,10 @@ ] }, { - "name": "opentelemetry-cpp" + "name": "opentelemetry-cpp", + "features": [ + "zipkin" + ] }, "boost-program-options" ]