Skip to content

Commit

Permalink
Adding metrics for country hit/miss (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcottner authored Jun 7, 2024
1 parent 034f0d7 commit 465b05c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/endpoints/country.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ pub async fn get_country(
req: HttpRequest,
state: Data<EndpointState>,
) -> Result<HttpResponse, ClassifyError> {
let metrics = &state.metrics;

// check provided API Key
match Query::<Params>::from_query(req.query_string()) {
Ok(req_query) => {
state
.metrics
metrics
.incr_with_tags("country")
.with_tag("api_key", &req_query.key)
.send();
Expand Down Expand Up @@ -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);
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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",
]
);
Expand Down

0 comments on commit 465b05c

Please sign in to comment.