Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
WebService Class Reference
Collaboration diagram for WebService:
Collaboration graph

Public Member Functions

 WebService (distributed_tracer &tracer, const std::string &name)
 
void handle_request (const std::string &request_id, const std::map< std::string, std::string > &headers)
 

Private Member Functions

void process_business_logic (std::shared_ptr< trace_span > parent_span)
 
void query_database (std::shared_ptr< trace_span > parent_span)
 
void call_downstream_service (std::shared_ptr< trace_span > parent_span)
 

Private Attributes

distributed_tracertracer_
 
std::string service_name_
 

Detailed Description

Constructor & Destructor Documentation

◆ WebService()

WebService::WebService ( distributed_tracer & tracer,
const std::string & name )
inline
Examples
distributed_tracing_example.cpp.

Definition at line 35 of file distributed_tracing_example.cpp.

36 : tracer_(tracer), service_name_(name) {}
distributed_tracer & tracer_

Member Function Documentation

◆ call_downstream_service()

void WebService::call_downstream_service ( std::shared_ptr< trace_span > parent_span)
inlineprivate
Examples
distributed_tracing_example.cpp.

Definition at line 134 of file distributed_tracing_example.cpp.

134 {
135 // Create a child span for downstream call
136 auto span_result = tracer_.start_child_span(*parent_span, "downstream_call");
137 if (!span_result.is_ok()) return;
138
139 auto span = span_result.value();
140 span->tags["peer.service"] = "downstream_service";
141 span->tags["span.kind"] = "client";
142
143 // Extract context to propagate to downstream service
144 auto context = tracer_.extract_context(*span);
145
146 // Prepare headers for downstream call
147 std::map<std::string, std::string> headers;
148 tracer_.inject_context(context, headers);
149
150 std::cout << "[" << service_name_ << "] Calling downstream service..." << std::endl;
151 std::cout << " Propagating trace: " << context.trace_id << std::endl;
152
153 // Simulate HTTP call with headers
154 std::this_thread::sleep_for(30ms);
155
156 tracer_.finish_span(span);
157 }
trace_context extract_context(const trace_span &span) const
Extract trace context for propagation.
void inject_context(const trace_context &context, Carrier &carrier)
Inject trace context into carrier (e.g., HTTP headers)
common::Result< bool > finish_span(std::shared_ptr< trace_span > span)
Finish a span.
common::Result< std::shared_ptr< trace_span > > start_child_span(const trace_span &parent, const std::string &operation_name)
Start a child span.

References kcenon::monitoring::distributed_tracer::extract_context(), kcenon::monitoring::distributed_tracer::finish_span(), kcenon::monitoring::distributed_tracer::inject_context(), service_name_, kcenon::monitoring::distributed_tracer::start_child_span(), and tracer_.

Referenced by handle_request().

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

◆ handle_request()

void WebService::handle_request ( const std::string & request_id,
const std::map< std::string, std::string > & headers )
inline
Examples
distributed_tracing_example.cpp.

Definition at line 39 of file distributed_tracing_example.cpp.

40 {
41 std::cout << "[" << service_name_ << "] Processing request: " << request_id << std::endl;
42
43 // Extract trace context from incoming headers
44 auto context_result = tracer_.extract_context_from_carrier(headers);
45
46 std::shared_ptr<trace_span> span;
47
48 if (context_result.is_ok()) {
49 // Continue existing trace
50 auto span_result = tracer_.start_span_from_context(
51 context_result.value(),
52 service_name_ + "_handler"
53 );
54 if (span_result.is_ok()) {
55 span = span_result.value();
56 std::cout << "[" << service_name_ << "] Continuing trace: "
57 << context_result.value().trace_id << std::endl;
58 }
59 } else {
60 // Start new trace
61 auto span_result = tracer_.start_span(
62 service_name_ + "_handler",
64 );
65 if (span_result.is_ok()) {
66 span = span_result.value();
67 std::cout << "[" << service_name_ << "] Started new trace: "
68 << span->trace_id << std::endl;
69 }
70 }
71
72 if (span) {
73 // Add tags to the span
74 span->tags["service"] = service_name_;
75 span->tags["request_id"] = request_id;
76 span->tags["http.method"] = "GET";
77 span->tags["http.url"] = "/api/process";
78 span->tags["user.id"] = "user123";
79
80 // Simulate processing
82
83 // Simulate calling downstream service
85
86 // Mark span as successful
87 span->status = trace_span::status_code::ok;
88
89 // Finish the span
90 tracer_.finish_span(span);
91
92 std::cout << "[" << service_name_ << "] Span completed: "
93 << span->span_id << std::endl;
94 }
95 }
void process_business_logic(std::shared_ptr< trace_span > parent_span)
void call_downstream_service(std::shared_ptr< trace_span > parent_span)
common::Result< std::shared_ptr< trace_span > > start_span_from_context(const trace_context &context, const std::string &operation_name)
Start a span from trace context (for incoming requests)
common::Result< trace_context > extract_context_from_carrier(const Carrier &carrier)
Extract trace context from carrier.
common::Result< std::shared_ptr< trace_span > > start_span(const std::string &operation_name, const std::string &service_name="monitoring_system")
Start a new root span.

References call_downstream_service(), kcenon::monitoring::distributed_tracer::extract_context_from_carrier(), kcenon::monitoring::distributed_tracer::finish_span(), process_business_logic(), service_name_, kcenon::monitoring::distributed_tracer::start_span(), kcenon::monitoring::distributed_tracer::start_span_from_context(), and tracer_.

Referenced by simulate_distributed_system().

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

◆ process_business_logic()

void WebService::process_business_logic ( std::shared_ptr< trace_span > parent_span)
inlineprivate
Examples
distributed_tracing_example.cpp.

Definition at line 98 of file distributed_tracing_example.cpp.

98 {
99 // Create a child span for business logic
100 auto span_result = tracer_.start_child_span(*parent_span, "business_logic");
101 if (!span_result.is_ok()) return;
102
103 auto span = span_result.value();
104 span->tags["operation"] = "data_processing";
105
106 std::cout << "[" << service_name_ << "] Processing business logic..." << std::endl;
107
108 // Simulate some work
109 std::this_thread::sleep_for(50ms);
110
111 // Simulate database query
112 query_database(span);
113
114 tracer_.finish_span(span);
115 }
void query_database(std::shared_ptr< trace_span > parent_span)

References kcenon::monitoring::distributed_tracer::finish_span(), query_database(), service_name_, kcenon::monitoring::distributed_tracer::start_child_span(), and tracer_.

Referenced by handle_request().

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

◆ query_database()

void WebService::query_database ( std::shared_ptr< trace_span > parent_span)
inlineprivate
Examples
distributed_tracing_example.cpp.

Definition at line 117 of file distributed_tracing_example.cpp.

117 {
118 // Create a child span for database query
119 auto span_result = tracer_.start_child_span(*parent_span, "database_query");
120 if (!span_result.is_ok()) return;
121
122 auto span = span_result.value();
123 span->tags["db.type"] = "postgresql";
124 span->tags["db.statement"] = "SELECT * FROM users WHERE id = ?";
125
126 std::cout << "[" << service_name_ << "] Querying database..." << std::endl;
127
128 // Simulate database query
129 std::this_thread::sleep_for(20ms);
130
131 tracer_.finish_span(span);
132 }

References kcenon::monitoring::distributed_tracer::finish_span(), service_name_, kcenon::monitoring::distributed_tracer::start_child_span(), and tracer_.

Referenced by process_business_logic().

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

Member Data Documentation

◆ service_name_

std::string WebService::service_name_
private

◆ tracer_


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