blob: 9a4606ef5561d55699515480cbf53ed29c827b00 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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.
}
|