Skip to content

Commit

Permalink
impl(pubsub-otel): add jaeger example (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
alevenberg authored Dec 8, 2023
1 parent 42b7134 commit b1e1ae1
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 11 deletions.
16 changes: 16 additions & 0 deletions pubsub-open-telemetry/.jaeger_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

exporters:
logging:
loglevel: DEBUG
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
service:
pipelines:
traces:
receivers:
- otlp
exporters:
- logging
8 changes: 8 additions & 0 deletions pubsub-open-telemetry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ target_compile_features(publisher_helper PUBLIC cxx_std_14)
target_link_libraries(publisher_helper PUBLIC parse_args opentelemetry-cpp::api
opentelemetry-cpp::sdk)

add_executable(publisher_jaeger publisher_jaeger.cc)
target_compile_features(publisher_jaeger PRIVATE cxx_std_14)
target_link_libraries(
publisher_jaeger
PRIVATE publisher_helper parse_args opentelemetry-cpp::otlp_grpc_client
opentelemetry-cpp::otlp_grpc_exporter
opentelemetry-cpp::otlp_recordable)

add_executable(publisher publisher.cc)
target_compile_features(publisher PRIVATE cxx_std_14)
target_link_libraries(publisher PRIVATE publisher_helper parse_args)
Expand Down
2 changes: 1 addition & 1 deletion pubsub-open-telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ substantially faster.
```sh
cd cpp-samples/pubsub-open-telemetry
cmake -S . -B .build -DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake -G Ninja
cmake --build .build
cmake --build .build --target quickstart
```

### 4. Run the examples
Expand Down
92 changes: 85 additions & 7 deletions pubsub-open-telemetry/publisher.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ To find the traces, navigate to the Cloud Trace UI.

#### Publish trace

![Screenshot of the publish span in the Cloud Trace UI.](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.](assets/create_span.png)
![Screenshot of the create span in the Cloud Trace UI.](assets/create_span.png)

## Build and run

### Using CMake and Vcpkg

```sh
cd cpp-samples/pubsub-open-telemetry
cmake -S . -B .build -DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake -G Ninja
cmake --build .build --target publisher
```

#### Run basic publisher examples

```shell
Expand Down Expand Up @@ -72,25 +80,28 @@ A simple publisher application with Open Telemetery enabled:

## Zipkin

Zipkin exporter is only supported by CMake at the moment.
This example uses the
[Zipkins exporter](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/exporters/zipkin),
which is only supported by CMake at the moment.

### Setup

If you do not already, have one create a local Zipkin instance.
If you do not already have one, create a local
[Zipkin instance](https://zipkin.io/pages/quickstart.html).

#### (optional) Create a local Zipkin instance.

To run Zipkin on the host `http://localhost:9411`
Run Zipkin at the endpoint `http://localhost:9411`:

```shell
docker run -d -p 9411:9411 openzipkin/zipkin
```

To kill the instance
To kill the instance:

```shell
docker container ls
docker rm -f openzipkin/zipkin
docker stop <container-id>
```

<!-- TODO(issues/285): when the library in vcpkg is updated, add the screenshots
Expand All @@ -106,12 +117,12 @@ docker rm -f openzipkin/zipkin

### Using CMake and Vcpkg

#### Run the publisher with Zipkin
#### Build 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
cmake --build .build --target publisher_zipkin
```

#### Run basic publisher examples
Expand All @@ -121,3 +132,70 @@ cmake --build .build
.build/publisher_zipkin [project-name] [topic-id] -n 1000
.build/publisher_zipkin [project-name] [topic-id] --tracing-rate 0.01 -n 10
```

## Jaeger

This example uses
[OpenTelemetry Protocol (OTLP)](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/examples/otlp)
with gRPC. This example is only implemented using CMake at the moment.

### Setup

If you do not already have one, create a local
[Jaeger instance](https://www.jaegertracing.io/docs/1.52/getting-started).

#### (optional) Create a local Jaeger instance.

Run the Jaeger UI at the endpoint `http://localhost:16686`:

```shell
docker run --rm --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
-p 14250:14250 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 \
jaegertracing/all-in-one:1.52
```

To kill the instance:

```shell
docker container ls
docker stop <container-id>
```

<!-- TODO(issues/285): when the library in vcpkg is updated, add the screenshots
#### Publish trace
![Screenshot of the publish span in the Jaeger UI.](assets/jaeger_publish_span.png)
#### Create trace
![Screenshot of the create span in the Jaeger UI.](assets/jaeger_create_span.png) -->

## Build and run

### Using CMake and Vcpkg

#### Build the publisher with Jaeger

```sh
cd cpp-samples/pubsub-open-telemetry
cmake -DWITH_OTLP_GRPC=ON -S . -B .build -DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake -G Ninja
cmake --build .build --target publisher_jaeger
```

#### Run basic publisher examples

```shell
.build/publisher_jaeger [project-name] [topic-id]
.build/publisher_jaeger [project-name] [topic-id] -n 1000
.build/publisher_jaeger [project-name] [topic-id] --tracing-rate 0.01 -n 10
```
68 changes: 68 additions & 0 deletions pubsub-open-telemetry/publisher_jaeger.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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/pubsub/publisher.h"
#include "parse_args.h"
#include "publisher_helper.h"
#include <opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.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 trace_sdk = opentelemetry::sdk::trace;
namespace otlp = opentelemetry::exporter::otlp;

namespace {

void ConfigureOtlpGrpcExporterTracer(ParseResult const& args) {
otlp::OtlpGrpcExporterOptions opts;
auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts);
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));
opentelemetry::trace::Provider::SetTracerProvider(std::move(provider));
}

} // namespace

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(); });

ConfigureOtlpGrpcExporterTracer(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;
}
2 changes: 0 additions & 2 deletions pubsub-open-telemetry/publisher_zipkin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// 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"
Expand All @@ -27,7 +26,6 @@

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

0 comments on commit b1e1ae1

Please sign in to comment.