Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
trace_context.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 <array>
19#include <cstdint>
20#include <optional>
21#include <string>
22#include <string_view>
23#include <utility>
24#include <vector>
25
27
28// Forward declarations
29class span;
30
34using trace_id_t = std::array<uint8_t, 16>;
35
39using span_id_t = std::array<uint8_t, 8>;
40
44enum class trace_flags : uint8_t
45{
46 none = 0x00,
47 sampled = 0x01
48};
49
78{
79public:
83 trace_context() = default;
84
93 std::optional<span_id_t> parent_span_id = std::nullopt);
94
99 [[nodiscard]] static auto current() -> trace_context;
100
106 [[nodiscard]] static auto create_span(std::string_view name) -> span;
107
113 [[nodiscard]] auto create_child_span(std::string_view name) const -> span;
114
122 [[nodiscard]] auto to_traceparent() const -> std::string;
123
130 [[nodiscard]] auto to_headers() const
131 -> std::vector<std::pair<std::string, std::string>>;
132
138 [[nodiscard]] static auto from_traceparent(std::string_view traceparent)
139 -> trace_context;
140
146 [[nodiscard]] static auto from_headers(
147 const std::vector<std::pair<std::string, std::string>>& headers)
148 -> trace_context;
149
154 [[nodiscard]] auto is_valid() const noexcept -> bool;
155
160 [[nodiscard]] auto is_sampled() const noexcept -> bool;
161
166 [[nodiscard]] auto trace_id() const noexcept -> const trace_id_t&;
167
172 [[nodiscard]] auto span_id() const noexcept -> const span_id_t&;
173
178 [[nodiscard]] auto parent_span_id() const noexcept
179 -> const std::optional<span_id_t>&;
180
185 [[nodiscard]] auto flags() const noexcept -> trace_flags;
186
191 [[nodiscard]] auto trace_id_hex() const -> std::string;
192
197 [[nodiscard]] auto span_id_hex() const -> std::string;
198
202 [[nodiscard]] auto operator==(const trace_context& other) const noexcept
203 -> bool;
204
208 [[nodiscard]] auto operator!=(const trace_context& other) const noexcept
209 -> bool;
210
211private:
214 std::optional<span_id_t> parent_span_id_;
216 bool valid_{false};
217
222 static void set_current(const trace_context& ctx);
223
227 static void clear_current();
228
229 // span class needs access to set_current
230 friend class span;
231};
232
237[[nodiscard]] auto generate_trace_id() -> trace_id_t;
238
243[[nodiscard]] auto generate_span_id() -> span_id_t;
244
251[[nodiscard]] auto bytes_to_hex(const uint8_t* data, size_t size) -> std::string;
252
260[[nodiscard]] auto hex_to_bytes(std::string_view hex, uint8_t* out, size_t size)
261 -> bool;
262
263} // namespace kcenon::network::tracing
RAII span for distributed tracing.
Definition span.h:103
Immutable trace context for distributed tracing.
static auto create_span(std::string_view name) -> span
Create a new root span with a new trace context.
auto trace_id() const noexcept -> const trace_id_t &
Get the trace ID.
auto span_id_hex() const -> std::string
Convert span ID to hex string.
static auto current() -> trace_context
Get the current trace context from thread-local storage.
static void clear_current()
Clear the current thread-local trace context.
auto trace_id_hex() const -> std::string
Convert trace ID to hex string.
static auto from_headers(const std::vector< std::pair< std::string, std::string > > &headers) -> trace_context
Parse trace context from HTTP headers.
auto to_headers() const -> std::vector< std::pair< std::string, std::string > >
Convert context to HTTP headers for propagation.
static auto from_traceparent(std::string_view traceparent) -> trace_context
Parse trace context from W3C traceparent header.
auto parent_span_id() const noexcept -> const std::optional< span_id_t > &
Get the parent span ID.
auto is_sampled() const noexcept -> bool
Check if this trace is sampled.
auto flags() const noexcept -> trace_flags
Get the trace flags.
auto create_child_span(std::string_view name) const -> span
Create a child span inheriting this context.
auto is_valid() const noexcept -> bool
Check if this context is valid.
std::optional< span_id_t > parent_span_id_
static void set_current(const trace_context &ctx)
Set the current thread-local trace context.
auto span_id() const noexcept -> const span_id_t &
Get the span ID.
trace_context()=default
Default constructor creates an invalid context.
auto to_traceparent() const -> std::string
Convert context to W3C traceparent header value.
auto bytes_to_hex(const uint8_t *data, size_t size) -> std::string
Convert bytes to lowercase hex string.
trace_flags
Trace flags (8-bit)
auto generate_span_id() -> span_id_t
Generate a random span ID.
std::array< uint8_t, 8 > span_id_t
Span ID type (64-bit identifier)
auto generate_trace_id() -> trace_id_t
Generate a random trace ID.
auto hex_to_bytes(std::string_view hex, uint8_t *out, size_t size) -> bool
Parse hex string to bytes.
std::array< uint8_t, 16 > trace_id_t
Trace ID type (128-bit identifier)