summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/opentelemetry-cpp/examples/batch
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:54:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:54:28 +0000
commite6918187568dbd01842d8d1d2c808ce16a894239 (patch)
tree64f88b554b444a49f656b6c656111a145cbbaa28 /src/jaegertracing/opentelemetry-cpp/examples/batch
parentInitial commit. (diff)
downloadceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz
ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/jaegertracing/opentelemetry-cpp/examples/batch')
-rw-r--r--src/jaegertracing/opentelemetry-cpp/examples/batch/BUILD13
-rw-r--r--src/jaegertracing/opentelemetry-cpp/examples/batch/CMakeLists.txt6
-rw-r--r--src/jaegertracing/opentelemetry-cpp/examples/batch/main.cc90
3 files changed, 109 insertions, 0 deletions
diff --git a/src/jaegertracing/opentelemetry-cpp/examples/batch/BUILD b/src/jaegertracing/opentelemetry-cpp/examples/batch/BUILD
new file mode 100644
index 000000000..2dedf6b29
--- /dev/null
+++ b/src/jaegertracing/opentelemetry-cpp/examples/batch/BUILD
@@ -0,0 +1,13 @@
+cc_binary(
+ name = "example_simple",
+ srcs = [
+ "main.cc",
+ ],
+ linkopts = ["-lpthread"],
+ tags = ["ostream"],
+ deps = [
+ "//api",
+ "//exporters/ostream:ostream_span_exporter",
+ "//sdk/src/trace",
+ ],
+)
diff --git a/src/jaegertracing/opentelemetry-cpp/examples/batch/CMakeLists.txt b/src/jaegertracing/opentelemetry-cpp/examples/batch/CMakeLists.txt
new file mode 100644
index 000000000..d1146d59b
--- /dev/null
+++ b/src/jaegertracing/opentelemetry-cpp/examples/batch/CMakeLists.txt
@@ -0,0 +1,6 @@
+include_directories(${CMAKE_SOURCE_DIR}/exporters/ostream/include)
+
+add_executable(batch_span_processor_example main.cc)
+
+target_link_libraries(batch_span_processor_example ${CMAKE_THREAD_LIBS_INIT}
+ opentelemetry_exporter_ostream_span opentelemetry_trace)
diff --git a/src/jaegertracing/opentelemetry-cpp/examples/batch/main.cc b/src/jaegertracing/opentelemetry-cpp/examples/batch/main.cc
new file mode 100644
index 000000000..9a4606ef5
--- /dev/null
+++ b/src/jaegertracing/opentelemetry-cpp/examples/batch/main.cc
@@ -0,0 +1,90 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+#include "opentelemetry/sdk/trace/tracer_provider.h"
+#include "opentelemetry/trace/provider.h"
+// Using an exporter that simply dumps span data to stdout.
+#include "opentelemetry/exporters/ostream/span_exporter.h"
+#include "opentelemetry/sdk/trace/batch_span_processor.h"
+
+#include <chrono>
+#include <thread>
+
+constexpr int kNumSpans = 10;
+namespace trace_api = opentelemetry::trace;
+namespace resource = opentelemetry::sdk::resource;
+namespace exporter_trace = opentelemetry::exporter::trace;
+namespace trace_sdk = opentelemetry::sdk::trace;
+namespace nostd = opentelemetry::nostd;
+
+namespace
+{
+
+void initTracer()
+{
+ auto exporter = std::unique_ptr<trace_sdk::SpanExporter>(new exporter_trace::OStreamSpanExporter);
+
+ // CONFIGURE BATCH SPAN PROCESSOR PARAMETERS
+
+ trace_sdk::BatchSpanProcessorOptions options{};
+ // We make the queue size `KNumSpans`*2+5 because when the queue is half full, a preemptive notif
+ // is sent to start an export call, which we want to avoid in this simple example.
+ options.max_queue_size = kNumSpans * 2 + 5;
+ // Time interval (in ms) between two consecutive exports.
+ options.schedule_delay_millis = std::chrono::milliseconds(3000);
+ // We export `kNumSpans` after every `schedule_delay_millis` milliseconds.
+ options.max_export_batch_size = kNumSpans;
+
+ resource::ResourceAttributes attributes = {{"service", "test_service"}, {"version", (uint32_t)1}};
+ auto resource = resource::Resource::Create(attributes);
+
+ auto processor = std::unique_ptr<trace_sdk::SpanProcessor>(
+ new trace_sdk::BatchSpanProcessor(std::move(exporter), options));
+
+ auto provider = nostd::shared_ptr<trace_api::TracerProvider>(
+ new trace_sdk::TracerProvider(std::move(processor), resource));
+ // Set the global trace provider.
+ trace_api::Provider::SetTracerProvider(provider);
+}
+
+nostd::shared_ptr<trace_api::Tracer> get_tracer()
+{
+ auto provider = trace_api::Provider::GetTracerProvider();
+ return provider->GetTracer("foo_library");
+}
+
+void StartAndEndSpans()
+{
+ for (int i = 1; i <= kNumSpans; ++i)
+ {
+ get_tracer()->StartSpan("Span " + std::to_string(i));
+ }
+}
+
+} // namespace
+
+int main()
+{
+ // Removing this line will leave the default noop TracerProvider in place.
+ initTracer();
+
+ std::cout << "Creating first batch of " << kNumSpans << " spans and waiting 3 seconds ...\n";
+ StartAndEndSpans();
+ std::this_thread::sleep_for(std::chrono::milliseconds(3000 + 50));
+ // The spans should now be exported.
+ std::cout << "....Exported!\n\n\n";
+
+ // Do the same again
+ std::cout << "Creating second batch of " << kNumSpans << " spans and waiting 3 seconds ...\n";
+ StartAndEndSpans();
+ std::this_thread::sleep_for(std::chrono::milliseconds(3000 + 50));
+ std::cout << "....Exported!\n\n\n";
+
+ // Shutdown and drain queue
+ StartAndEndSpans();
+ printf("Shutting down and draining queue.... \n");
+ std::this_thread::sleep_for(std::chrono::milliseconds(2000));
+ // We immediately let the program terminate which invokes the processor destructor
+ // which in turn invokes the processor Shutdown(), which finally drains the queue of ALL
+ // its spans.
+}