Skip to content

Commit da6e10c

Browse files
olix0rcratelyn
authored andcommitted
Update ResponseBody metrics to use a histogram
Signed-off-by: Oliver Gould <[email protected]>
1 parent f147343 commit da6e10c

File tree

3 files changed

+35
-55
lines changed

3 files changed

+35
-55
lines changed

linkerd/http/prom/src/body_data/body.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ where
4141
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
4242
let this = self.project();
4343
let inner = this.inner;
44-
let BodyDataMetrics {
45-
frames_total,
46-
frames_bytes,
47-
} = this.metrics;
44+
let BodyDataMetrics { frame_size } = this.metrics;
4845

4946
let data = std::task::ready!(inner.poll_data(cx));
5047

@@ -53,11 +50,8 @@ where
5350
//
5451
// NB: We're careful to call `remaining()` rather than `chunk()`, which
5552
// "can return a shorter slice (this allows non-continuous internal representation)."
56-
let bytes = <B::Data as bytes::Buf>::remaining(data)
57-
.try_into()
58-
.unwrap_or(u64::MAX);
59-
frames_bytes.inc_by(bytes);
60-
frames_total.inc();
53+
let bytes = bytes::Buf::remaining(data);
54+
frame_size.observe(linkerd_metrics::to_f64(bytes as u64));
6155
}
6256

6357
Poll::Ready(data)
Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
//! Prometheus counters for request and response bodies.
22
3-
use linkerd_metrics::prom::{self, Counter, Family, Registry};
3+
use linkerd_metrics::prom::{
4+
self, metrics::family::MetricConstructor, Family, Histogram, Registry, Unit,
5+
};
46

57
/// Counters for response body frames.
68
#[derive(Clone, Debug)]
79
pub struct ResponseBodyFamilies<L> {
8-
/// Counts the number of response body frames.
9-
resp_body_frames_total: Family<L, Counter>,
10-
/// Counts the total number of bytes in response body frames.
11-
resp_body_frames_bytes: Family<L, Counter>,
10+
/// Counts the number of response body frames by size.
11+
frame_sizes: Family<L, Histogram, NewHisto>,
1212
}
1313

1414
/// Counters to instrument a request or response body.
15-
#[derive(Clone, Debug, Default)]
15+
#[derive(Clone, Debug)]
1616
pub struct BodyDataMetrics {
1717
/// Counts the number of request body frames.
18-
pub frames_total: Counter,
19-
/// Counts the total number of bytes in request body frames.
20-
pub frames_bytes: Counter,
18+
pub frame_size: Histogram,
19+
}
20+
21+
#[derive(Clone, Copy)]
22+
struct NewHisto;
23+
24+
impl MetricConstructor<Histogram> for NewHisto {
25+
fn new_metric(&self) -> Histogram {
26+
Histogram::new([128.0, 1024.0, 10240.0].into_iter())
27+
}
2128
}
2229

2330
// === impl ResponseBodyFamilies ===
@@ -28,8 +35,7 @@ where
2835
{
2936
fn default() -> Self {
3037
Self {
31-
resp_body_frames_total: Default::default(),
32-
resp_body_frames_bytes: Default::default(),
38+
frame_sizes: Family::new_with_constructor(NewHisto),
3339
}
3440
}
3541
}
@@ -45,50 +51,25 @@ where
4551
+ Sync
4652
+ 'static,
4753
{
48-
const RESP_BODY_FRAMES_TOTAL_NAME: &'static str = "resp_body_frames_total";
49-
const RESP_BODY_FRAMES_TOTAL_HELP: &'static str =
50-
"Counts the number of frames in response bodies.";
51-
52-
const RESP_BODY_FRAMES_BYTES_NAME: &'static str = "resp_body_frames_bytes";
53-
const RESP_BODY_FRAMES_BYTES_HELP: &'static str =
54-
"Counts the total number of bytes in response bodies.";
55-
5654
/// Registers and returns a new family of body data metrics.
5755
pub fn register(registry: &mut Registry) -> Self {
58-
let resp_body_frames_total = Family::default();
59-
registry.register(
60-
Self::RESP_BODY_FRAMES_TOTAL_NAME,
61-
Self::RESP_BODY_FRAMES_TOTAL_HELP,
62-
resp_body_frames_total.clone(),
63-
);
64-
65-
let resp_body_frames_bytes = Family::default();
56+
let frame_sizes = Family::new_with_constructor(NewHisto);
6657
registry.register_with_unit(
67-
Self::RESP_BODY_FRAMES_BYTES_NAME,
68-
Self::RESP_BODY_FRAMES_BYTES_HELP,
69-
prom::Unit::Bytes,
70-
resp_body_frames_bytes.clone(),
58+
"response_frame_size",
59+
"Response data frame sizes",
60+
Unit::Bytes,
61+
frame_sizes.clone(),
7162
);
7263

73-
Self {
74-
resp_body_frames_total,
75-
resp_body_frames_bytes,
76-
}
64+
Self { frame_sizes }
7765
}
7866

7967
/// Returns the [`BodyDataMetrics`] for the given label set.
8068
pub fn metrics(&self, labels: &L) -> BodyDataMetrics {
81-
let Self {
82-
resp_body_frames_total,
83-
resp_body_frames_bytes,
84-
} = self;
69+
let Self { frame_sizes } = self;
8570

86-
let frames_total = resp_body_frames_total.get_or_create(labels).clone();
87-
let frames_bytes = resp_body_frames_bytes.get_or_create(labels).clone();
71+
let frame_size = frame_sizes.get_or_create(labels).clone();
8872

89-
BodyDataMetrics {
90-
frames_total,
91-
frames_bytes,
92-
}
73+
BodyDataMetrics { frame_size }
9374
}
9475
}

linkerd/metrics/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ pub trait Factor {
8585
const MAX_PRECISE_UINT64: u64 = 0x20_0000_0000_0000;
8686

8787
impl Factor for () {
88+
#[inline]
8889
fn factor(n: u64) -> f64 {
89-
n.wrapping_rem(MAX_PRECISE_UINT64 + 1) as f64
90+
to_f64(n)
9091
}
9192
}
93+
94+
pub fn to_f64(n: u64) -> f64 {
95+
n.wrapping_rem(MAX_PRECISE_UINT64 + 1) as f64
96+
}

0 commit comments

Comments
 (0)