Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
UserService Class Reference

Simulated User service. More...

Collaboration diagram for UserService:
Collaboration graph

Public Member Functions

trace_span get_user_profile (const trace_context &parent_ctx, const std::string &user_id)
 Fetch user profile.
 

Static Private Member Functions

static std::string generate_span_id ()
 

Private Attributes

std::string service_name_ = "user_service"
 

Detailed Description

Simulated User service.

Examples
multi_service_tracing_example.cpp.

Definition at line 175 of file multi_service_tracing_example.cpp.

Member Function Documentation

◆ generate_span_id()

static std::string UserService::generate_span_id ( )
inlinestaticprivate
Examples
multi_service_tracing_example.cpp.

Definition at line 223 of file multi_service_tracing_example.cpp.

223 {
224 std::string id;
225 for (int i = 0; i < 16; ++i) {
226 id += "0123456789abcdef"[rand() % 16];
227 }
228 return id;
229 }

Referenced by get_user_profile().

Here is the caller graph for this function:

◆ get_user_profile()

trace_span UserService::get_user_profile ( const trace_context & parent_ctx,
const std::string & user_id )
inline

Fetch user profile.

Examples
multi_service_tracing_example.cpp.

Definition at line 183 of file multi_service_tracing_example.cpp.

183 {
184 std::cout << "\n[" << service_name_ << "] Fetching user profile" << std::endl;
185 std::cout << " User ID from baggage: " << parent_ctx.baggage.at("user.id") << std::endl;
186
187 // Create child span
188 trace_span span;
189 span.trace_id = parent_ctx.trace_id;
190 span.span_id = generate_span_id();
191 span.parent_span_id = parent_ctx.span_id;
192 span.operation_name = "get_user_profile";
194 span.start_time = std::chrono::system_clock::now();
195
196 // Inherit baggage
197 span.baggage = parent_ctx.baggage;
198
199 // Add database query tags
200 span.tags["db.system"] = "postgresql";
201 span.tags["db.name"] = "users_db";
202 span.tags["db.statement"] = "SELECT * FROM users WHERE id = ?";
203 span.tags["db.user_id"] = user_id;
204
205 std::cout << " → Created child span: " << span.span_id << std::endl;
206 std::cout << " Parent span: " << span.parent_span_id << std::endl;
207
208 // Simulate database query
209 std::this_thread::sleep_for(15ms);
210
211 span.end_time = std::chrono::system_clock::now();
212 span.calculate_duration();
213 span.status = trace_span::status_code::ok;
214 span.tags["db.rows_returned"] = "1";
215
216 std::cout << " ✓ User profile fetched (duration: "
217 << span.duration.count() << "µs)" << std::endl;
218
219 return span;
220 }
static std::string generate_span_id()
std::unordered_map< std::string, std::string > baggage
Trace span representing a unit of work in distributed tracing.
void calculate_duration()
Calculate duration if span is finished.
std::unordered_map< std::string, std::string > tags
std::unordered_map< std::string, std::string > baggage
std::chrono::system_clock::time_point end_time
std::chrono::microseconds duration
std::chrono::system_clock::time_point start_time

References kcenon::monitoring::trace_context::baggage, kcenon::monitoring::trace_span::baggage, kcenon::monitoring::trace_span::calculate_duration(), kcenon::monitoring::trace_span::duration, kcenon::monitoring::trace_span::end_time, generate_span_id(), kcenon::monitoring::trace_span::operation_name, kcenon::monitoring::trace_span::parent_span_id, kcenon::monitoring::trace_span::service_name, service_name_, kcenon::monitoring::trace_context::span_id, kcenon::monitoring::trace_span::span_id, kcenon::monitoring::trace_span::start_time, kcenon::monitoring::trace_span::status, kcenon::monitoring::trace_span::tags, kcenon::monitoring::trace_context::trace_id, and kcenon::monitoring::trace_span::trace_id.

Referenced by simulate_multi_service_request().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ service_name_

std::string UserService::service_name_ = "user_service"
private

The documentation for this class was generated from the following file: