Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
otel_context.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
20#include <string>
21#include <optional>
22#include <unordered_map>
23
25
54 std::string trace_id;
55
64 std::string span_id;
65
76 std::string trace_flags;
77
86 std::string trace_state;
87
93 [[nodiscard]] bool is_valid() const noexcept {
94 return trace_id.length() == 32 && span_id.length() == 16 &&
96 }
97
103 [[nodiscard]] bool is_sampled() const noexcept {
104 if (trace_flags.empty()) {
105 return false;
106 }
107 // Check if the last character is '1', '3', '5', '7', '9', 'b', 'd', 'f'
108 // (odd numbers in hex, meaning bit 0 is set)
109 char last = trace_flags.back();
110 return (last == '1' || last == '3' || last == '5' || last == '7' ||
111 last == '9' || last == 'b' || last == 'd' || last == 'f' ||
112 last == 'B' || last == 'D' || last == 'F');
113 }
114
120 [[nodiscard]] static otel_context empty() noexcept {
121 return otel_context{};
122 }
123
124private:
125 [[nodiscard]] static bool is_valid_hex(const std::string& str) noexcept {
126 for (char c : str) {
127 if (!((c >= '0' && c <= '9') ||
128 (c >= 'a' && c <= 'f') ||
129 (c >= 'A' && c <= 'F'))) {
130 return false;
131 }
132 }
133 return true;
134 }
135};
136
155 std::string service_name;
156
162 std::string service_version;
163
171 std::string service_namespace;
172
182
190 std::unordered_map<std::string, std::string> attributes;
191};
192
216public:
222 static void set(const otel_context& ctx) {
223 get_storage() = ctx;
224 get_has_context() = true;
225 }
226
232 [[nodiscard]] static std::optional<otel_context> get() {
233 if (get_has_context()) {
234 return get_storage();
235 }
236 return std::nullopt;
237 }
238
242 static void clear() {
244 get_has_context() = false;
245 }
246
252 [[nodiscard]] static bool has_context() {
253 return get_has_context();
254 }
255
256private:
258 thread_local otel_context storage;
259 return storage;
260 }
261
262 static bool& get_has_context() {
263 thread_local bool has_ctx = false;
264 return has_ctx;
265 }
266};
267
287public:
298
309
310 // Non-copyable, non-movable
315
316private:
317 std::optional<otel_context> previous_context_;
319};
320
321} // namespace kcenon::logger::otlp
RAII guard for OpenTelemetry context.
otel_context_scope(otel_context_scope &&)=delete
otel_context_scope(const otel_context &ctx)
Construct and set context.
~otel_context_scope()
Destructor - restores previous context.
otel_context_scope & operator=(otel_context_scope &&)=delete
otel_context_scope(const otel_context_scope &)=delete
otel_context_scope & operator=(const otel_context_scope &)=delete
std::optional< otel_context > previous_context_
Thread-local storage for OpenTelemetry context.
static bool has_context()
Check if a context is set for the current thread.
static std::optional< otel_context > get()
Get the OpenTelemetry context for the current thread.
static void set(const otel_context &ctx)
Set the OpenTelemetry context for the current thread.
static void clear()
Clear the OpenTelemetry context for the current thread.
OpenTelemetry context for trace correlation.
bool is_sampled() const noexcept
Check if trace is sampled.
static otel_context empty() noexcept
Create an empty/invalid context.
static bool is_valid_hex(const std::string &str) noexcept
bool is_valid() const noexcept
Check if context is valid.
OpenTelemetry resource attributes.
std::unordered_map< std::string, std::string > attributes