11use std:: { fmt, time:: Duration } ;
22
3+ use tokio:: runtime:: Handle ;
4+
35use super :: { RuntimeIntervals , RuntimeMetrics , RuntimeMonitor } ;
46
57/// A reporter builder
68pub struct RuntimeMetricsReporterBuilder {
79 interval : Duration ,
8- metrics_transformer : Box < dyn FnMut ( & ' static str ) -> metrics:: Key > ,
10+ metrics_transformer : Box < dyn FnMut ( & ' static str ) -> metrics:: Key + Send > ,
911}
1012
1113impl fmt:: Debug for RuntimeMetricsReporterBuilder {
@@ -35,7 +37,12 @@ impl RuntimeMetricsReporterBuilder {
3537 }
3638
3739 /// Build the reporter
38- pub fn build ( mut self , monitor : RuntimeMonitor ) -> RuntimeMetricsReporter {
40+ pub fn build ( self ) -> RuntimeMetricsReporter {
41+ self . build_with_monitor ( RuntimeMonitor :: new ( & Handle :: current ( ) ) )
42+ }
43+
44+ /// Build the reporter with a specific [`RuntimeMonitor`]
45+ pub fn build_with_monitor ( mut self , monitor : RuntimeMonitor ) -> RuntimeMetricsReporter {
3946 RuntimeMetricsReporter {
4047 interval : self . interval ,
4148 intervals : monitor. intervals ( ) ,
@@ -50,13 +57,13 @@ impl RuntimeMetricsReporterBuilder {
5057 }
5158
5259 /// Run the reporter, describing the metrics beforehand
53- pub async fn describe_and_run ( self , monitor : RuntimeMonitor ) {
54- self . describe ( ) . build ( monitor ) . run ( ) . await ;
60+ pub async fn describe_and_run ( self ) {
61+ self . describe ( ) . build ( ) . run ( ) . await ;
5562 }
5663
5764 /// Run the reporter, not describing the metrics beforehand
58- pub async fn run_without_describing ( self , monitor : RuntimeMonitor ) {
59- self . build ( monitor ) . run ( ) . await ;
65+ pub async fn run_without_describing ( self ) {
66+ self . build ( ) . run ( ) . await ;
6067 }
6168}
6269
@@ -77,15 +84,16 @@ macro_rules! metric_key {
7784 ( $transform_fn: ident, $name: ident) => ( $transform_fn( concat!( "tokio_" , stringify!( $name) ) ) )
7885}
7986
87+ // calling `trim` since /// inserts spaces into docs
8088macro_rules! describe_metric_ref {
8189 ( $transform_fn: ident, $doc: expr, $name: ident: Counter <$unit: ident> [ ] ) => (
82- metrics:: describe_counter!( metric_key!( $transform_fn, $name) . name( ) . to_owned( ) , metrics:: Unit :: $unit, $doc)
90+ metrics:: describe_counter!( metric_key!( $transform_fn, $name) . name( ) . to_owned( ) , metrics:: Unit :: $unit, $doc. trim ( ) )
8391 ) ;
8492 ( $transform_fn: ident, $doc: expr, $name: ident: Gauge <$unit: ident> [ ] ) => (
85- metrics:: describe_gauge!( metric_key!( $transform_fn, $name) . name( ) . to_owned( ) , metrics:: Unit :: $unit, $doc)
93+ metrics:: describe_gauge!( metric_key!( $transform_fn, $name) . name( ) . to_owned( ) , metrics:: Unit :: $unit, $doc. trim ( ) )
8694 ) ;
8795 ( $transform_fn: ident, $doc: expr, $name: ident: Histogram <$unit: ident> [ ] ) => (
88- metrics:: describe_histogram!( metric_key!( $transform_fn, $name) . name( ) . to_owned( ) , metrics:: Unit :: $unit, $doc)
96+ metrics:: describe_histogram!( metric_key!( $transform_fn, $name) . name( ) . to_owned( ) , metrics:: Unit :: $unit, $doc. trim ( ) )
8997 ) ;
9098}
9199
@@ -260,7 +268,11 @@ impl MyMetricOp for (&metrics::Histogram, Vec<u64>) {
260268 fn op ( self , tokio : & tokio:: runtime:: RuntimeMetrics ) {
261269 for ( i, bucket) in self . 1 . iter ( ) . enumerate ( ) {
262270 let range = tokio. poll_time_histogram_bucket_range ( i) ;
263- self . 0 . record_many ( ( ( range. start + range. end ) . as_micros ( ) / 2 ) as f64 , * bucket as usize ) ;
271+ if * bucket > 0 {
272+ // emit using range.start to avoid very large numbers for open bucket
273+ // FIXME: do we want to do something else here
274+ self . 0 . record_many ( range. start . as_micros ( ) as f64 , * bucket as usize ) ;
275+ }
264276 }
265277 }
266278}
0 commit comments