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 431 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 442 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 445 of file trace_exporters.h.

446 : 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 451 of file trace_exporters.h.

451 {
452 jaeger_span_data jaeger_span;
453 jaeger_span.trace_id = span.trace_id;
454 jaeger_span.span_id = span.span_id;
455 jaeger_span.parent_span_id = span.parent_span_id;
456 jaeger_span.operation_name = span.operation_name;
457 jaeger_span.service_name = config_.service_name.value_or(span.service_name);
458
459 // Convert timestamps
460 auto start_epoch = span.start_time.time_since_epoch();
461 jaeger_span.start_time = std::chrono::duration_cast<std::chrono::microseconds>(start_epoch);
462
463 auto end_epoch = span.end_time.time_since_epoch();
464 jaeger_span.duration = std::chrono::duration_cast<std::chrono::microseconds>(end_epoch - start_epoch);
465
466 // Convert tags
467 for (const auto& [key, value] : span.tags) {
468 jaeger_span.tags.emplace_back(key, value);
469 }
470
471 // Add process tags
472 jaeger_span.process_tags.emplace_back("service.name", jaeger_span.service_name);
473
474 return jaeger_span;
475 }
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(), TEST_F(), TEST_F(), TEST_F(), 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 477 of file trace_exporters.h.

477 {
478 try {
479 std::vector<jaeger_span_data> jaeger_spans;
480 jaeger_spans.reserve(spans.size());
481
482 for (const auto& span : spans) {
483 jaeger_spans.push_back(convert_span(span));
484 }
485
486 // Convert to appropriate format and send
487 common::VoidResult send_result = common::ok();
489 send_result = send_thrift_batch(jaeger_spans);
491 send_result = send_grpc_batch(jaeger_spans);
492 } else {
493 return common::VoidResult::err(error_info(monitoring_error_code::invalid_configuration,
494 "Invalid Jaeger export format", "monitoring_system").to_common_error());
495 }
496
497 if (send_result.is_ok()) {
498 exported_spans_ += spans.size();
499 } else {
501 return send_result;
502 }
503
504 return common::ok();
505
506 } catch (const std::exception& e) {
508 return common::VoidResult::err(error_info(monitoring_error_code::operation_failed,
509 "Jaeger export failed: " + std::string(e.what()), "monitoring_system").to_common_error());
510 }
511 }
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 513 of file trace_exporters.h.

513 {
514 // Jaeger exporter typically sends immediately, so flush is a no-op
515 return common::ok();
516 }

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 522 of file trace_exporters.h.

522 {
523 return {
524 {"exported_spans", exported_spans_.load()},
525 {"failed_exports", failed_exports_.load()},
526 {"dropped_spans", dropped_spans_.load()}
527 };
528 }
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 561 of file trace_exporters.h.

561 {
562 // Serialize as a Jaeger api_v2 `Batch` protobuf message. Real gRPC
563 // transport would additionally wrap this in a 5-byte gRPC length
564 // prefix and multiplex over HTTP/2; when a gRPC transport is
565 // available the raw `Batch` bytes below can be used as the message
566 // payload directly.
567 std::string resolved_service;
568 if (config_.service_name) {
569 resolved_service = *config_.service_name;
570 } else if (!spans.empty()) {
571 resolved_service = spans.front().service_name;
572 }
573 auto payload = encode_jaeger_batch(spans, resolved_service);
574
575 http_request request;
576 request.url = config_.endpoint;
577 request.method = "POST";
578 request.headers["Content-Type"] = "application/x-protobuf";
579 for (const auto& [key, value] : config_.headers) {
580 request.headers[key] = value;
581 }
582 request.body = payload;
583 request.timeout = config_.timeout;
584
585 return send_with_retry(request);
586 }
common::VoidResult send_with_retry(const http_request &request)
std::vector< uint8_t > encode_jaeger_batch(const std::vector< jaeger_span_data > &spans, const std::string &service_name)
Encode a batch of Jaeger spans (with shared process) into a Jaeger api_v2 Batch protobuf message.
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::encode_jaeger_batch(), 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::trace_export_config::service_name, 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 531 of file trace_exporters.h.

531 {
532 // Build JSON payload for Thrift over HTTP
533 std::ostringstream payload;
534 payload << "{\"data\":[{\"spans\":[";
535 bool first = true;
536 for (const auto& span : spans) {
537 if (!first) payload << ",";
538 payload << span.to_thrift_json();
539 first = false;
540 }
541 payload << "]}]}";
542
543 std::string body = payload.str();
544
545 // Build HTTP request
546 http_request request;
547 request.url = config_.endpoint + "/api/traces";
548 request.method = "POST";
549 request.headers["Content-Type"] = "application/x-thrift";
550 request.headers["Accept"] = "application/json";
551 for (const auto& [key, value] : config_.headers) {
552 request.headers[key] = value;
553 }
554 request.body = std::vector<uint8_t>(body.begin(), body.end());
555 request.timeout = config_.timeout;
556
557 // Send with retry
558 return send_with_retry(request);
559 }

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 588 of file trace_exporters.h.

588 {
589 std::size_t attempt = 0;
590 std::chrono::milliseconds delay = base_retry_delay_;
591
592 while (attempt < max_retries_) {
593 auto result = transport_->send(request);
594 if (result.is_ok()) {
595 const auto& response = result.value();
596 if (response.status_code >= 200 && response.status_code < 300) {
597 return common::ok();
598 }
599 // Retry on 5xx errors
600 if (response.status_code >= 500) {
601 attempt++;
602 if (attempt < max_retries_) {
603 std::this_thread::sleep_for(delay);
604 delay *= 2; // Exponential backoff
605 }
606 continue;
607 }
608 // Non-retryable error
609 return common::VoidResult::err(error_info(monitoring_error_code::operation_failed,
610 "Jaeger export failed with status: " + std::to_string(response.status_code),
611 "monitoring_system").to_common_error());
612 }
613 attempt++;
614 if (attempt < max_retries_) {
615 std::this_thread::sleep_for(delay);
616 delay *= 2;
617 }
618 }
619 return common::VoidResult::err(error_info(monitoring_error_code::operation_failed,
620 "Jaeger export failed after " + std::to_string(max_retries_) + " retries",
621 "monitoring_system").to_common_error());
622 }
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 518 of file trace_exporters.h.

518 {
519 return flush();
520 }
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 439 of file trace_exporters.h.

439{100};

Referenced by send_with_retry().

◆ config_

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

Definition at line 433 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 437 of file trace_exporters.h.

437{0};

Referenced by get_stats().

◆ exported_spans_

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

Definition at line 435 of file trace_exporters.h.

435{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 436 of file trace_exporters.h.

436{0};

Referenced by export_spans(), and get_stats().

◆ max_retries_

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

Definition at line 438 of file trace_exporters.h.

438{3};

Referenced by send_with_retry().

◆ transport_

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

Definition at line 434 of file trace_exporters.h.

Referenced by send_with_retry().


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