Skip to content

Commit 92ca57a

Browse files
committed
track responses by status code
1 parent ae201a8 commit 92ca57a

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/metrics/instance.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
2020
use crate::util::errors::AppResult;
2121
use crate::{app::App, db::DieselPool};
22-
use prometheus::{proto::MetricFamily, IntCounter, IntGauge, IntGaugeVec, HistogramVec};
22+
use prometheus::{
23+
proto::MetricFamily, HistogramVec, IntCounter, IntCounterVec, IntGauge, IntGaugeVec,
24+
};
2325

2426
metrics! {
2527
pub struct InstanceMetrics {
@@ -35,6 +37,8 @@ metrics! {
3537

3638
/// Response times of our endpoints
3739
pub response_times: HistogramVec["endpoint"],
40+
/// Nmber of responses per status code
41+
pub responses_by_status_code_total: IntCounterVec["status"],
3842

3943
/// Number of download requests that were served with an unconditional redirect.
4044
pub downloads_unconditional_redirects_total: IntCounter,

src/metrics/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ mod service;
1111

1212
load_metric_type!(IntGauge as single);
1313
load_metric_type!(IntCounter as single);
14+
load_metric_type!(IntCounterVec as vec);
1415
load_metric_type!(IntGaugeVec as vec);
1516
load_metric_type!(HistogramVec as vec);

src/middleware/update_metrics.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ impl Middleware for UpdateMetrics {
3030
.with_label_values(&[endpoint])
3131
.observe(req.elapsed().as_millis() as f64 / 1000.0);
3232

33+
let status = match &res {
34+
Ok(res) => res.status().as_u16(),
35+
Err(_) => 500,
36+
};
37+
metrics
38+
.responses_by_status_code_total
39+
.with_label_values(&[&status.to_string()])
40+
.inc();
41+
3342
res
3443
}
3544
}

0 commit comments

Comments
 (0)