Skip to content

Commit

Permalink
webapp: Aggregate graph endpoints for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Nov 20, 2024
1 parent 7e81f97 commit 6f690ab
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions repology-webapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,36 @@ use crate::static_files::STATIC_FILES;

async fn track_metrics(matched_path: MatchedPath, req: Request, next: Next) -> impl IntoResponse {
let start = Instant::now();
// normalize some paths which lead to the same endpoints; XXX this will hopefully be gone
// someday when endpoints are redesigned (e.g. /projects/:bound/ → /projects/?start=)
let path = matched_path
.as_str()
.trim_end_matches(":bound/")
.trim_end_matches("/:sorting")
.to_owned();

let path_for_metrics = {
// normalize some paths which lead to the same endpoints; XXX this will hopefully be gone
// someday when endpoints are redesigned (e.g. /projects/:bound/ → /projects/?start=)
let mut path = matched_path
.as_str()
.trim_end_matches(":bound/")
.trim_end_matches("/:sorting");
if path.starts_with("/graph/total/") {
path = "/graph/total/..."
}
if path.starts_with("/graph/repo/") {
path = "/graph/repo/..."
}

path.to_owned()
};

let response = next.run(req).await;

let latency = start.elapsed().as_secs_f64();
let status = response.status().as_u16().to_string();

counter!("repology_webapp_http_requests_total", "path" => path.clone(), "status" => status)
counter!("repology_webapp_http_requests_total", "path" => path_for_metrics.clone(), "status" => status)
.increment(1);
histogram!("repology_webapp_http_requests_duration_seconds", "path" => path.clone())
histogram!("repology_webapp_http_requests_duration_seconds", "path" => path_for_metrics.clone())
.record(latency);

if let Some(body_size) = response.body().size_hint().exact() {
histogram!("repology_webapp_http_response_size_bytes", "path" => path)
histogram!("repology_webapp_http_response_size_bytes", "path" => path_for_metrics)
.record(body_size as f64);
}

Expand Down

0 comments on commit 6f690ab

Please sign in to comment.