Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::tracing Namespace Reference

Classes

struct  batch_config
 Batch export configuration. More...
 
struct  otlp_config
 OTLP exporter configuration. More...
 
class  span
 RAII span for distributed tracing. More...
 
struct  span_event
 Span event (timestamped annotation) More...
 
class  trace_context
 Immutable trace context for distributed tracing. More...
 
struct  tracing_config
 Main configuration structure for tracing. More...
 

Typedefs

using attribute_value = std::variant<std::string, int64_t, double, bool>
 Attribute value type (supports string, int64, double, bool)
 
using trace_id_t = std::array<uint8_t, 16>
 Trace ID type (128-bit identifier)
 
using span_id_t = std::array<uint8_t, 8>
 Span ID type (64-bit identifier)
 
using span_processor_callback = std::function<void(const span&)>
 Span processor callback type.
 

Enumerations

enum class  span_status { unset = 0 , ok = 1 , error = 2 }
 Span status codes following OpenTelemetry conventions. More...
 
enum class  span_kind {
  internal = 0 , server = 1 , client = 2 , producer = 3 ,
  consumer = 4
}
 Span kind following OpenTelemetry conventions. More...
 
enum class  trace_flags : uint8_t { none = 0x00 , sampled = 0x01 }
 Trace flags (8-bit) More...
 
enum class  exporter_type {
  none , console , otlp_grpc , otlp_http ,
  jaeger , zipkin
}
 Exporter types for trace data. More...
 
enum class  sampler_type { always_on , always_off , trace_id , parent_based }
 Sampler types for trace sampling decisions. More...
 

Functions

auto generate_trace_id () -> trace_id_t
 Generate a random trace ID.
 
auto generate_span_id () -> span_id_t
 Generate a random span ID.
 
auto bytes_to_hex (const uint8_t *data, size_t size) -> std::string
 Convert bytes to lowercase hex string.
 
auto hex_to_bytes (std::string_view hex, uint8_t *out, size_t size) -> bool
 Parse hex string to bytes.
 
void configure_tracing (const tracing_config &config)
 Initialize the tracing system with configuration.
 
void shutdown_tracing ()
 Shutdown the tracing system.
 
void flush_tracing ()
 Force flush all pending spans.
 
auto is_tracing_enabled () -> bool
 Check if tracing is enabled.
 
void register_span_processor (span_processor_callback callback)
 Register a custom span processor.
 
void export_span (const span &s)
 Export a completed span.
 

Typedef Documentation

◆ attribute_value

using kcenon::network::tracing::attribute_value = std::variant<std::string, int64_t, double, bool>

Attribute value type (supports string, int64, double, bool)

Definition at line 55 of file span.h.

◆ span_id_t

using kcenon::network::tracing::span_id_t = std::array<uint8_t, 8>

Span ID type (64-bit identifier)

Definition at line 39 of file trace_context.h.

◆ span_processor_callback

using kcenon::network::tracing::span_processor_callback = std::function<void(const span&)>

Span processor callback type.

Called when a span ends. Can be used for custom export or processing.

Definition at line 283 of file tracing_config.h.

◆ trace_id_t

using kcenon::network::tracing::trace_id_t = std::array<uint8_t, 16>

Trace ID type (128-bit identifier)

Definition at line 34 of file trace_context.h.

Enumeration Type Documentation

◆ exporter_type

Exporter types for trace data.

Enumerator
none 

Tracing disabled.

console 

Console/stdout output (for debugging)

otlp_grpc 

OTLP over gRPC (OpenTelemetry Collector)

otlp_http 

OTLP over HTTP (OpenTelemetry Collector)

jaeger 

Jaeger native format.

zipkin 

Zipkin format.

Definition at line 32 of file tracing_config.h.

33{
34 none,
35 console,
36 otlp_grpc,
37 otlp_http,
38 jaeger,
39 zipkin
40};
@ otlp_http
OTLP over HTTP (OpenTelemetry Collector)
@ otlp_grpc
OTLP over gRPC (OpenTelemetry Collector)
@ console
Console/stdout output (for debugging)

◆ sampler_type

Sampler types for trace sampling decisions.

Enumerator
always_on 

Sample all traces.

always_off 

Sample no traces.

trace_id 

Sample based on trace ID ratio.

parent_based 

Sample based on parent span's sampling decision.

Definition at line 45 of file tracing_config.h.

46{
47 always_on,
49 trace_id,
51};
@ trace_id
Sample based on trace ID ratio.
@ parent_based
Sample based on parent span's sampling decision.

◆ span_kind

Span kind following OpenTelemetry conventions.

Enumerator
internal 

Default, represents internal operations.

server 

Server-side handling of a request.

client 

Client-side request.

producer 

Message producer (e.g., queue publisher)

consumer 

Message consumer (e.g., queue subscriber)

Definition at line 43 of file span.h.

44{
45 internal = 0,
46 server = 1,
47 client = 2,
48 producer = 3,
49 consumer = 4
50};
@ consumer
Message consumer (e.g., queue subscriber)
@ server
Server-side handling of a request.
@ producer
Message producer (e.g., queue publisher)

◆ span_status

Span status codes following OpenTelemetry conventions.

Enumerator
unset 

Default status, span completed without explicit status.

ok 

Operation completed successfully.

error 

Operation failed.

Definition at line 33 of file span.h.

34{
35 unset = 0,
36 ok = 1,
37 error = 2
38};
@ unset
Default status, span completed without explicit status.
Result< T > error(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")

◆ trace_flags

enum class kcenon::network::tracing::trace_flags : uint8_t
strong

Trace flags (8-bit)

Enumerator
none 
sampled 

Definition at line 44 of file trace_context.h.

45{
46 none = 0x00,
47 sampled = 0x01
48};

Function Documentation

◆ bytes_to_hex()

auto kcenon::network::tracing::bytes_to_hex ( const uint8_t * data,
size_t size ) -> std::string
nodiscard

Convert bytes to lowercase hex string.

Parameters
dataByte array
sizeNumber of bytes
Returns
Hex string representation

Definition at line 315 of file trace_context.cpp.

316{
317 std::string result;
318 result.reserve(size * 2);
319
320 for (size_t i = 0; i < size; ++i)
321 {
322 result.push_back(hex_chars[(data[i] >> 4) & 0x0F]);
323 result.push_back(hex_chars[data[i] & 0x0F]);
324 }
325
326 return result;
327}

Referenced by kcenon::network::tracing::trace_context::span_id_hex(), and kcenon::network::tracing::trace_context::trace_id_hex().

Here is the caller graph for this function:

◆ configure_tracing()

void kcenon::network::tracing::configure_tracing ( const tracing_config & config)

Initialize the tracing system with configuration.

Parameters
configTracing configuration

This function must be called before creating any spans. It initializes the tracing system with the specified configuration.

Example:
config.otlp.endpoint = "http://localhost:4317";
config.service_name = "my-service";
tracing_config config
Definition exporters.cpp:29
void configure_tracing(const tracing_config &config)
Initialize the tracing system with configuration.
std::string endpoint
Endpoint URL for OTLP exporter @default "http://localhost:4317" for gRPC, "http://localhost:4318" for...
Main configuration structure for tracing.
exporter_type exporter
Exporter type to use @default exporter_type::none.
otlp_config otlp
OTLP exporter configuration.
std::string service_name
Service name for trace identification @default "network_system".

Definition at line 599 of file exporters.cpp.

600{
601 const bool should_log = config.debug && config.exporter != exporter_type::none;
602
603 {
604 std::lock_guard<std::mutex> lock(g_tracing_state.mutex);
605
606 g_tracing_state.config = config;
607 g_tracing_state.enabled.store(config.exporter != exporter_type::none,
608 std::memory_order_release);
609 }
610
611 if (should_log)
612 {
613 std::lock_guard<std::mutex> lock(g_console_mutex);
614 std::cout << "[TRACING] Configured with exporter: ";
615 switch (config.exporter)
616 {
617 case exporter_type::console:
618 std::cout << "console";
619 break;
620 case exporter_type::otlp_grpc:
621 std::cout << "otlp_grpc (" << config.otlp.endpoint << ")";
622 break;
623 case exporter_type::otlp_http:
624 std::cout << "otlp_http (" << config.otlp.endpoint << ")";
625 break;
626 case exporter_type::jaeger:
627 std::cout << "jaeger (" << config.jaeger_endpoint << ")";
628 break;
629 case exporter_type::zipkin:
630 std::cout << "zipkin (" << config.zipkin_endpoint << ")";
631 break;
632 default:
633 std::cout << "none";
634 break;
635 }
636 std::cout << ", service: " << config.service_name
637 << ", sample_rate: " << config.sample_rate << "\n";
638 }
639}

References config, console, kcenon::network::tracing::tracing_config::debug, kcenon::network::tracing::otlp_config::endpoint, kcenon::network::tracing::tracing_config::exporter, jaeger, kcenon::network::tracing::tracing_config::jaeger_endpoint, none, kcenon::network::tracing::tracing_config::otlp, otlp_grpc, otlp_http, kcenon::network::tracing::tracing_config::sample_rate, kcenon::network::tracing::tracing_config::service_name, zipkin, and kcenon::network::tracing::tracing_config::zipkin_endpoint.

◆ export_span()

void kcenon::network::tracing::export_span ( const span & s)

Export a completed span.

Parameters
sThe span to export

This function is called automatically when a span ends. It exports the span according to the configured exporter.

Definition at line 701 of file exporters.cpp.

702{
703 process_span(s);
704}

Referenced by kcenon::network::tracing::span::impl::do_end().

Here is the caller graph for this function:

◆ flush_tracing()

void kcenon::network::tracing::flush_tracing ( )

Force flush all pending spans.

Blocks until all pending spans are exported.

Definition at line 660 of file exporters.cpp.

661{
662 // Flush batch queue
663 std::vector<std::string> batch;
664 {
665 std::lock_guard<std::mutex> lock(g_tracing_state.batch_mutex);
666 if (!g_tracing_state.batch_queue.empty())
667 {
668 batch = std::move(g_tracing_state.batch_queue);
669 g_tracing_state.batch_queue.clear();
670 g_tracing_state.queued_count.store(0, std::memory_order_relaxed);
671 }
672 }
673
674 if (!batch.empty())
675 {
676 export_otlp_http(batch);
677 }
678
679 {
680 std::lock_guard<std::mutex> lock(g_console_mutex);
681 std::cout << std::flush;
682 }
683}

Referenced by shutdown_tracing().

Here is the caller graph for this function:

◆ generate_span_id()

auto kcenon::network::tracing::generate_span_id ( ) -> span_id_t
nodiscard

Generate a random span ID.

Returns
Randomly generated 64-bit span ID

Definition at line 308 of file trace_context.cpp.

309{
310 span_id_t id{};
311 get_random_bytes(id.data(), id.size());
312 return id;
313}
std::array< uint8_t, 8 > span_id_t
Span ID type (64-bit identifier)

Referenced by kcenon::network::tracing::trace_context::create_child_span(), and kcenon::network::tracing::trace_context::create_span().

Here is the caller graph for this function:

◆ generate_trace_id()

auto kcenon::network::tracing::generate_trace_id ( ) -> trace_id_t
nodiscard

Generate a random trace ID.

Returns
Randomly generated 128-bit trace ID

Definition at line 301 of file trace_context.cpp.

302{
303 trace_id_t id{};
304 get_random_bytes(id.data(), id.size());
305 return id;
306}
std::array< uint8_t, 16 > trace_id_t
Trace ID type (128-bit identifier)

Referenced by kcenon::network::tracing::trace_context::create_child_span(), and kcenon::network::tracing::trace_context::create_span().

Here is the caller graph for this function:

◆ hex_to_bytes()

auto kcenon::network::tracing::hex_to_bytes ( std::string_view hex,
uint8_t * out,
size_t size ) -> bool
nodiscard

Parse hex string to bytes.

Parameters
hexHex string
outOutput byte array
sizeExpected number of bytes
Returns
true if parsing succeeded

Definition at line 329 of file trace_context.cpp.

330{
331 if (hex.size() != size * 2)
332 {
333 return false;
334 }
335
336 for (size_t i = 0; i < size; ++i)
337 {
338 int high = hex_char_to_value(hex[i * 2]);
339 int low = hex_char_to_value(hex[i * 2 + 1]);
340
341 if (high < 0 || low < 0)
342 {
343 return false;
344 }
345
346 out[i] = static_cast<uint8_t>((high << 4) | low);
347 }
348
349 return true;
350}

Referenced by kcenon::network::tracing::trace_context::from_traceparent().

Here is the caller graph for this function:

◆ is_tracing_enabled()

◆ register_span_processor()

void kcenon::network::tracing::register_span_processor ( span_processor_callback callback)

Register a custom span processor.

Parameters
callbackCallback function called when spans end

Can be used to implement custom export logic or additional processing.

Definition at line 690 of file exporters.cpp.

691{
692 if (!callback)
693 {
694 return;
695 }
696
697 std::lock_guard<std::mutex> lock(g_tracing_state.mutex);
698 g_tracing_state.processors.push_back(std::move(callback));
699}

◆ shutdown_tracing()

void kcenon::network::tracing::shutdown_tracing ( )

Shutdown the tracing system.

Flushes any pending spans and releases resources. Should be called before application exit.

Example:
// At application shutdown
void shutdown_tracing()
Shutdown the tracing system.

Definition at line 641 of file exporters.cpp.

642{
643 // Flush any pending spans before shutdown
645
646 std::lock_guard<std::mutex> lock(g_tracing_state.mutex);
647
648 g_tracing_state.enabled.store(false, std::memory_order_release);
649 g_tracing_state.processors.clear();
650 g_tracing_state.config = tracing_config{};
651
652 // Clear batch queue
653 {
654 std::lock_guard<std::mutex> batch_lock(g_tracing_state.batch_mutex);
655 g_tracing_state.batch_queue.clear();
656 g_tracing_state.queued_count.store(0, std::memory_order_relaxed);
657 }
658}
void flush_tracing()
Force flush all pending spans.

References flush_tracing().

Here is the call graph for this function: