Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::jaeger_exporter Class Reference

Jaeger trace exporter implementation. More...

#include <trace_exporters.h>

Inheritance diagram for kcenon::monitoring::jaeger_exporter:
Inheritance graph
Collaboration diagram for kcenon::monitoring::jaeger_exporter:
Collaboration graph

Public Member Functions

 jaeger_exporter (const trace_export_config &config)
 
 jaeger_exporter (const trace_export_config &config, std::unique_ptr< http_transport > transport)
 
jaeger_span_data convert_span (const trace_span &span) const
 Convert internal span to Jaeger format.
 
common::VoidResult export_spans (const std::vector< trace_span > &spans) override
 Export a batch of spans.
 
common::VoidResult flush () override
 Flush any pending spans.
 
common::VoidResult shutdown () override
 Shutdown the exporter.
 
std::unordered_map< std::string, std::size_t > get_stats () const override
 Get exporter statistics.
 
- Public Member Functions inherited from kcenon::monitoring::trace_exporter_interface
virtual ~trace_exporter_interface ()=default
 

Private Member Functions

common::VoidResult send_thrift_batch (const std::vector< jaeger_span_data > &spans)
 
common::VoidResult send_grpc_batch (const std::vector< jaeger_span_data > &spans)
 
common::VoidResult send_with_retry (const http_request &request)
 

Private Attributes

trace_export_config config_
 
std::unique_ptr< http_transporttransport_
 
std::atomic< std::size_t > exported_spans_ {0}
 
std::atomic< std::size_t > failed_exports_ {0}
 
std::atomic< std::size_t > dropped_spans_ {0}
 
std::size_t max_retries_ {3}
 
std::chrono::milliseconds base_retry_delay_ {100}
 

Detailed Description

Jaeger trace exporter implementation.

Supports both Thrift over HTTP and gRPC protocols. Default endpoint: /api/traces for Thrift HTTP

Definition at line 268 of file trace_exporters.h.

Constructor & Destructor Documentation

◆ jaeger_exporter() [1/2]

kcenon::monitoring::jaeger_exporter::jaeger_exporter ( const trace_export_config & config)
inlineexplicit

Definition at line 279 of file trace_exporters.h.

std::unique_ptr< http_transport > transport_
std::unique_ptr< http_transport > create_default_transport()
Create default HTTP transport.

◆ jaeger_exporter() [2/2]

kcenon::monitoring::jaeger_exporter::jaeger_exporter ( const trace_export_config & config,
std::unique_ptr< http_transport > transport )
inline

Definition at line 282 of file trace_exporters.h.

283 : config_(config), transport_(std::move(transport)) {}

Member Function Documentation

◆ convert_span()

jaeger_span_data kcenon::monitoring::jaeger_exporter::convert_span ( const trace_span & span) const
inline

Convert internal span to Jaeger format.

Definition at line 288 of file trace_exporters.h.

288 {
289 jaeger_span_data jaeger_span;
290 jaeger_span.trace_id = span.trace_id;
291 jaeger_span.span_id = span.span_id;
292 jaeger_span.parent_span_id = span.parent_span_id;
293 jaeger_span.operation_name = span.operation_name;
294 jaeger_span.service_name = config_.service_name.value_or(span.service_name);
295
296 // Convert timestamps
297 auto start_epoch = span.start_time.time_since_epoch();
298 jaeger_span.start_time = std::chrono::duration_cast<std::chrono::microseconds>(start_epoch);
299
300 auto end_epoch = span.end_time.time_since_epoch();
301 jaeger_span.duration = std::chrono::duration_cast<std::chrono::microseconds>(end_epoch - start_epoch);
302
303 // Convert tags
304 for (const auto& [key, value] : span.tags) {
305 jaeger_span.tags.emplace_back(key, value);
306 }
307
308 // Add process tags
309 jaeger_span.process_tags.emplace_back("service.name", jaeger_span.service_name);
310
311 return jaeger_span;
312 }
std::optional< std::string > service_name
Override service name.

References config_, kcenon::monitoring::jaeger_span_data::duration, kcenon::monitoring::trace_span::end_time, kcenon::monitoring::jaeger_span_data::operation_name, kcenon::monitoring::trace_span::operation_name, kcenon::monitoring::jaeger_span_data::parent_span_id, kcenon::monitoring::trace_span::parent_span_id, kcenon::monitoring::jaeger_span_data::process_tags, kcenon::monitoring::jaeger_span_data::service_name, kcenon::monitoring::trace_export_config::service_name, kcenon::monitoring::trace_span::service_name, kcenon::monitoring::jaeger_span_data::span_id, kcenon::monitoring::trace_span::span_id, kcenon::monitoring::jaeger_span_data::start_time, kcenon::monitoring::trace_span::start_time, kcenon::monitoring::jaeger_span_data::tags, kcenon::monitoring::trace_span::tags, kcenon::monitoring::jaeger_span_data::trace_id, and kcenon::monitoring::trace_span::trace_id.

Referenced by export_spans(), and TEST_F().

Here is the caller graph for this function:

◆ export_spans()

common::VoidResult kcenon::monitoring::jaeger_exporter::export_spans ( const std::vector< trace_span > & spans)
inlineoverridevirtual

Export a batch of spans.

Implements kcenon::monitoring::trace_exporter_interface.

Definition at line 314 of file trace_exporters.h.

314 {
315 try {
316 std::vector<jaeger_span_data> jaeger_spans;
317 jaeger_spans.reserve(spans.size());
318
319 for (const auto& span : spans) {
320 jaeger_spans.push_back(convert_span(span));
321 }
322
323 // Convert to appropriate format and send
324 common::VoidResult send_result = common::ok();
326 send_result = send_thrift_batch(jaeger_spans);
328 send_result = send_grpc_batch(jaeger_spans);
329 } else {
330 return common::VoidResult::err(error_info(monitoring_error_code::invalid_configuration,
331 "Invalid Jaeger export format", "monitoring_system").to_common_error());
332 }
333
334 if (send_result.is_ok()) {
335 exported_spans_ += spans.size();
336 } else {
338 return send_result;
339 }
340
341 return common::ok();
342
343 } catch (const std::exception& e) {
345 return common::VoidResult::err(error_info(monitoring_error_code::operation_failed,
346 "Jaeger export failed: " + std::string(e.what()), "monitoring_system").to_common_error());
347 }
348 }
std::atomic< std::size_t > exported_spans_
common::VoidResult send_grpc_batch(const std::vector< jaeger_span_data > &spans)
common::VoidResult send_thrift_batch(const std::vector< jaeger_span_data > &spans)
std::atomic< std::size_t > failed_exports_
jaeger_span_data convert_span(const trace_span &span) const
Convert internal span to Jaeger format.
@ jaeger_thrift
Jaeger Thrift protocol.

References config_, convert_span(), exported_spans_, failed_exports_, kcenon::monitoring::trace_export_config::format, kcenon::monitoring::invalid_configuration, kcenon::monitoring::jaeger_grpc, kcenon::monitoring::jaeger_thrift, kcenon::monitoring::operation_failed, send_grpc_batch(), send_thrift_batch(), and kcenon::monitoring::error_info::to_common_error().

Referenced by TEST_F(), TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flush()

common::VoidResult kcenon::monitoring::jaeger_exporter::flush ( )
inlineoverridevirtual

Flush any pending spans.

Implements kcenon::monitoring::trace_exporter_interface.

Definition at line 350 of file trace_exporters.h.

350 {
351 // Jaeger exporter typically sends immediately, so flush is a no-op
352 return common::ok();
353 }

Referenced by shutdown(), and TEST_F().

Here is the caller graph for this function:

◆ get_stats()

std::unordered_map< std::string, std::size_t > kcenon::monitoring::jaeger_exporter::get_stats ( ) const
inlineoverridevirtual

Get exporter statistics.

Implements kcenon::monitoring::trace_exporter_interface.

Definition at line 359 of file trace_exporters.h.

359 {
360 return {
361 {"exported_spans", exported_spans_.load()},
362 {"failed_exports", failed_exports_.load()},
363 {"dropped_spans", dropped_spans_.load()}
364 };
365 }
std::atomic< std::size_t > dropped_spans_

References dropped_spans_, exported_spans_, and failed_exports_.

Referenced by TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ send_grpc_batch()

common::VoidResult kcenon::monitoring::jaeger_exporter::send_grpc_batch ( const std::vector< jaeger_span_data > & spans)
inlineprivate

Definition at line 398 of file trace_exporters.h.

398 {
399 // gRPC would require a different transport mechanism
400 // For now, fall back to HTTP POST with protobuf
401 std::vector<uint8_t> payload;
402 for (const auto& span : spans) {
403 auto proto = span.to_protobuf();
404 payload.insert(payload.end(), proto.begin(), proto.end());
405 }
406
407 http_request request;
408 request.url = config_.endpoint;
409 request.method = "POST";
410 request.headers["Content-Type"] = "application/grpc+proto";
411 for (const auto& [key, value] : config_.headers) {
412 request.headers[key] = value;
413 }
414 request.body = payload;
415 request.timeout = config_.timeout;
416
417 return send_with_retry(request);
418 }
common::VoidResult send_with_retry(const http_request &request)
std::chrono::milliseconds timeout
Request timeout.
std::unordered_map< std::string, std::string > headers
Custom HTTP headers.

References kcenon::monitoring::http_request::body, config_, kcenon::monitoring::trace_export_config::endpoint, kcenon::monitoring::http_request::headers, kcenon::monitoring::trace_export_config::headers, kcenon::monitoring::http_request::method, send_with_retry(), kcenon::monitoring::http_request::timeout, kcenon::monitoring::trace_export_config::timeout, and kcenon::monitoring::http_request::url.

Referenced by export_spans().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_thrift_batch()

common::VoidResult kcenon::monitoring::jaeger_exporter::send_thrift_batch ( const std::vector< jaeger_span_data > & spans)
inlineprivate

Definition at line 368 of file trace_exporters.h.

368 {
369 // Build JSON payload for Thrift over HTTP
370 std::ostringstream payload;
371 payload << "{\"data\":[{\"spans\":[";
372 bool first = true;
373 for (const auto& span : spans) {
374 if (!first) payload << ",";
375 payload << span.to_thrift_json();
376 first = false;
377 }
378 payload << "]}]}";
379
380 std::string body = payload.str();
381
382 // Build HTTP request
383 http_request request;
384 request.url = config_.endpoint + "/api/traces";
385 request.method = "POST";
386 request.headers["Content-Type"] = "application/x-thrift";
387 request.headers["Accept"] = "application/json";
388 for (const auto& [key, value] : config_.headers) {
389 request.headers[key] = value;
390 }
391 request.body = std::vector<uint8_t>(body.begin(), body.end());
392 request.timeout = config_.timeout;
393
394 // Send with retry
395 return send_with_retry(request);
396 }

References kcenon::monitoring::http_request::body, config_, kcenon::monitoring::trace_export_config::endpoint, kcenon::monitoring::http_request::headers, kcenon::monitoring::trace_export_config::headers, kcenon::monitoring::http_request::method, send_with_retry(), kcenon::monitoring::http_request::timeout, kcenon::monitoring::trace_export_config::timeout, and kcenon::monitoring::http_request::url.

Referenced by export_spans().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_with_retry()

common::VoidResult kcenon::monitoring::jaeger_exporter::send_with_retry ( const http_request & request)
inlineprivate

Definition at line 420 of file trace_exporters.h.

420 {
421 std::size_t attempt = 0;
422 std::chrono::milliseconds delay = base_retry_delay_;
423
424 while (attempt < max_retries_) {
425 auto result = transport_->send(request);
426 if (result.is_ok()) {
427 const auto& response = result.value();
428 if (response.status_code >= 200 && response.status_code < 300) {
429 return common::ok();
430 }
431 // Retry on 5xx errors
432 if (response.status_code >= 500) {
433 attempt++;
434 if (attempt < max_retries_) {
435 std::this_thread::sleep_for(delay);
436 delay *= 2; // Exponential backoff
437 }
438 continue;
439 }
440 // Non-retryable error
441 return common::VoidResult::err(error_info(monitoring_error_code::operation_failed,
442 "Jaeger export failed with status: " + std::to_string(response.status_code),
443 "monitoring_system").to_common_error());
444 }
445 attempt++;
446 if (attempt < max_retries_) {
447 std::this_thread::sleep_for(delay);
448 delay *= 2;
449 }
450 }
451 return common::VoidResult::err(error_info(monitoring_error_code::operation_failed,
452 "Jaeger export failed after " + std::to_string(max_retries_) + " retries",
453 "monitoring_system").to_common_error());
454 }
std::chrono::milliseconds base_retry_delay_
@ delay
Delay requests until resources are available.

References base_retry_delay_, kcenon::monitoring::delay, max_retries_, kcenon::monitoring::operation_failed, kcenon::monitoring::error_info::to_common_error(), and transport_.

Referenced by send_grpc_batch(), and send_thrift_batch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdown()

common::VoidResult kcenon::monitoring::jaeger_exporter::shutdown ( )
inlineoverridevirtual

Shutdown the exporter.

Implements kcenon::monitoring::trace_exporter_interface.

Definition at line 355 of file trace_exporters.h.

355 {
356 return flush();
357 }
common::VoidResult flush() override
Flush any pending spans.

References flush().

Referenced by TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ base_retry_delay_

std::chrono::milliseconds kcenon::monitoring::jaeger_exporter::base_retry_delay_ {100}
private

Definition at line 276 of file trace_exporters.h.

276{100};

Referenced by send_with_retry().

◆ config_

trace_export_config kcenon::monitoring::jaeger_exporter::config_
private

Definition at line 270 of file trace_exporters.h.

Referenced by convert_span(), export_spans(), send_grpc_batch(), and send_thrift_batch().

◆ dropped_spans_

std::atomic<std::size_t> kcenon::monitoring::jaeger_exporter::dropped_spans_ {0}
private

Definition at line 274 of file trace_exporters.h.

274{0};

Referenced by get_stats().

◆ exported_spans_

std::atomic<std::size_t> kcenon::monitoring::jaeger_exporter::exported_spans_ {0}
private

Definition at line 272 of file trace_exporters.h.

272{0};

Referenced by export_spans(), and get_stats().

◆ failed_exports_

std::atomic<std::size_t> kcenon::monitoring::jaeger_exporter::failed_exports_ {0}
private

Definition at line 273 of file trace_exporters.h.

273{0};

Referenced by export_spans(), and get_stats().

◆ max_retries_

std::size_t kcenon::monitoring::jaeger_exporter::max_retries_ {3}
private

Definition at line 275 of file trace_exporters.h.

275{3};

Referenced by send_with_retry().

◆ transport_

std::unique_ptr<http_transport> kcenon::monitoring::jaeger_exporter::transport_
private

Definition at line 271 of file trace_exporters.h.

Referenced by send_with_retry().


The documentation for this class was generated from the following file: