Skip to content

Commit 9317826

Browse files
examples/metrics-advanced: add explanation
1 parent 5eb0601 commit 9317826

File tree

1 file changed

+19
-0
lines changed
  • examples/metrics-advanced/src

1 file changed

+19
-0
lines changed

examples/metrics-advanced/src/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
3434
};
3535

3636
// for example 3
37+
// Unlike a regular OpenTelemetry histogram with fixed buckets, which can be
38+
// specified explicitly, an exponential histogram calculates bucket widths
39+
// automatically, growing them exponentially. The configuration is
40+
// controlled by two parameters: max_size defines the maximum number of
41+
// buckets, while max_scale adjusts the resolution, with higher values
42+
// providing greater precision.
43+
// If the minimum and maximum values are known in advance, a regular
44+
// histogram is often the better choice. However, if the range of values is
45+
// unpredictable e.g. may include extreme outliers, an exponential histogram
46+
// is more suitable. A example is measuring packet round-trip time in a
47+
// WLAN: while most packets return in milliseconds, some may occasionally
48+
// take hundreds of milliseconds or even seconds.
49+
// Details are in:
50+
// https://opentelemetry.io/docs/specs/otel/metrics/data-model/#exponentialhistogram
3751
let my_view_change_aggregation = |i: &Instrument| {
3852
if i.name() == "my_third_histogram" {
3953
Stream::builder()
@@ -137,6 +151,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
137151
histogram3.record(-1.3, &[KeyValue::new("mykey1", "myvalue1")]);
138152
histogram3.record(-5.5, &[KeyValue::new("mykey1", "myvalue1")]);
139153
histogram3.record(7.5, &[KeyValue::new("mykey1", "myvalue1")]);
154+
// Internally the exponential histogram puts values either into a list of
155+
// negative buckets or a list of positive buckets. Based on the values which
156+
// are added the buckets are adjusted automatically. E.g. if the next record
157+
// is commented/uncommented, will result in a exponential histogram with a
158+
// different scale.
140159
histogram3.record(0.4, &[KeyValue::new("mykey1", "myvalue1")]);
141160

142161
// Metrics are exported by default every 60 seconds when using stdout exporter,

0 commit comments

Comments
 (0)