Skip to content

Commit aac9f88

Browse files
committed
feat(http/prom): add an error labeler
this commit introduces a `LabelError<E>` type, which provides a `StreamLabel` implementation that maps boxed errors to labels. this is generic across `E`-typed labels that can be constructed `From` a reference to a boxed `linkerd_error::Error`. Signed-off-by: katelyn martin <kate@buoyant.io>
1 parent 8c07b5a commit aac9f88

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

linkerd/http/prom/src/stream_label.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use linkerd_error::Error;
44
use prometheus_client::encoding::EncodeLabelSet;
55

6+
pub mod error;
67
pub mod status;
78

89
/// A strategy for labeling request/responses streams for status and duration
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//! [`StreamLabel`] implementation for labeling errors.
2+
3+
use super::StreamLabel;
4+
use linkerd_error::Error;
5+
6+
/// A [`StreamLabel`] implementation that maps boxed errors to labels.
7+
#[derive(Clone, Debug, Default)]
8+
pub struct LabelError<E> {
9+
error: Option<E>,
10+
}
11+
12+
// === impl LabelError ===
13+
14+
impl<E> StreamLabel for LabelError<E>
15+
where
16+
E: for<'a> From<&'a Error>,
17+
E: Clone + Send + 'static,
18+
{
19+
type DurationLabels = ();
20+
type StatusLabels = Option<E>;
21+
22+
fn init_response<B>(&mut self, _: &http::Response<B>) {}
23+
24+
fn end_response(&mut self, res: Result<Option<&http::HeaderMap>, &Error>) {
25+
let Err(err) = res else { return };
26+
let labels = E::from(err);
27+
self.error = Some(labels);
28+
}
29+
30+
fn status_labels(&self) -> Self::StatusLabels {
31+
self.error.clone()
32+
}
33+
34+
fn duration_labels(&self) -> Self::DurationLabels {}
35+
}

0 commit comments

Comments
 (0)