Skip to content

Metric advanced examples - change histogram buckets #1257

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/metrics-advanced/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ opentelemetry = { path = "../../opentelemetry", features = ["metrics"] }
opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["metrics", "rt-tokio"] }
opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["metrics"]}
tokio = { version = "1.0", features = ["full"] }
serde_json = {version = "1.0"}
69 changes: 67 additions & 2 deletions examples/metrics-advanced/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use opentelemetry::metrics::Unit;
use opentelemetry::Key;
use opentelemetry::{metrics::MeterProvider as _, KeyValue};
use opentelemetry_sdk::metrics::{Instrument, MeterProvider, PeriodicReader, Stream};
use opentelemetry_sdk::metrics::{Aggregation, Instrument, MeterProvider, PeriodicReader, Stream};
use opentelemetry_sdk::{runtime, Resource};
use std::error::Error;

Expand All @@ -28,7 +28,25 @@ fn init_meter_provider() -> MeterProvider {
}
};

let exporter = opentelemetry_stdout::MetricsExporter::default();
// for example 3
let my_view_change_aggregation = |i: &Instrument| {
if i.name == "my_second_histogram" {
Some(
Stream::new().aggregation(Aggregation::ExplicitBucketHistogram {
boundaries: vec![0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5],
record_min_max: false,
}),
)
} else {
None
}
};

let exporter = opentelemetry_stdout::MetricsExporterBuilder::default()
// uncomment the below lines to pretty print output.
// .with_encoder(|writer, data|
// Ok(serde_json::to_writer_pretty(writer, &data).unwrap()))
.build();
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
MeterProvider::builder()
.with_reader(reader)
Expand All @@ -38,6 +56,7 @@ fn init_meter_provider() -> MeterProvider {
)]))
.with_view(my_view_rename_and_unit)
.with_view(my_view_drop_attributes)
.with_view(my_view_change_aggregation)
.build()
}

Expand Down Expand Up @@ -86,6 +105,52 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
.as_ref(),
);

// Example 3 - Change Aggregation configuration using View.
// Histograms are by default aggregated using ExplicitBucketHistogram
// with default buckets. The configured view will change the aggregation to
// use a custom set of boundaries, and min/max values will not be recorded.
let histogram2 = meter
.f64_histogram("my_second_histogram")
.with_unit(Unit::new("ms"))
.with_description("My histogram example description")
.init();

// Record measurements using the histogram instrument.
// The values recorded are in the range of 1.2 to 1.5, warranting
// the change of boundaries.
histogram2.record(
1.5,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
KeyValue::new("mykey3", "myvalue3"),
KeyValue::new("mykey4", "myvalue4"),
]
.as_ref(),
);

histogram2.record(
1.2,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
KeyValue::new("mykey3", "myvalue3"),
KeyValue::new("mykey4", "myvalue4"),
]
.as_ref(),
);

histogram2.record(
1.23,
[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
KeyValue::new("mykey3", "myvalue3"),
KeyValue::new("mykey4", "myvalue4"),
]
.as_ref(),
);

// Metrics are exported by default every 30 seconds when using stdout exporter,
// however shutting down the MeterProvider here instantly flushes
// the metrics, instead of waiting for the 30 sec interval.
Expand Down
9 changes: 6 additions & 3 deletions opentelemetry-sdk/src/metrics/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ pub enum Aggregation {
/// bucket with a boundary that is greater than or equal to the measurement. As
/// an example, boundaries defined as:
///
/// vec![0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0];
/// vec![0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 750.0,
/// 1000.0, 2500.0, 5000.0, 7500.0, 10000.0];
///
/// Will define these buckets:
///
/// (-∞, 0], (0, 5.0], (5.0, 10.0], (10.0, 25.0], (25.0, 50.0], (50.0, 75.0],
/// (75.0, 100.0], (100.0, 250.0], (250.0, 500.0], (500.0, 1000.0], (1000.0, +∞)
/// (-∞, 0], (0, 5.0], (5.0, 10.0], (10.0, 25.0], (25.0, 50.0], (50.0,
/// 75.0], (75.0, 100.0], (100.0, 250.0], (250.0, 500.0], (500.0,
/// 750.0], (750.0, 1000.0], (1000.0, 2500.0], (2500.0, 5000.0],
/// (5000.0, 7500.0], (7500.0, 10000.0], (10000.0, +∞)
boundaries: Vec<f64>,

/// Indicates whether to not record the min and max of the distribution.
Expand Down