Skip to content

Commit 70a29bb

Browse files
stdout also can print extended histogram
1 parent d400c03 commit 70a29bb

File tree

1 file changed

+78
-4
lines changed

1 file changed

+78
-4
lines changed

opentelemetry-stdout/src/metrics/exporter.rs

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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\tType : Histogram");
121121
print_histogram(hist);
122122
}
123-
MetricData::ExponentialHistogram(_) => {
123+
MetricData::ExponentialHistogram(hist) => {
124124
println!("\t\tType : 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\tTemporality : Cumulative");
199+
} else {
200+
println!("\t\tTemporality : Delta");
201+
}
202+
let datetime: DateTime<Utc> = histogram.start_time().into();
203+
println!(
204+
"\t\tStartTime : {}",
205+
datetime.format("%Y-%m-%d %H:%M:%S%.6f")
206+
);
207+
let datetime: DateTime<Utc> = histogram.time().into();
208+
println!(
209+
"\t\tEndTime : {}",
210+
datetime.format("%Y-%m-%d %H:%M:%S%.6f")
211+
);
212+
println!("\t\tExponential Histogram DataPoints");
213+
print_exponential_hist_data_points(histogram.data_points());
214+
}
215+
196216
fn print_sum_data_points<'a, T: Debug + Copy + 'a>(
197217
data_points: impl Iterator<Item = &'a SumDataPoint<T>>,
198218
) {
@@ -266,6 +286,60 @@ 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\tDataPoint #{i}");
294+
println!("\t\t\tCount : {}", data_point.count());
295+
println!("\t\t\tSum : {:?}", data_point.sum());
296+
if let Some(min) = &data_point.min() {
297+
println!("\t\t\tMin : {min:?}");
298+
}
299+
300+
if let Some(max) = &data_point.max() {
301+
println!("\t\t\tMax : {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\tScale : {}", scale);
308+
println!("\t\t\tBase : {:.3}", base);
309+
println!("\t\t\tZeroCount : {}", data_point.zero_count());
310+
println!("\t\t\tZeroThreshold : {}", data_point.zero_threshold());
311+
312+
println!("\t\t\tAttributes :");
313+
for kv in data_point.attributes() {
314+
println!("\t\t\t\t -> {} : {}", kv.key, kv.value.as_str());
315+
}
316+
317+
let negative_bucket = data_point.negative_bucket();
318+
let negative_offset = negative_bucket.offset();
319+
println!("\t\t\tNegativeOffset : {}", negative_offset);
320+
for (i, count) in negative_bucket.counts().enumerate() {
321+
let from = -base.powf(i as f64 + negative_offset as f64);
322+
let to = -base.powf(i as f64 + negative_offset as f64 + 1.0f64);
323+
println!(
324+
"\t\t\t\t -> Bucket {} (> {:.3} to <= {:.3}) : {}",
325+
i, from, to, count
326+
);
327+
}
328+
329+
let positive_bucket = data_point.positive_bucket();
330+
let positive_offset = positive_bucket.offset();
331+
println!("\t\t\tPositiveOffset : {}", positive_offset);
332+
for (i, count) in positive_bucket.counts().enumerate() {
333+
let from = base.powf(i as f64 + positive_offset as f64);
334+
let to = base.powf(i as f64 + positive_offset as f64 + 1.0f64);
335+
println!(
336+
"\t\t\t\t -> Bucket {} (> {:.3} to <= {:.3}) : {}",
337+
i, from, to, count
338+
);
339+
}
340+
}
341+
}
342+
269343
/// Configuration for the stdout metrics exporter
270344
#[derive(Default)]
271345
pub struct MetricExporterBuilder {

0 commit comments

Comments
 (0)