Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
test_thread_context_simple.cpp
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
11#include <gtest/gtest.h>
13
14using namespace kcenon::monitoring;
15
19TEST(ThreadContextSimpleTest, ContextMetadataBasics) {
20 // Test constructor
21 context_metadata metadata("test-request");
22 EXPECT_EQ(metadata.request_id, "test-request");
23 EXPECT_TRUE(metadata.correlation_id.empty());
24 EXPECT_TRUE(metadata.user_id.empty());
25 EXPECT_FALSE(metadata.empty()); // Has request_id, so not empty
26
27 // Test set_tag and get_tag
28 metadata.set_tag("environment", "test");
29 metadata.set_tag("version", "1.0.0");
30
31 EXPECT_EQ(metadata.get_tag("environment"), "test");
32 EXPECT_EQ(metadata.get_tag("version"), "1.0.0");
33 EXPECT_EQ(metadata.get_tag("nonexistent"), ""); // Returns empty string
34
35 // Test setting other fields
36 metadata.correlation_id = "corr-123";
37 metadata.user_id = "user-456";
38
39 EXPECT_EQ(metadata.correlation_id, "corr-123");
40 EXPECT_EQ(metadata.user_id, "user-456");
41}
42
46TEST(ThreadContextSimpleTest, ContextMetadataEmpty) {
47 // Empty constructor
48 context_metadata empty_metadata;
49 EXPECT_TRUE(empty_metadata.empty());
50 EXPECT_TRUE(empty_metadata.request_id.empty());
51 EXPECT_TRUE(empty_metadata.correlation_id.empty());
52 EXPECT_TRUE(empty_metadata.user_id.empty());
53 EXPECT_TRUE(empty_metadata.tags.empty());
54
55 // Add a tag - should not be empty anymore
56 empty_metadata.set_tag("test", "value");
57 EXPECT_FALSE(empty_metadata.empty());
58}
59
63TEST(ThreadContextSimpleTest, ThreadContextData) {
65
66 // Check default values
67 EXPECT_TRUE(data.request_id.empty());
68 EXPECT_TRUE(data.correlation_id.empty());
69 EXPECT_TRUE(data.user_id.empty());
70 EXPECT_TRUE(data.span_id.empty());
71 EXPECT_TRUE(data.trace_id.empty());
72 EXPECT_FALSE(data.parent_span_id.has_value());
73 EXPECT_TRUE(data.tags.empty());
74
75 // Set values
76 data.request_id = "req-123";
77 data.trace_id = "trace-456";
78 data.span_id = "span-789";
79 data.parent_span_id = "parent-span-101";
80 data.tags["env"] = "test";
81
82 EXPECT_EQ(data.request_id, "req-123");
83 EXPECT_EQ(data.trace_id, "trace-456");
84 EXPECT_EQ(data.span_id, "span-789");
85 EXPECT_TRUE(data.parent_span_id.has_value());
86 EXPECT_EQ(data.parent_span_id.value(), "parent-span-101");
87 EXPECT_EQ(data.tags["env"], "test");
88}
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.
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.
std::string span_id
Current span ID for distributed tracing.
std::string request_id
Unique identifier for the current request.
std::string correlation_id
Correlation ID for cross-service tracing.
std::string trace_id
Trace ID linking all spans in a trace.
std::unordered_map< std::string, std::string > tags
Arbitrary key-value tags.
std::optional< std::string > parent_span_id
Parent span ID (if nested)
std::string user_id
User identifier associated with the request.
TEST(ThreadContextSimpleTest, ContextMetadataBasics)
Thread-local context management for request tracking and distributed tracing.