28using namespace std::chrono_literals;
61 config.
headers[
"x-api-key"] =
"example-key";
62 config.
headers[
"x-environment"] =
"development";
77 std::vector<trace_span> spans;
79 auto now = std::chrono::system_clock::now();
80 std::string trace_id =
"0123456789abcdef0123456789abcdef";
85 root.
span_id =
"0123456789abcdef";
91 root.
status = trace_span::status_code::ok;
92 root.
tags[
"http.method"] =
"GET";
93 root.
tags[
"http.url"] =
"/api/users";
94 root.
tags[
"http.status_code"] =
"200";
95 spans.push_back(root);
100 db_span.
span_id =
"fedcba9876543210";
107 db_span.
status = trace_span::status_code::ok;
108 db_span.
tags[
"db.system"] =
"postgresql";
109 db_span.
tags[
"db.statement"] =
"SELECT * FROM users";
110 db_span.
tags[
"db.name"] =
"user_db";
111 spans.push_back(db_span);
116 cache_span.
span_id =
"1234567890abcdef";
123 cache_span.
status = trace_span::status_code::ok;
124 cache_span.
tags[
"cache.key"] =
"user:123";
125 cache_span.
tags[
"cache.hit"] =
"false";
126 spans.push_back(cache_span);
135 std::cout <<
"=== OTLP Export Example ===" << std::endl;
139 std::cout <<
"\n1. Configuring OTLP exporter..." << std::endl;
142 auto validation = config.validate();
144 if (validation.is_err()) {
145 std::cerr <<
" Configuration validation failed: "
146 << validation.error().message << std::endl;
150 std::cout <<
" โ Configuration validated" << std::endl;
151 std::cout <<
" Endpoint: " << config.endpoint << std::endl;
152 std::cout <<
" Service: " << config.service_name
153 <<
" v" << config.service_version << std::endl;
154 std::cout <<
" Max batch size: " << config.max_batch_size << std::endl;
155 std::cout <<
" Max retries: " << config.max_retry_attempts << std::endl;
158 std::cout <<
"\n2. Creating OTLP exporter..." << std::endl;
161 std::cout <<
" โ Exporter created" << std::endl;
164 std::cout <<
"\n3. Starting exporter..." << std::endl;
166 auto start_result = exporter->start();
167 if (start_result.is_err()) {
168 std::cerr <<
" โ Failed to start exporter: "
169 << start_result.error().message << std::endl;
170 std::cerr <<
" Note: Make sure an OTLP receiver is running on "
171 << config.endpoint << std::endl;
172 std::cerr <<
" You can use: docker run -p 4317:4317 otel/opentelemetry-collector"
177 std::cout <<
" โ Exporter started and connected" << std::endl;
180 std::cout <<
"\n4. Creating sample trace spans..." << std::endl;
183 std::cout <<
" โ Created " << spans.size() <<
" spans" << std::endl;
185 for (
const auto& span : spans) {
186 std::cout <<
" - " << span.operation_name
187 <<
" (duration: " << span.duration.count() <<
"ยตs)" << std::endl;
191 std::cout <<
"\n5. Exporting spans..." << std::endl;
193 auto export_result = exporter->export_spans(spans);
194 if (export_result.is_ok()) {
195 std::cout <<
" โ Export succeeded" << std::endl;
197 std::cerr <<
" โ Export failed: "
198 << export_result.error().message << std::endl;
202 std::cout <<
"\n6. Exporter statistics:" << std::endl;
204 auto detailed_stats = exporter->get_detailed_stats();
205 std::cout <<
" Spans exported: " << detailed_stats.spans_exported << std::endl;
206 std::cout <<
" Spans dropped: " << detailed_stats.spans_dropped << std::endl;
207 std::cout <<
" Export failures: " << detailed_stats.export_failures << std::endl;
208 std::cout <<
" Retry attempts: " << detailed_stats.retries << std::endl;
209 std::cout <<
" Total export time: "
210 << detailed_stats.total_export_time.count() <<
"ยตs" << std::endl;
213 std::cout <<
"\n7. Shutting down exporter..." << std::endl;
216 exporter->shutdown();
217 std::cout <<
" โ Exporter shutdown complete" << std::endl;
219 std::cout <<
"\n=== Example completed successfully ===" << std::endl;
221 }
catch (
const std::exception& e) {
222 std::cerr <<
"Exception: " << e.what() << std::endl;
230 std::cout <<
"\n=== Batch Export Optimization ===" << std::endl;
238 auto start_result = exporter->start();
240 if (start_result.is_err()) {
241 std::cerr <<
" Skipping batch demo (no OTLP receiver available)" << std::endl;
245 std::cout <<
"\n1. Creating large batch of spans..." << std::endl;
248 std::vector<trace_span> large_batch;
249 auto now = std::chrono::system_clock::now();
251 for (
int i = 0; i < 25; ++i) {
253 span.
trace_id =
"batch00000000000000000000000000" + std::to_string(i);
254 span.
span_id =
"span000000000000" + std::to_string(i);
257 span.
start_time = now + std::chrono::milliseconds(i);
260 span.
status = trace_span::status_code::ok;
261 large_batch.push_back(span);
264 std::cout <<
" Created " << large_batch.size() <<
" spans" << std::endl;
265 std::cout <<
" Batch size: " << config.
max_batch_size << std::endl;
268 std::cout <<
"\n2. Exporting batch..." << std::endl;
269 auto export_result = exporter->export_spans(large_batch);
271 if (export_result.is_ok()) {
272 std::cout <<
" โ Batch export succeeded" << std::endl;
274 std::cerr <<
" โ Batch export failed: "
275 << export_result.error().message << std::endl;
279 auto stats = exporter->get_detailed_stats();
280 std::cout <<
"\n3. Final statistics:" << std::endl;
281 std::cout <<
" Total exported: " << stats.spans_exported << std::endl;
282 std::cout <<
" Batches sent: " << stats.batches_sent << std::endl;
284 exporter->shutdown();
286 }
catch (
const std::exception& e) {
287 std::cerr <<
"Exception: " << e.what() << std::endl;
292 std::cout <<
"OpenTelemetry Protocol (OTLP) Export Example\n" << std::endl;
297 std::cout <<
"\n" << std::string(60,
'=') <<
"\n" << std::endl;
Distributed tracing implementation for monitoring system.
std::unique_ptr< otlp_grpc_exporter > create_otlp_grpc_exporter(const std::string &endpoint="localhost:4317")
Create OTLP gRPC exporter with default configuration.
void demonstrate_batch_export()
Demonstrate batch export optimization.
std::vector< trace_span > create_sample_spans()
Create sample trace spans for export.
otlp_grpc_config create_otlp_config()
Configure OTLP exporter with custom settings.
void demonstrate_otlp_export()
Demonstrate OTLP export with error handling.
OTLP gRPC trace exporter implementation.
Configuration for OTLP gRPC exporter.
std::string service_version
Service version.
std::unordered_map< std::string, std::string > resource_attributes
Resource attributes.
std::string endpoint
OTLP receiver endpoint.
std::size_t max_retry_attempts
Maximum retry attempts.
std::chrono::milliseconds batch_timeout
Batch export timeout.
std::string service_name
Service name.
std::size_t max_queue_size
Maximum queued spans.
std::size_t max_batch_size
Maximum spans per batch.
std::chrono::milliseconds initial_backoff
Initial retry backoff.
std::chrono::milliseconds max_backoff
Maximum retry backoff.
std::chrono::milliseconds timeout
Request timeout.
std::unordered_map< std::string, std::string > headers
Custom headers.
Trace span representing a unit of work in distributed tracing.
std::string parent_span_id
void calculate_duration()
Calculate duration if span is finished.
std::unordered_map< std::string, std::string > tags
std::chrono::system_clock::time_point end_time
std::string operation_name
std::chrono::system_clock::time_point start_time