diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index dcb04ccf4e4..f764512acea 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(plugin) add_subdirectory(simple) +add_subdirectory(batch) diff --git a/examples/batch/BUILD b/examples/batch/BUILD new file mode 100644 index 00000000000..2c9e6ef9e11 --- /dev/null +++ b/examples/batch/BUILD @@ -0,0 +1,12 @@ +cc_binary( + name = "example_simple", + srcs = [ + "main.cc", + ], + linkopts = ["-lpthread"], + deps = [ + "//api", + "//exporters/ostream:ostream_span_exporter", + "//sdk/src/trace", + ], +) diff --git a/examples/batch/CMakeLists.txt b/examples/batch/CMakeLists.txt new file mode 100644 index 00000000000..d1146d59b33 --- /dev/null +++ b/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/examples/batch/main.cc b/examples/batch/main.cc new file mode 100644 index 00000000000..7d7fa57f36a --- /dev/null +++ b/examples/batch/main.cc @@ -0,0 +1,81 @@ +#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 +#include + +constexpr int kNumSpans = 10; + +namespace +{ + +void initTracer() +{ + auto exporter = std::unique_ptr( + new opentelemetry::exporter::trace::OStreamSpanExporter); + + // CONFIGURE BATCH SPAN PROCESSOR PARAMETERS + + // 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. + const size_t max_queue_size = kNumSpans * 2 + 5; + + // Time interval (in ms) between two consecutive exports. + const std::chrono::milliseconds schedule_delay_millis = std::chrono::milliseconds(3000); + + // We export `kNumSpans` after every `schedule_delay_millis` milliseconds. + const size_t max_export_batch_size = kNumSpans; + + auto processor = std::shared_ptr(new sdktrace::BatchSpanProcessor( + std::move(exporter), max_queue_size, schedule_delay_millis, max_export_batch_size)); + + auto provider = nostd::shared_ptr( + new sdktrace::TracerProvider(processor)); + // Set the global trace provider. + opentelemetry::trace::Provider::SetTracerProvider(provider); +} + +nostd::shared_ptr get_tracer() +{ + auto provider = opentelemetry::trace::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. +} diff --git a/examples/simple/CMakeLists.txt b/examples/simple/CMakeLists.txt index 8b137891791..b86593ee378 100644 --- a/examples/simple/CMakeLists.txt +++ b/examples/simple/CMakeLists.txt @@ -1 +1,9 @@ +include_directories(${CMAKE_SOURCE_DIR}/exporters/ostream/include) +add_library(foo_library foo_library/foo_library.cc) +target_link_libraries(foo_library opentelemetry_exporter_ostream_span + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) + +add_executable(example_simple main.cc) +target_link_libraries(example_simple ${CMAKE_THREAD_LIBS_INIT} foo_library + opentelemetry_trace)