@@ -6,8 +6,8 @@ use opentelemetry_sdk::{
66 error:: OTelSdkResult ,
77 metrics:: {
88 data:: {
9- Gauge , GaugeDataPoint , Histogram , HistogramDataPoint , ResourceMetrics , ScopeMetrics ,
10- Sum , SumDataPoint ,
9+ ExponentialHistogram , ExponentialHistogramDataPoint , Gauge , GaugeDataPoint , Histogram ,
10+ HistogramDataPoint , ResourceMetrics , ScopeMetrics , Sum , SumDataPoint ,
1111 } ,
1212 exporter:: PushMetricExporter ,
1313 } ,
@@ -120,9 +120,9 @@ fn print_metrics<'a>(metrics: impl Iterator<Item = &'a ScopeMetrics>) {
120120 println ! ( "\t \t Type : Histogram" ) ;
121121 print_histogram ( hist) ;
122122 }
123- MetricData :: ExponentialHistogram ( _ ) => {
123+ MetricData :: ExponentialHistogram ( hist ) => {
124124 println ! ( "\t \t Type : Exponential Histogram" ) ;
125- // TODO: add support for ExponentialHistogram
125+ print_exponential_histogram ( hist ) ;
126126 }
127127 }
128128 }
@@ -193,6 +193,26 @@ fn print_histogram<T: Debug + Copy>(histogram: &Histogram<T>) {
193193 print_hist_data_points ( histogram. data_points ( ) ) ;
194194}
195195
196+ fn print_exponential_histogram < T : Debug + Copy > ( histogram : & ExponentialHistogram < T > ) {
197+ if histogram. temporality ( ) == Temporality :: Cumulative {
198+ println ! ( "\t \t Temporality : Cumulative" ) ;
199+ } else {
200+ println ! ( "\t \t Temporality : Delta" ) ;
201+ }
202+ let datetime: DateTime < Utc > = histogram. start_time ( ) . into ( ) ;
203+ println ! (
204+ "\t \t StartTime : {}" ,
205+ datetime. format( "%Y-%m-%d %H:%M:%S%.6f" )
206+ ) ;
207+ let datetime: DateTime < Utc > = histogram. time ( ) . into ( ) ;
208+ println ! (
209+ "\t \t EndTime : {}" ,
210+ datetime. format( "%Y-%m-%d %H:%M:%S%.6f" )
211+ ) ;
212+ println ! ( "\t \t Exponential Histogram DataPoints" ) ;
213+ print_exponential_hist_data_points ( histogram. data_points ( ) ) ;
214+ }
215+
196216fn print_sum_data_points < ' a , T : Debug + Copy + ' a > (
197217 data_points : impl Iterator < Item = & ' a SumDataPoint < T > > ,
198218) {
@@ -266,6 +286,70 @@ fn print_hist_data_points<'a, T: Debug + Copy + 'a>(
266286 }
267287}
268288
289+ fn print_exponential_hist_data_points < ' a , T : Debug + Copy + ' a > (
290+ data_points : impl Iterator < Item = & ' a ExponentialHistogramDataPoint < T > > ,
291+ ) {
292+ for ( i, data_point) in data_points. enumerate ( ) {
293+ println ! ( "\t \t DataPoint #{i}" ) ;
294+ println ! ( "\t \t \t Count : {}" , data_point. count( ) ) ;
295+ println ! ( "\t \t \t Sum : {:?}" , data_point. sum( ) ) ;
296+ if let Some ( min) = & data_point. min ( ) {
297+ println ! ( "\t \t \t Min : {min:?}" ) ;
298+ }
299+
300+ if let Some ( max) = & data_point. max ( ) {
301+ println ! ( "\t \t \t Max : {max:?}" ) ;
302+ }
303+
304+ let scale = data_point. scale ( ) ;
305+ let base = 2.0f64 . powf ( 2.0f64 . powf ( -scale as f64 ) ) ;
306+
307+ println ! ( "\t \t \t Scale : {:?}" , scale) ;
308+ println ! ( "\t \t \t Base : {:?}" , base) ;
309+ println ! ( "\t \t \t ZeroCount : {}" , data_point. zero_count( ) ) ;
310+ println ! ( "\t \t \t ZeroThreshold : {}" , data_point. zero_threshold( ) ) ;
311+
312+ println ! ( "\t \t \t Attributes :" ) ;
313+ for kv in data_point. attributes ( ) {
314+ println ! ( "\t \t \t \t -> {} : {}" , kv. key, kv. value. as_str( ) ) ;
315+ }
316+
317+ // Bucket upper-bounds are inclusive while bucket lower-bounds are
318+ // exclusive. Details if a bound is including/excluding can be found in:
319+ // https://opentelemetry.io/docs/specs/otel/metrics/data-model/#histogram-bucket-inclusivity
320+
321+ let negative_bucket = data_point. negative_bucket ( ) ;
322+ let negative_offset = negative_bucket. offset ( ) ;
323+ println ! ( "\t \t \t NegativeOffset : {}" , negative_offset) ;
324+ for ( i, count) in negative_bucket
325+ . counts ( )
326+ . collect :: < Vec < _ > > ( )
327+ . into_iter ( )
328+ . enumerate ( )
329+ . rev ( )
330+ {
331+ let lower = -base. powf ( i as f64 + negative_offset as f64 + 1.0f64 ) ;
332+ let upper = -base. powf ( i as f64 + negative_offset as f64 ) ;
333+ println ! (
334+ "\t \t \t \t Bucket {} ({:?}, {:?}] : {}" ,
335+ i, lower, upper, count
336+ ) ;
337+ }
338+
339+ let positive_bucket = data_point. positive_bucket ( ) ;
340+ let positive_offset = positive_bucket. offset ( ) ;
341+ println ! ( "\t \t \t PositiveOffset : {}" , positive_offset) ;
342+ for ( i, count) in positive_bucket. counts ( ) . enumerate ( ) {
343+ let lower = base. powf ( i as f64 + positive_offset as f64 ) ;
344+ let upper = base. powf ( i as f64 + positive_offset as f64 + 1.0f64 ) ;
345+ println ! (
346+ "\t \t \t \t Bucket {} ({:?}, {:?}] : {}" ,
347+ i, lower, upper, count
348+ ) ;
349+ }
350+ }
351+ }
352+
269353/// Configuration for the stdout metrics exporter
270354#[ derive( Default ) ]
271355pub struct MetricExporterBuilder {
0 commit comments