Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
tracing_config.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
16#pragma once
17
18#include <chrono>
19#include <functional>
20#include <map>
21#include <string>
22
24
25// Forward declarations
26class span;
27struct span_event;
28
32enum class exporter_type
33{
34 none,
35 console,
36 otlp_grpc,
37 otlp_http,
38 jaeger,
39 zipkin
40};
41
45enum class sampler_type
46{
47 always_on,
49 trace_id,
51};
52
57{
62 size_t max_queue_size = 512;
63
68 std::chrono::milliseconds schedule_delay{5000};
69
75
80 std::chrono::milliseconds export_timeout{30000};
81};
82
87{
92 std::string endpoint;
93
97 std::map<std::string, std::string> headers;
98
103 std::chrono::milliseconds timeout{10000};
104
109 bool insecure = false;
110
114 std::string certificate_path;
115};
116
136{
142
147 std::string service_name = "network_system";
148
153 std::string service_version;
154
159 std::string service_namespace;
160
166
170 std::map<std::string, std::string> resource_attributes;
171
177
184 double sample_rate = 1.0;
185
190
195 std::string jaeger_endpoint = "http://localhost:14268/api/traces";
196
201 std::string zipkin_endpoint = "http://localhost:9411/api/v2/spans";
202
207
212 bool debug = false;
213
218 [[nodiscard]] static auto console() -> tracing_config
219 {
222 return config;
223 }
224
230 [[nodiscard]] static auto otlp_grpc(
231 const std::string& endpoint = "http://localhost:4317") -> tracing_config
232 {
235 config.otlp.endpoint = endpoint;
236 return config;
237 }
238
244 [[nodiscard]] static auto otlp_http(
245 const std::string& endpoint = "http://localhost:4318") -> tracing_config
246 {
249 config.otlp.endpoint = endpoint;
250 return config;
251 }
252
258 [[nodiscard]] static auto jaeger(
259 const std::string& endpoint = "http://localhost:14268/api/traces")
261 {
263 config.exporter = exporter_type::jaeger;
264 config.jaeger_endpoint = endpoint;
265 return config;
266 }
267
272 [[nodiscard]] static auto disabled() -> tracing_config
273 {
274 return tracing_config{};
275 }
276};
277
283using span_processor_callback = std::function<void(const span&)>;
284
302void configure_tracing(const tracing_config& config);
303
316void shutdown_tracing();
317
323void flush_tracing();
324
329[[nodiscard]] auto is_tracing_enabled() -> bool;
330
338
346void export_span(const span& s);
347
348} // namespace kcenon::network::tracing
RAII span for distributed tracing.
Definition span.h:103
tracing_config config
Definition exporters.cpp:29
exporter_type
Exporter types for trace data.
@ otlp_http
OTLP over HTTP (OpenTelemetry Collector)
@ otlp_grpc
OTLP over gRPC (OpenTelemetry Collector)
@ console
Console/stdout output (for debugging)
void flush_tracing()
Force flush all pending spans.
auto is_tracing_enabled() -> bool
Check if tracing is enabled.
void export_span(const span &s)
Export a completed span.
void register_span_processor(span_processor_callback callback)
Register a custom span processor.
sampler_type
Sampler types for trace sampling decisions.
@ trace_id
Sample based on trace ID ratio.
@ parent_based
Sample based on parent span's sampling decision.
void configure_tracing(const tracing_config &config)
Initialize the tracing system with configuration.
std::function< void(const span &)> span_processor_callback
Span processor callback type.
void shutdown_tracing()
Shutdown the tracing system.
Batch export configuration.
size_t max_export_batch_size
Maximum batch size for a single export @default 512.
std::chrono::milliseconds schedule_delay
Maximum time to wait before exporting a batch @default 5000ms.
std::chrono::milliseconds export_timeout
Timeout for export operations @default 30000ms.
size_t max_queue_size
Maximum number of spans to batch before export @default 512.
OTLP exporter configuration.
std::map< std::string, std::string > headers
Custom headers for OTLP requests.
std::string endpoint
Endpoint URL for OTLP exporter @default "http://localhost:4317" for gRPC, "http://localhost:4318" for...
std::string certificate_path
Certificate file path for TLS (optional)
bool insecure
Use insecure connection (no TLS) @default false.
std::chrono::milliseconds timeout
Connection timeout @default 10000ms.
Main configuration structure for tracing.
static auto otlp_http(const std::string &endpoint="http://localhost:4318") -> tracing_config
Create default configuration for OTLP HTTP exporter.
std::string zipkin_endpoint
Zipkin exporter endpoint @default "http://localhost:9411/api/v2/spans".
static auto console() -> tracing_config
Create default configuration with console exporter.
exporter_type exporter
Exporter type to use @default exporter_type::none.
std::string service_instance_id
Service instance ID (unique per instance) @default "" (auto-generated if empty)
sampler_type sampler
Sampler type to use @default sampler_type::always_on.
std::string service_version
Service version @default "".
std::string service_namespace
Service namespace @default "".
static auto otlp_grpc(const std::string &endpoint="http://localhost:4317") -> tracing_config
Create default configuration for OTLP gRPC exporter.
std::string jaeger_endpoint
Jaeger exporter endpoint @default "http://localhost:14268/api/traces".
double sample_rate
Sampling rate (0.0 to 1.0)
std::map< std::string, std::string > resource_attributes
Additional resource attributes.
static auto disabled() -> tracing_config
Create disabled tracing configuration.
otlp_config otlp
OTLP exporter configuration.
static auto jaeger(const std::string &endpoint="http://localhost:14268/api/traces") -> tracing_config
Create default configuration for Jaeger exporter.
std::string service_name
Service name for trace identification @default "network_system".
batch_config batch
Batch export configuration.
bool debug
Enable debug output @default false.