Skip to content

Commit 1d05045

Browse files
committed
feat: metric for method not found
1 parent 6425abd commit 1d05045

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/metrics.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ pub(crate) const ROUTER_PARSE_ERRORS: &str = "ajj.router.parse_errors";
3333
pub(crate) const ROUTER_PARSE_ERRORS_HELP: &str =
3434
"Number of parse errors encountered by ajj router methods. This implies no response was sent.";
3535

36+
/// Metric for counting method not found errors.
37+
pub(crate) const ROUTER_METHOD_NOT_FOUND: &str = "ajj.router.method_not_found";
38+
pub(crate) const ROUTER_METHOD_NOT_FOUND_HELP: &str =
39+
"Number of times ajj router methods encountered a method not found error. This implies a response was sent.";
40+
3641
static DESCRIBE: LazyLock<()> = LazyLock::new(|| {
3742
metrics::describe_counter!(ROUTER_CALLS, metrics::Unit::Count, ROUTER_CALLS_HELP);
3843
metrics::describe_counter!(ROUTER_ERRORS, metrics::Unit::Count, ROUTER_ERRORS_HELP);
@@ -56,6 +61,11 @@ static DESCRIBE: LazyLock<()> = LazyLock::new(|| {
5661
metrics::Unit::Count,
5762
ROUTER_PARSE_ERRORS_HELP
5863
);
64+
metrics::describe_counter!(
65+
ROUTER_METHOD_NOT_FOUND,
66+
metrics::Unit::Count,
67+
ROUTER_METHOD_NOT_FOUND_HELP
68+
);
5969
});
6070

6171
/// Get or register a counter for calls to a specific service and method.
@@ -168,3 +178,20 @@ pub(crate) fn record_parse_error(service_name: &'static str) {
168178
let counter = parse_errors(service_name);
169179
counter.increment(1);
170180
}
181+
182+
/// Get or register a counter for method not found errors.
183+
pub(crate) fn method_not_found_errors(service_name: &'static str, method: &str) -> Counter {
184+
let _ = &DESCRIBE;
185+
metrics::counter!(ROUTER_METHOD_NOT_FOUND, "service" => service_name.to_string(), "method" => method.to_string())
186+
}
187+
188+
/// Record a method not found error.
189+
pub(crate) fn record_method_not_found(
190+
response_sent: bool,
191+
service_name: &'static str,
192+
method: &str,
193+
) {
194+
let counter = method_not_found_errors(service_name, method);
195+
counter.increment(1);
196+
record_output(response_sent, service_name, method);
197+
}

src/routes/ctx.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,18 @@ impl HandlerArgs {
464464
self.req.id_owned()
465465
}
466466

467+
/// Get the method of the JSON-RPC request.
468+
pub fn method(&self) -> &str {
469+
self.req.method()
470+
}
471+
467472
/// Get the OpenTelemetry span name for this handler invocation.
468473
pub fn otel_span_name(&self) -> String {
469474
format!("{}/{}", self.ctx.service_name(), self.req.method())
470475
}
476+
477+
/// Get the service name for this handler invocation.
478+
pub const fn service_name(&self) -> &'static str {
479+
self.ctx.service_name()
480+
}
471481
}

src/routes/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ impl Route {
5252
pub(crate) fn default_fallback() -> Self {
5353
Self::new(tower::service_fn(|args: HandlerArgs| async {
5454
let id = args.id_owned();
55+
crate::metrics::record_method_not_found(
56+
id.is_some(),
57+
args.service_name(),
58+
args.method(),
59+
);
5560
drop(args); // no longer needed
56-
5761
Ok(Response::maybe_method_not_found(id.as_deref()))
5862
}))
5963
}

0 commit comments

Comments
 (0)