Skip to content

Commit 9c5f761

Browse files
committed
fix: wip
1 parent cad0e9a commit 9c5f761

File tree

1 file changed

+66
-32
lines changed

1 file changed

+66
-32
lines changed

src/api/metrics.rs

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ lazy_static! {
2828
.unwrap();
2929
}
3030

31+
const fn test() {}
32+
3133
pub(super) enum MetricsConfig {
3234
Borrowed(&'static str),
3335
Owned(FullPath),
@@ -53,44 +55,76 @@ impl MetricsConfig {
5355
}
5456
}
5557

58+
trait MetricsWrapper<I>: Fn(I) -> <Self as MetricsWrapper<I>>::Output
59+
where
60+
I: Filter<Extract = (WarpResponse, MetricsConfig), Error = Rejection> + Clone + Send + 'static,
61+
{
62+
type Output: Filter<Extract = (WarpResponse,), Error = Rejection> + Clone + Send + 'static;
63+
}
64+
65+
impl<F, I, O> MetricsWrapper<I> for F
66+
where
67+
F: Fn(I) -> O,
68+
I: Filter<Extract = (WarpResponse, MetricsConfig), Error = Rejection> + Clone + Send + 'static,
69+
O: Filter<Extract = (WarpResponse,), Error = Rejection> + Clone + Send + 'static,
70+
{
71+
type Output = O;
72+
}
73+
74+
fn hihi<I>(
75+
http_req_histogram: &'static HistogramVec,
76+
http_status_counter: &'static IntCounterVec,
77+
) -> impl MetricsWrapper<I>
78+
where
79+
I: Filter<Extract = (WarpResponse, MetricsConfig), Error = Rejection> + Clone + Send + 'static,
80+
{
81+
move |filter: I| {
82+
warp::filters::method::method()
83+
.map(move |method| {
84+
let timer = http_req_histogram
85+
.with_label_values(&["", ""])
86+
.start_timer()
87+
.into();
88+
(method, timer)
89+
})
90+
.untuple_one()
91+
.and(filter)
92+
.map(
93+
move |method: Method,
94+
mut timer: HistogramTimerWrapper,
95+
res: WarpResponse,
96+
config: MetricsConfig| {
97+
http_status_counter
98+
.with_label_values(&[
99+
config.as_str(),
100+
method.as_str(),
101+
res.status().as_str(),
102+
])
103+
.inc();
104+
105+
if let Some(timer) = timer.take() {
106+
let time = timer.stop_and_discard();
107+
108+
http_req_histogram
109+
.with_label_values(&[config.as_str(), method.as_str()])
110+
.observe(time);
111+
112+
debug!("request took {}ms", time * 1000f64);
113+
}
114+
115+
res
116+
},
117+
)
118+
}
119+
}
120+
56121
pub(super) fn metrics_wrapper<F>(
57122
filter: F,
58123
) -> impl Filter<Extract = (WarpResponse,), Error = Rejection> + Clone + Send + 'static
59124
where
60125
F: Filter<Extract = (WarpResponse, MetricsConfig), Error = Rejection> + Clone + Send + 'static,
61126
{
62-
warp::filters::method::method()
63-
.map(|method| {
64-
let timer = HTTP_REQ_HISTOGRAM
65-
.with_label_values(&["", ""])
66-
.start_timer()
67-
.into();
68-
(method, timer)
69-
})
70-
.untuple_one()
71-
.and(filter)
72-
.map(
73-
|method: Method,
74-
mut timer: HistogramTimerWrapper,
75-
res: WarpResponse,
76-
config: MetricsConfig| {
77-
HTTP_STATUS_COUNTER
78-
.with_label_values(&[config.as_str(), method.as_str(), res.status().as_str()])
79-
.inc();
80-
81-
if let Some(timer) = timer.take() {
82-
let time = timer.stop_and_discard();
83-
84-
HTTP_REQ_HISTOGRAM
85-
.with_label_values(&[config.as_str(), method.as_str()])
86-
.observe(time);
87-
88-
debug!("request took {}ms", time * 1000f64);
89-
}
90-
91-
res
92-
},
93-
)
127+
hihi(&*HTTP_REQ_HISTOGRAM, &*HTTP_STATUS_COUNTER)(filter)
94128
}
95129

96130
fn internal_server_error_and_trace<E: Display>(error: &E) -> WarpResponse {

0 commit comments

Comments
 (0)