Add metrics to object storage body streams

This commit is contained in:
asonix 2023-09-20 19:46:10 -05:00
parent 49d3037358
commit cde4a72203
2 changed files with 28 additions and 4 deletions

View file

@ -405,9 +405,9 @@ impl Store for ObjectStore {
return Err(status_error(response).await); return Err(status_error(response).await);
} }
Ok(Box::pin(crate::stream::map_err( Ok(Box::pin(crate::stream::metrics(
response.bytes_stream(), "pict-rs.object-storage.get-object-request.stream",
payload_to_io_error, crate::stream::map_err(response.bytes_stream(), payload_to_io_error),
))) )))
} }
@ -434,7 +434,11 @@ impl Store for ObjectStore {
)); ));
} }
let mut stream = response.bytes_stream().into_streamer(); let stream = std::pin::pin!(crate::stream::metrics(
"pict-rs.object-storage.get-object-request.stream",
response.bytes_stream()
));
let mut stream = stream.into_streamer();
while let Some(res) = stream.next().await { while let Some(res) = stream.next().await {
let mut bytes = res.map_err(payload_to_io_error)?; let mut bytes = res.map_err(payload_to_io_error)?;

View file

@ -3,6 +3,26 @@ use futures_core::Stream;
use std::{pin::Pin, time::Duration}; use std::{pin::Pin, time::Duration};
use streem::IntoStreamer; use streem::IntoStreamer;
use crate::future::WithMetrics;
pub(crate) fn metrics<S>(name: &'static str, stream: S) -> impl Stream<Item = S::Item>
where
S: Stream,
S::Item: 'static,
{
streem::from_fn(|yielder| {
async move {
let stream = std::pin::pin!(stream);
let mut streamer = stream.into_streamer();
while let Some(item) = streamer.next().await {
yielder.yield_(item).await;
}
}
.with_metrics(name)
})
}
pub(crate) fn make_send<S>(stream: S) -> impl Stream<Item = S::Item> + Send pub(crate) fn make_send<S>(stream: S) -> impl Stream<Item = S::Item> + Send
where where
S: Stream + 'static, S: Stream + 'static,