-
Notifications
You must be signed in to change notification settings - Fork 599
feat: Improve support for exponential histogram #3259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thomasstauffer666
wants to merge
5
commits into
open-telemetry:main
Choose a base branch
from
thomasstauffer666:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae7224c
examples/metrics-advanced: add example with exponential histogram
thomasstauffer666 ee28930
stdout: also can print extended histogram
thomasstauffer666 29045c2
aggregation validate: use constants for output
thomasstauffer666 9bb947d
Merge branch 'main' into main
cijothomas 8cd4200
Merge branch 'main' into main
cijothomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| use opentelemetry::global; | ||
| use opentelemetry::KeyValue; | ||
| use opentelemetry_sdk::metrics::{Instrument, SdkMeterProvider, Stream, Temporality}; | ||
| use opentelemetry_sdk::metrics::{Aggregation, Instrument, SdkMeterProvider, Stream, Temporality}; | ||
| use opentelemetry_sdk::Resource; | ||
| use std::error::Error; | ||
|
|
||
|
|
@@ -33,6 +33,36 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider { | |
| } | ||
| }; | ||
|
|
||
| // for example 3 | ||
| // Unlike a regular OpenTelemetry histogram with fixed buckets, which can be | ||
| // specified explicitly, an exponential histogram calculates bucket widths | ||
| // automatically, growing them exponentially. The configuration is | ||
| // controlled by two parameters: max_size defines the maximum number of | ||
| // buckets, while max_scale adjusts the resolution, with higher values | ||
| // providing greater precision. | ||
| // If the minimum and maximum values are known in advance, a regular | ||
| // histogram is often the better choice. However, if the range of values is | ||
| // unpredictable e.g. may include extreme outliers, an exponential histogram | ||
| // is more suitable. A example is measuring packet round-trip time in a | ||
| // WLAN: while most packets return in milliseconds, some may occasionally | ||
| // take hundreds of milliseconds or even seconds. | ||
| // Details are in: | ||
| // https://opentelemetry.io/docs/specs/otel/metrics/data-model/#exponentialhistogram | ||
| let my_view_change_aggregation = |i: &Instrument| { | ||
| if i.name() == "my_third_histogram" { | ||
| Stream::builder() | ||
| .with_aggregation(Aggregation::Base2ExponentialHistogram { | ||
| max_size: 10, | ||
| max_scale: 5, | ||
| record_min_max: true, | ||
| }) | ||
| .build() | ||
| .ok() | ||
| } else { | ||
| None | ||
| } | ||
| }; | ||
|
|
||
| // Build exporter using Delta Temporality. | ||
| let exporter = opentelemetry_stdout::MetricExporterBuilder::default() | ||
| .with_temporality(Temporality::Delta) | ||
|
|
@@ -47,6 +77,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider { | |
| .with_resource(resource) | ||
| .with_view(my_view_rename_and_unit) | ||
| .with_view(my_view_change_cardinality) | ||
| .with_view(my_view_change_aggregation) | ||
| .build(); | ||
| global::set_meter_provider(provider.clone()); | ||
| provider | ||
|
|
@@ -112,6 +143,23 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | |
|
|
||
| histogram2.record(1.8, &[KeyValue::new("mykey1", "v7")]); | ||
|
|
||
| // Example 3 - Use exponential histogram. | ||
| let histogram3 = meter | ||
| .f64_histogram("my_third_histogram") | ||
| .with_description("My histogram example description") | ||
| .build(); | ||
| histogram3.record(-1.3, &[KeyValue::new("mykey1", "v1")]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -ve values are not expected to be provided, though we dont validate that! See #3260 for open PR/discussions. |
||
| histogram3.record(-5.5, &[KeyValue::new("mykey1", "v1")]); | ||
| // is intentionally at the boundary of bucket | ||
| histogram3.record(-4.0, &[KeyValue::new("mykey1", "v1")]); | ||
| histogram3.record(16.0, &[KeyValue::new("mykey1", "v1")]); | ||
| // Internally the exponential histogram puts values either into a list of | ||
| // negative buckets or a list of positive buckets. Based on the values which | ||
| // are added the buckets are adjusted automatically. E.g. depending if the | ||
| // next record is commented/uncommented, then exponential histogram will | ||
| // have a different scale. | ||
| histogram3.record(0.4, &[KeyValue::new("mykey1", "v1")]); | ||
|
|
||
| // Metrics are exported by default every 60 seconds when using stdout exporter, | ||
| // however shutting down the MeterProvider here instantly flushes | ||
| // the metrics, instead of waiting for the 60 sec interval. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| ## vNext | ||
|
|
||
| - ExponentialHistogram supported in stdout | ||
|
|
||
| ## 0.31.0 | ||
|
|
||
| Released 2025-Sep-25 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.