util/metrics.rs: Ported to opentelemetry 0.18.
This commit is contained in:
parent
904d563ec3
commit
5ec3bde885
1 changed files with 8 additions and 5 deletions
|
@ -3,17 +3,17 @@ use std::time::Instant;
|
|||
use futures::{future::BoxFuture, Future, FutureExt};
|
||||
use rand::Rng;
|
||||
|
||||
use opentelemetry::{metrics::*, trace::TraceId, KeyValue};
|
||||
use opentelemetry::{metrics::*, trace::TraceId, Context, KeyValue};
|
||||
|
||||
pub trait RecordDuration<'a>: 'a {
|
||||
type Output;
|
||||
|
||||
fn record_duration(
|
||||
self,
|
||||
r: &'a ValueRecorder<f64>,
|
||||
r: &'a Histogram<f64>,
|
||||
attributes: &'a [KeyValue],
|
||||
) -> BoxFuture<'a, Self::Output>;
|
||||
fn bound_record_duration(self, r: &'a BoundValueRecorder<f64>) -> BoxFuture<'a, Self::Output>;
|
||||
fn bound_record_duration(self, r: &'a Histogram<f64>) -> BoxFuture<'a, Self::Output>;
|
||||
}
|
||||
|
||||
impl<'a, T, O> RecordDuration<'a> for T
|
||||
|
@ -24,13 +24,14 @@ where
|
|||
|
||||
fn record_duration(
|
||||
self,
|
||||
r: &'a ValueRecorder<f64>,
|
||||
r: &'a Histogram<f64>,
|
||||
attributes: &'a [KeyValue],
|
||||
) -> BoxFuture<'a, Self::Output> {
|
||||
async move {
|
||||
let request_start = Instant::now();
|
||||
let res = self.await;
|
||||
r.record(
|
||||
&Context::current(),
|
||||
Instant::now()
|
||||
.saturating_duration_since(request_start)
|
||||
.as_secs_f64(),
|
||||
|
@ -41,14 +42,16 @@ where
|
|||
.boxed()
|
||||
}
|
||||
|
||||
fn bound_record_duration(self, r: &'a BoundValueRecorder<f64>) -> BoxFuture<'a, Self::Output> {
|
||||
fn bound_record_duration(self, r: &'a Histogram<f64>) -> BoxFuture<'a, Self::Output> {
|
||||
async move {
|
||||
let request_start = Instant::now();
|
||||
let res = self.await;
|
||||
r.record(
|
||||
&Context::current(),
|
||||
Instant::now()
|
||||
.saturating_duration_since(request_start)
|
||||
.as_secs_f64(),
|
||||
&[],
|
||||
);
|
||||
res
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue