forked from open-telemetry/opentelemetry-specification
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example for batch span processor (open-telemetry#241)
- Loading branch information
1 parent
6631930
commit c47e443
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
add_subdirectory(plugin) | ||
add_subdirectory(simple) | ||
add_subdirectory(batch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cc_binary( | ||
name = "example_simple", | ||
srcs = [ | ||
"main.cc", | ||
], | ||
linkopts = ["-lpthread"], | ||
deps = [ | ||
"//api", | ||
"//exporters/ostream:ostream_span_exporter", | ||
"//sdk/src/trace", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <chrono> | ||
#include <thread> | ||
|
||
constexpr int kNumSpans = 10; | ||
|
||
namespace | ||
{ | ||
|
||
void initTracer() | ||
{ | ||
auto exporter = std::unique_ptr<sdktrace::SpanExporter>( | ||
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<sdktrace::SpanProcessor>(new sdktrace::BatchSpanProcessor( | ||
std::move(exporter), max_queue_size, schedule_delay_millis, max_export_batch_size)); | ||
|
||
auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>( | ||
new sdktrace::TracerProvider(processor)); | ||
// Set the global trace provider. | ||
opentelemetry::trace::Provider::SetTracerProvider(provider); | ||
} | ||
|
||
nostd::shared_ptr<opentelemetry::trace::Tracer> 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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |