From 465b05cac986200f973bdba79c1a0fc6311f7f95 Mon Sep 17 00:00:00 2001 From: Alex Cottner <148472676+alexcottner@users.noreply.github.com> Date: Fri, 7 Jun 2024 10:29:53 -0600 Subject: [PATCH] Adding metrics for country hit/miss (#157) --- src/endpoints/country.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/endpoints/country.rs b/src/endpoints/country.rs index 4940d7e..592d580 100644 --- a/src/endpoints/country.rs +++ b/src/endpoints/country.rs @@ -47,11 +47,12 @@ pub async fn get_country( req: HttpRequest, state: Data, ) -> Result { + let metrics = &state.metrics; + // check provided API Key match Query::::from_query(req.query_string()) { Ok(req_query) => { - state - .metrics + metrics .incr_with_tags("country") .with_tag("api_key", &req_query.key) .send(); @@ -81,6 +82,7 @@ pub async fn get_country( if country_opt.is_none() { let mut response = HttpResponse::NotFound(); + metrics.incr_with_tags("country_miss").send(); return response.json(&COUNTRY_NOT_FOUND_RESPONSE); } @@ -90,6 +92,8 @@ pub async fn get_country( "max-age=0, no-cache, no-store, must-revalidate", )); + metrics.incr_with_tags("country_hit").send(); + let country = country_opt.unwrap(); response.json(CountryResponse { country_code: match country.iso_code { @@ -236,8 +240,11 @@ mod tests { *log.lock().unwrap().deref(), vec![ "test.country:1|c|#api_key:testkey", + "test.country_miss:1|c", "test.country:1|c|#api_key:testkey", + "test.country_hit:1|c", "test.country:1|c|#api_key:firefox-downstream-foo_bar", + "test.country_hit:1|c", "test.country:1|c|#api_key:firefox-downstream-foo-bar", ] );