Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move object store read/write timer into inner #3627

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
200 changes: 118 additions & 82 deletions src/object-store/src/layers/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
use std::fmt::{Debug, Formatter};
use std::io;
use std::task::{Context, Poll};
use std::time::Instant;

use async_trait::async_trait;
use bytes::Bytes;
use common_telemetry::debug;
use futures::{FutureExt, TryFutureExt};
use lazy_static::lazy_static;
use opendal::raw::*;
use opendal::ErrorKind;
use prometheus::{
exponential_buckets, histogram_opts, register_histogram_vec, register_int_counter_vec,
HistogramVec, IntCounterVec,
Histogram, HistogramVec, IntCounterVec,
};

type Result<T> = std::result::Result<T, opendal::Error>;
Expand Down Expand Up @@ -156,24 +156,27 @@ impl<A: Accessor> LayeredAccessor for PrometheusAccessor<A> {
let timer = REQUESTS_DURATION_SECONDS
.with_label_values(&[&self.scheme, Operation::Read.into_static()])
.start_timer();
let read_res = self.inner.read(path, args).await;
timer.observe_duration();
dimbtp marked this conversation as resolved.
Show resolved Hide resolved

let read_res = self
.inner
.read(path, args)
.map(|v| {
v.map(|(rp, r)| {
(
rp,
PrometheusMetricWrapper::new(r, Operation::Read, &self.scheme),
)
})
read_res
.map(|(rp, r)| {
(
rp,
PrometheusMetricWrapper::new(
r,
Operation::Read,
BYTES_TOTAL
.with_label_values(&[&self.scheme, Operation::Read.into_static()]),
REQUESTS_DURATION_SECONDS
.with_label_values(&[&self.scheme, Operation::Read.into_static()]),
),
dimbtp marked this conversation as resolved.
Show resolved Hide resolved
)
})
.map_err(|e| {
increment_errors_total(Operation::Read, e.kind());
e
})
.await;
timer.observe_duration();
read_res.map_err(|e| {
increment_errors_total(Operation::Read, e.kind());
e
})
}

async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
Expand All @@ -184,24 +187,27 @@ impl<A: Accessor> LayeredAccessor for PrometheusAccessor<A> {
let timer = REQUESTS_DURATION_SECONDS
.with_label_values(&[&self.scheme, Operation::Write.into_static()])
.start_timer();
let write_res = self.inner.write(path, args).await;
timer.observe_duration();

let write_res = self
.inner
.write(path, args)
.map(|v| {
v.map(|(rp, r)| {
(
rp,
PrometheusMetricWrapper::new(r, Operation::Write, &self.scheme),
)
})
write_res
.map(|(rp, r)| {
(
rp,
PrometheusMetricWrapper::new(
r,
Operation::Write,
BYTES_TOTAL
.with_label_values(&[&self.scheme, Operation::Write.into_static()]),
REQUESTS_DURATION_SECONDS
.with_label_values(&[&self.scheme, Operation::Write.into_static()]),
),
)
})
.map_err(|e| {
increment_errors_total(Operation::Write, e.kind());
e
})
.await;
timer.observe_duration();
write_res.map_err(|e| {
increment_errors_total(Operation::Write, e.kind());
e
})
}

async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
Expand All @@ -212,13 +218,7 @@ impl<A: Accessor> LayeredAccessor for PrometheusAccessor<A> {
.with_label_values(&[&self.scheme, Operation::Stat.into_static()])
.start_timer();

let stat_res = self
.inner
.stat(path, args)
.inspect_err(|e| {
increment_errors_total(Operation::Stat, e.kind());
})
.await;
let stat_res = self.inner.stat(path, args).await;
timer.observe_duration();
stat_res.map_err(|e| {
increment_errors_total(Operation::Stat, e.kind());
Expand Down Expand Up @@ -321,17 +321,31 @@ impl<A: Accessor> LayeredAccessor for PrometheusAccessor<A> {
let timer = REQUESTS_DURATION_SECONDS
.with_label_values(&[&self.scheme, Operation::BlockingRead.into_static()])
.start_timer();
let result = self.inner.blocking_read(path, args).map(|(rp, r)| {
(
rp,
PrometheusMetricWrapper::new(r, Operation::BlockingRead, &self.scheme),
)
});
let result = self.inner.blocking_read(path, args);
timer.observe_duration();
result.map_err(|e| {
increment_errors_total(Operation::BlockingRead, e.kind());
e
})

result
.map(|(rp, r)| {
(
rp,
PrometheusMetricWrapper::new(
r,
Operation::BlockingRead,
BYTES_TOTAL.with_label_values(&[
&self.scheme,
Operation::BlockingRead.into_static(),
]),
REQUESTS_DURATION_SECONDS.with_label_values(&[
&self.scheme,
Operation::BlockingRead.into_static(),
]),
),
)
})
.map_err(|e| {
increment_errors_total(Operation::BlockingRead, e.kind());
e
})
}

fn blocking_write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::BlockingWriter)> {
Expand All @@ -342,17 +356,31 @@ impl<A: Accessor> LayeredAccessor for PrometheusAccessor<A> {
let timer = REQUESTS_DURATION_SECONDS
.with_label_values(&[&self.scheme, Operation::BlockingWrite.into_static()])
.start_timer();
let result = self.inner.blocking_write(path, args).map(|(rp, r)| {
(
rp,
PrometheusMetricWrapper::new(r, Operation::BlockingWrite, &self.scheme),
)
});
let result = self.inner.blocking_write(path, args);
timer.observe_duration();
result.map_err(|e| {
increment_errors_total(Operation::BlockingWrite, e.kind());
e
})

result
.map(|(rp, r)| {
(
rp,
PrometheusMetricWrapper::new(
r,
Operation::BlockingWrite,
BYTES_TOTAL.with_label_values(&[
&self.scheme,
Operation::BlockingWrite.into_static(),
]),
REQUESTS_DURATION_SECONDS.with_label_values(&[
&self.scheme,
Operation::BlockingWrite.into_static(),
]),
),
)
})
.map_err(|e| {
increment_errors_total(Operation::BlockingWrite, e.kind());
e
})
}

fn blocking_stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
Expand Down Expand Up @@ -410,15 +438,35 @@ pub struct PrometheusMetricWrapper<R> {
inner: R,

op: Operation,
scheme: String,
bytes_counter: Histogram,
requests_duration_seconds: Histogram,

start: Instant,
dimbtp marked this conversation as resolved.
Show resolved Hide resolved
bytes: u64,
}

impl<R> Drop for PrometheusMetricWrapper<R> {
fn drop(&mut self) {
self.bytes_counter.observe(self.bytes as f64);
let dur = self.start.elapsed().as_secs_f64();
self.requests_duration_seconds.observe(dur);
dimbtp marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl<R> PrometheusMetricWrapper<R> {
fn new(inner: R, op: Operation, scheme: &String) -> Self {
fn new(
inner: R,
op: Operation,
bytes_counter: Histogram,
requests_duration_seconds: Histogram,
) -> Self {
dimbtp marked this conversation as resolved.
Show resolved Hide resolved
Self {
inner,
op,
scheme: scheme.to_string(),
bytes_counter,
requests_duration_seconds,
start: Instant::now(),
dimbtp marked this conversation as resolved.
Show resolved Hide resolved
bytes: 0,
}
}
}
Expand All @@ -427,9 +475,7 @@ impl<R: oio::Read> oio::Read for PrometheusMetricWrapper<R> {
fn poll_read(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize>> {
self.inner.poll_read(cx, buf).map(|res| match res {
Ok(bytes) => {
BYTES_TOTAL
.with_label_values(&[&self.scheme, Operation::Read.into_static()])
.observe(bytes as f64);
self.bytes += bytes as u64;
Ok(bytes)
}
Err(e) => {
Expand All @@ -452,9 +498,7 @@ impl<R: oio::Read> oio::Read for PrometheusMetricWrapper<R> {
fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes>>> {
self.inner.poll_next(cx).map(|res| match res {
Some(Ok(bytes)) => {
BYTES_TOTAL
.with_label_values(&[&self.scheme, Operation::Read.into_static()])
.observe(bytes.len() as f64);
self.bytes += bytes.len() as u64;
Some(Ok(bytes))
}
Some(Err(e)) => {
Expand All @@ -471,9 +515,7 @@ impl<R: oio::BlockingRead> oio::BlockingRead for PrometheusMetricWrapper<R> {
self.inner
.read(buf)
.map(|n| {
BYTES_TOTAL
.with_label_values(&[&self.scheme, Operation::BlockingRead.into_static()])
.observe(n as f64);
self.bytes += n as u64;
n
})
.map_err(|e| {
Expand All @@ -492,9 +534,7 @@ impl<R: oio::BlockingRead> oio::BlockingRead for PrometheusMetricWrapper<R> {
fn next(&mut self) -> Option<Result<Bytes>> {
self.inner.next().map(|res| match res {
Ok(bytes) => {
BYTES_TOTAL
.with_label_values(&[&self.scheme, Operation::BlockingRead.into_static()])
.observe(bytes.len() as f64);
self.bytes += bytes.len() as u64;
Ok(bytes)
}
Err(e) => {
Expand All @@ -511,9 +551,7 @@ impl<R: oio::Write> oio::Write for PrometheusMetricWrapper<R> {
self.inner
.poll_write(cx, bs)
.map_ok(|n| {
BYTES_TOTAL
.with_label_values(&[&self.scheme, Operation::Write.into_static()])
.observe(n as f64);
self.bytes += n as u64;
n
})
.map_err(|err| {
Expand Down Expand Up @@ -542,9 +580,7 @@ impl<R: oio::BlockingWrite> oio::BlockingWrite for PrometheusMetricWrapper<R> {
self.inner
.write(bs)
.map(|n| {
BYTES_TOTAL
.with_label_values(&[&self.scheme, Operation::BlockingWrite.into_static()])
.observe(n as f64);
self.bytes += n as u64;
n
})
.map_err(|err| {
Expand Down