Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
thread_context.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
42#include <string>
43#include <chrono>
44#include <optional>
45#include <unordered_map>
46#include <memory>
47
48namespace kcenon { namespace monitoring {
49
58 std::string request_id;
59 std::string correlation_id;
60 std::string user_id;
61 std::unordered_map<std::string, std::string> tags;
62
67 explicit context_metadata(std::string req_id = "")
68 : request_id(std::move(req_id)) {}
69
74 bool empty() const {
75 return request_id.empty() && correlation_id.empty() && user_id.empty() && tags.empty();
76 }
77
83 void set_tag(const std::string& key, const std::string& value) {
84 tags[key] = value;
85 }
86
92 std::string get_tag(const std::string& key) const {
93 auto it = tags.find(key);
94 return it != tags.end() ? it->second : "";
95 }
96};
97
108 std::string request_id;
109 std::string correlation_id;
110 std::string user_id;
111 std::string span_id;
112 std::string trace_id;
113 std::chrono::steady_clock::time_point start_time;
114 std::optional<std::string> parent_span_id;
115 std::unordered_map<std::string, std::string> tags;
116
120 thread_context_data() : start_time(std::chrono::steady_clock::now()) {}
121
126 explicit thread_context_data(std::string req_id)
127 : request_id(std::move(req_id))
128 , start_time(std::chrono::steady_clock::now()) {}
129
135 void add_tag(const std::string& key, const std::string& value) {
136 tags[key] = value;
137 }
138
144 std::string get_tag(const std::string& key) const {
145 auto it = tags.find(key);
146 return it != tags.end() ? it->second : "";
147 }
148};
149
176public:
183 static thread_context_data& create(const std::string& request_id = "");
184
190
195 static bool has_context();
196
200 static void clear();
201
206 static std::string generate_request_id();
207
212 static std::string generate_correlation_id();
213
219 static bool copy_from(const thread_context_data& source);
220
221private:
222 static thread_local std::unique_ptr<thread_context_data> current_context_;
223};
224
225} } // namespace kcenon::monitoring
Thread-local context management for request tracking.
static std::string generate_correlation_id()
Generate a unique correlation ID.
static bool copy_from(const thread_context_data &source)
Copy context data from another source into the current thread.
static void clear()
Clear and destroy the current thread-local context.
static thread_context_data * current()
Get the current thread-local context.
static thread_local std::unique_ptr< thread_context_data > current_context_
static thread_context_data & create(const std::string &request_id="")
Create a new thread-local context, replacing any existing one.
static std::string generate_request_id()
Generate a unique request ID.
static bool has_context()
Check whether a context exists on the current thread.
Context metadata for thread-specific information.
std::string correlation_id
Correlation ID for tracing across services.
std::unordered_map< std::string, std::string > tags
Arbitrary key-value tags.
std::string request_id
Unique identifier for the current request.
context_metadata(std::string req_id="")
Construct context metadata with an optional request ID.
std::string user_id
User identifier associated with the request.
std::string get_tag(const std::string &key) const
Retrieve a tag value by key.
void set_tag(const std::string &key, const std::string &value)
Set a custom tag on this context.
bool empty() const
Check if all metadata fields are empty.
Enhanced thread context data for comprehensive request and trace tracking.
thread_context_data(std::string req_id)
Construct with a request ID. Records the current time as start_time.
std::chrono::steady_clock::time_point start_time
Context creation timestamp.
std::string span_id
Current span ID for distributed tracing.
void add_tag(const std::string &key, const std::string &value)
Add or update a custom tag.
std::string request_id
Unique identifier for the current request.
std::string correlation_id
Correlation ID for cross-service tracing.
std::string get_tag(const std::string &key) const
Retrieve a tag value by key.
std::string trace_id
Trace ID linking all spans in a trace.
std::unordered_map< std::string, std::string > tags
Arbitrary key-value tags.
thread_context_data()
Default constructor. Records the current time as start_time.
std::optional< std::string > parent_span_id
Parent span ID (if nested)
std::string user_id
User identifier associated with the request.