Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl(pubsub-otel): add zipkin example #284

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pubsub-open-telemetry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 52 additions & 12 deletions pubsub-open-telemetry/publisher.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@ 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

```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
```

Expand Down Expand Up @@ -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
```

<!-- TODO(issues/285): when the library in vcpkg is updated, add the screenshots
#### Publish trace

![Screenshot of the publish span in the Zipkin UI.](assets/zipkin_publish_span.png)

#### Create trace

![Screenshot of the create span in the Zipkin UI.](assets/zipkin_create_span.png) -->

## 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
```
69 changes: 69 additions & 0 deletions pubsub-open-telemetry/publisher_zipkin.cc
Original file line number Diff line number Diff line change
@@ -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 <opentelemetry/sdk/trace/batch_span_processor_factory.h>
#include <opentelemetry/sdk/trace/batch_span_processor_options.h>
#include <opentelemetry/sdk/trace/processor.h>
#include <opentelemetry/sdk/trace/tracer_provider_factory.h>
#include <opentelemetry/trace/provider.h>
#include <iostream>

// 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<void> 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;
}
5 changes: 4 additions & 1 deletion pubsub-open-telemetry/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
]
},
{
"name": "opentelemetry-cpp"
"name": "opentelemetry-cpp",
"features": [
"zipkin"
]
},
"boost-program-options"
]
Expand Down