Skip to content

Commit bda33c7

Browse files
committed
feat: record completed calls and active calls
1 parent e263b34 commit bda33c7

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

src/metrics.rs

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
use metrics::{counter, gauge, Counter, Gauge};
12
use std::sync::LazyLock;
23

3-
use metrics::Counter;
4-
54
/// Metric name for counting router calls.
65
pub(crate) const ROUTER_CALLS: &str = "ajj.router.calls";
76
pub(crate) const ROUTER_CALLS_HELP: &str =
@@ -38,6 +37,14 @@ pub(crate) const ROUTER_METHOD_NOT_FOUND: &str = "ajj.router.method_not_found";
3837
pub(crate) const ROUTER_METHOD_NOT_FOUND_HELP: &str =
3938
"Number of times ajj router methods encountered a method not found error. This implies a response was sent.";
4039

40+
/// Metric for tracking active calls.
41+
pub(crate) const ACTIVE_CALLS: &str = "ajj.router.active_calls";
42+
pub(crate) const ACTIVE_CALLS_HELP: &str = "Number of active calls being processed";
43+
44+
/// Metric for tracking completed calls.
45+
pub(crate) const COMPLETED_CALLS: &str = "ajj.router.completed_calls";
46+
pub(crate) const COMPLETED_CALLS_HELP: &str = "Number of completed calls handled";
47+
4148
static DESCRIBE: LazyLock<()> = LazyLock::new(|| {
4249
metrics::describe_counter!(ROUTER_CALLS, metrics::Unit::Count, ROUTER_CALLS_HELP);
4350
metrics::describe_counter!(ROUTER_ERRORS, metrics::Unit::Count, ROUTER_ERRORS_HELP);
@@ -66,12 +73,14 @@ static DESCRIBE: LazyLock<()> = LazyLock::new(|| {
6673
metrics::Unit::Count,
6774
ROUTER_METHOD_NOT_FOUND_HELP
6875
);
76+
metrics::describe_gauge!(ACTIVE_CALLS, metrics::Unit::Count, ACTIVE_CALLS_HELP);
77+
metrics::describe_counter!(COMPLETED_CALLS, metrics::Unit::Count, COMPLETED_CALLS_HELP);
6978
});
7079

7180
/// Get or register a counter for calls to a specific service and method.
7281
pub(crate) fn calls(service_name: &'static str, method: &str) -> Counter {
7382
let _ = &DESCRIBE;
74-
metrics::counter!(
83+
counter!(
7584
ROUTER_CALLS,
7685
"service" => service_name.to_string(),
7786
"method" => method.to_string()
@@ -82,12 +91,13 @@ pub(crate) fn calls(service_name: &'static str, method: &str) -> Counter {
8291
pub(crate) fn record_call(service_name: &'static str, method: &str) {
8392
let counter = calls(service_name, method);
8493
counter.increment(1);
94+
increment_active_calls(service_name, method);
8595
}
8696

8797
/// Get or register a counter for errors from a specific service and method.
8898
pub(crate) fn errors(service_name: &'static str, method: &str) -> Counter {
8999
let _ = &DESCRIBE;
90-
metrics::counter!(
100+
counter!(
91101
ROUTER_ERRORS,
92102
"service" => service_name.to_string(),
93103
"method" => method.to_string()
@@ -103,7 +113,7 @@ pub(crate) fn record_execution_error(service_name: &'static str, method: &str) {
103113
/// Get or register a counter for successes from a specific service and method.
104114
pub(crate) fn successes(service_name: &'static str, method: &str) -> Counter {
105115
let _ = &DESCRIBE;
106-
metrics::counter!(
116+
counter!(
107117
ROUTER_SUCCESSES,
108118
"service" => service_name.to_string(),
109119
"method" => method.to_string()
@@ -129,7 +139,7 @@ pub(crate) fn record_execution(success: bool, service_name: &'static str, method
129139
/// Get or register a counter for responses from a specific service and method.
130140
pub(crate) fn responses(service_name: &'static str, method: &str) -> Counter {
131141
let _ = &DESCRIBE;
132-
metrics::counter!(
142+
counter!(
133143
ROUTER_RESPONSES,
134144
"service" => service_name.to_string(),
135145
"method" => method.to_string()
@@ -145,7 +155,7 @@ pub(crate) fn record_response(service_name: &'static str, method: &str) {
145155
/// Get or register a counter for omitted notification responses from a specific service and method.
146156
pub(crate) fn response_omitted(service_name: &'static str, method: &str) -> Counter {
147157
let _ = &DESCRIBE;
148-
metrics::counter!(
158+
counter!(
149159
ROUTER_NOTIFICATION_RESPONSE_OMITTED,
150160
"service" => service_name.to_string(),
151161
"method" => method.to_string()
@@ -165,12 +175,14 @@ pub(crate) fn record_output(response_sent: bool, service_name: &'static str, met
165175
} else {
166176
record_response_omitted(service_name, method);
167177
}
178+
record_completed_call(service_name, method);
179+
decrement_active_calls(service_name, method);
168180
}
169181

170182
// Get or register a counter for parse errors.
171183
pub(crate) fn parse_errors(service_name: &'static str) -> Counter {
172184
let _ = &DESCRIBE;
173-
metrics::counter!(ROUTER_PARSE_ERRORS, "service" => service_name.to_string())
185+
counter!(ROUTER_PARSE_ERRORS, "service" => service_name.to_string())
174186
}
175187

176188
/// Record a parse error.
@@ -182,7 +194,7 @@ pub(crate) fn record_parse_error(service_name: &'static str) {
182194
/// Get or register a counter for method not found errors.
183195
pub(crate) fn method_not_found_errors(service_name: &'static str, method: &str) -> Counter {
184196
let _ = &DESCRIBE;
185-
metrics::counter!(ROUTER_METHOD_NOT_FOUND, "service" => service_name.to_string(), "method" => method.to_string())
197+
counter!(ROUTER_METHOD_NOT_FOUND, "service" => service_name.to_string(), "method" => method.to_string())
186198
}
187199

188200
/// Record a method not found error.
@@ -195,3 +207,40 @@ pub(crate) fn record_method_not_found(
195207
counter.increment(1);
196208
record_output(response_sent, service_name, method);
197209
}
210+
211+
/// Get or register a gauge for active calls to a specific service.
212+
pub(crate) fn active_calls(service_name: &'static str, method: &str) -> Gauge {
213+
let _ = &DESCRIBE;
214+
gauge!(ACTIVE_CALLS, "service" => service_name.to_string(), "method" => method.to_string())
215+
}
216+
217+
/// Increment the active calls gauge for a specific service.
218+
pub(crate) fn increment_active_calls(service_name: &'static str, method: &str) {
219+
let _ = &DESCRIBE;
220+
let gauge = active_calls(service_name, method);
221+
gauge.increment(1);
222+
}
223+
224+
/// Decrement the active calls gauge for a specific service.
225+
pub(crate) fn decrement_active_calls(service_name: &'static str, method: &str) {
226+
let _ = &DESCRIBE;
227+
let gauge = active_calls(service_name, method);
228+
gauge.decrement(1);
229+
}
230+
231+
/// Get or register a counter for completed calls to a specific service.
232+
pub(crate) fn completed_calls(service_name: &'static str, method: &str) -> Counter {
233+
let _ = &DESCRIBE;
234+
counter!(
235+
COMPLETED_CALLS,
236+
"service" => service_name.to_string(),
237+
"method" => method.to_string()
238+
)
239+
}
240+
241+
/// Record a completed call to a specific service and method.
242+
pub(crate) fn record_completed_call(service_name: &'static str, method: &str) {
243+
let _ = &DESCRIBE;
244+
let counter = completed_calls(service_name, method);
245+
counter.increment(1);
246+
}

src/router.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ where
111111
self.inner.service_name()
112112
}
113113

114+
/// Set the OpenTelemetry service name for this router, overriding any
115+
/// existing name.
116+
///
117+
/// ## Note
118+
///
119+
/// Routers wrap an `Arc`. If multiple references to the router exist,
120+
/// this method will clone the inner state before setting the name.
121+
pub fn set_name(self, service_name: &'static str) -> Self {
122+
tap_inner!(self, mut this => {
123+
this.service_name = Some(service_name);
124+
})
125+
}
126+
114127
/// If this router is the only reference to its inner state, return the
115128
/// inner state. Otherwise, clone the inner state and return the clone.
116129
fn into_inner(self) -> RouterInner<S> {

0 commit comments

Comments
 (0)