Skip to content

Commit

Permalink
feat(http/metrics): count response body frames along with number of r…
Browse files Browse the repository at this point in the history
…esponses

Signed-off-by: Aleksandr Fedotov <nerodono0@gmail.com>
  • Loading branch information
nerodono committed Jul 1, 2024
1 parent ca1daf3 commit 7fe13ec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions linkerd/http/metrics/src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ where
{
last_update: Instant,
total: Counter,
response_frames_total: Counter,
by_status: HashMap<Option<http::StatusCode>, StatusMetrics<C>>,
}

Expand Down Expand Up @@ -82,6 +83,7 @@ impl<C: Hash + Eq> Default for Metrics<C> {
Self {
last_update: Instant::now(),
total: Counter::default(),
response_frames_total: Counter::default(),
by_status: HashMap::default(),
}
}
Expand Down
11 changes: 11 additions & 0 deletions linkerd/http/metrics/src/requests/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ where
)
}

fn response_frames_total(&self) -> Metric<'_, Prefixed<'_, &'static str>, Counter> {
Metric::new(
self.prefix_key("response_frames_total"),
"Total number of chunks sent through HTTP as the response.",
)
}

fn response_latency_ms(
&self,
) -> Metric<'_, Prefixed<'_, &'static str>, Histogram<latency::Ms>> {
Expand Down Expand Up @@ -128,6 +135,10 @@ where
metric.fmt_help(f)?;
Self::fmt_by_target(&registry, f, metric, |s| &s.total)?;

let metric = self.response_frames_total();
metric.fmt_help(f)?;
Self::fmt_by_target(&registry, f, metric, |s| &s.response_frames_total)?;

if self.include_latencies {
let metric = self.response_latency_ms();
metric.fmt_help(f)?;
Expand Down
15 changes: 11 additions & 4 deletions linkerd/http/metrics/src/requests/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,8 @@ where
C: ClassifyEos,
C::Class: Hash + Eq,
{
fn record_latency(self: Pin<&mut Self>) {
fn record_latency(self: Pin<&mut Self>, now: Instant) {
let this = self.project();
let now = Instant::now();

let lock = match this.metrics.as_mut() {
Some(lock) => lock,
Expand Down Expand Up @@ -377,6 +376,11 @@ where
}
}

fn count_frame<C: Hash + Eq>(lock: &Mutex<Metrics<C>>) {
let metrics = lock.lock();
metrics.response_frames_total.incr();
}

fn measure_class<C: Hash + Eq>(
lock: &Arc<Mutex<Metrics<C>>>,
class: C,
Expand Down Expand Up @@ -415,8 +419,11 @@ where
let poll = ready!(self.as_mut().project().inner.poll_data(cx));
let frame = poll.map(|opt| opt.map_err(|e| self.as_mut().measure_err(e.into())));

if let Some(lock) = self.metrics.as_ref().map(Arc::as_ref) {
count_frame(lock);
}
if !(*self.as_mut().project().latency_recorded) {
self.record_latency();
self.record_latency(Instant::now());
}

Poll::Ready(frame)
Expand Down Expand Up @@ -457,7 +464,7 @@ where
{
fn drop(mut self: Pin<&mut Self>) {
if !self.as_ref().latency_recorded {
self.as_mut().record_latency();
self.as_mut().record_latency(Instant::now());
}

if let Some(c) = self.as_mut().project().classify.take().map(|c| c.eos(None)) {
Expand Down

0 comments on commit 7fe13ec

Please sign in to comment.