Skip to content

Commit

Permalink
respond to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alevenberg committed Dec 5, 2023
1 parent 35aba22 commit 1268493
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 84 deletions.
7 changes: 4 additions & 3 deletions pubsub-open-telemetry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
72 changes: 0 additions & 72 deletions pubsub-open-telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <project-id> <topic-id>
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
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions pubsub-open-telemetry/publisher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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";
Expand Down
77 changes: 77 additions & 0 deletions pubsub-open-telemetry/publisher.md
Original file line number Diff line number Diff line change
@@ -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 <project-id> <topic-id>
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
```
15 changes: 15 additions & 0 deletions pubsub-open-telemetry/publisher_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>
#include <string>

namespace gc = ::google::cloud;
namespace pubsub = gc::pubsub;
namespace trace_sdk = ::opentelemetry::sdk::trace;
namespace trace = ::opentelemetry::trace;

namespace {

Expand Down Expand Up @@ -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<trace_sdk::TracerProvider *>(provider.get())->ForceFlush();
}

std::shared_ptr<trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}
9 changes: 6 additions & 3 deletions pubsub-open-telemetry/publisher_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions pubsub-open-telemetry/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"opentelemetry"
]
},
{
"name": "opentelemetry-cpp"
},
"boost-program-options"
]
}

0 comments on commit 1268493

Please sign in to comment.