Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #778 from nappa85/master
Stream metrics
- Loading branch information
Showing
5 changed files
with
113 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::{time::Duration, pin::Pin, task::Poll}; | ||
|
||
use futures::Stream; | ||
|
||
use crate::{QueryResult, DbErr, Statement}; | ||
|
||
pub(crate) struct MetricStream<'a> { | ||
metric_callback: &'a Option<crate::metric::Callback>, | ||
stmt: &'a Statement, | ||
elapsed: Option<Duration>, | ||
stream: Pin<Box<dyn Stream<Item = Result<QueryResult, DbErr>> + 'a + Send>>, | ||
} | ||
|
||
impl<'a> MetricStream<'a> { | ||
pub(crate) fn new<S>(metric_callback: &'a Option<crate::metric::Callback>, stmt: &'a Statement, elapsed: Option<Duration>, stream: S) -> Self | ||
where | ||
S: Stream<Item = Result<QueryResult, DbErr>> + 'a + Send, | ||
{ | ||
MetricStream { | ||
metric_callback, | ||
stmt, | ||
elapsed, | ||
stream: Box::pin(stream), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Stream for MetricStream<'a> { | ||
type Item = Result<QueryResult, DbErr>; | ||
|
||
fn poll_next( | ||
self: Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> Poll<Option<Self::Item>> { | ||
let this = self.get_mut(); | ||
let _start = this.metric_callback.is_some().then(std::time::SystemTime::now); | ||
let res = Pin::new(&mut this.stream).poll_next(cx); | ||
if let (Some(_start), Some(elapsed)) = (_start, &mut this.elapsed) { | ||
*elapsed += _start.elapsed().unwrap_or_default(); | ||
} | ||
res | ||
} | ||
} | ||
|
||
impl<'a> Drop for MetricStream<'a> { | ||
fn drop(&mut self) { | ||
if let (Some(callback), Some(elapsed)) = (self.metric_callback.as_deref(), self.elapsed) { | ||
let info = crate::metric::Info { | ||
elapsed: elapsed, | ||
statement: self.stmt, | ||
failed: false, | ||
}; | ||
callback(&info); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod metric; | ||
|
||
mod query; | ||
mod transaction; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters