Skip to content

Commit e0c9087

Browse files
authored
Metric advanced examples - change histogram buckets (#1257)
1 parent 2b5f4b1 commit e0c9087

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

examples/metrics-advanced/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ opentelemetry = { path = "../../opentelemetry", features = ["metrics"] }
1010
opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["metrics", "rt-tokio"] }
1111
opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["metrics"]}
1212
tokio = { version = "1.0", features = ["full"] }
13+
serde_json = {version = "1.0"}

examples/metrics-advanced/src/main.rs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use opentelemetry::metrics::Unit;
22
use opentelemetry::Key;
33
use opentelemetry::{metrics::MeterProvider as _, KeyValue};
4-
use opentelemetry_sdk::metrics::{Instrument, MeterProvider, PeriodicReader, Stream};
4+
use opentelemetry_sdk::metrics::{Aggregation, Instrument, MeterProvider, PeriodicReader, Stream};
55
use opentelemetry_sdk::{runtime, Resource};
66
use std::error::Error;
77

@@ -28,7 +28,25 @@ fn init_meter_provider() -> MeterProvider {
2828
}
2929
};
3030

31-
let exporter = opentelemetry_stdout::MetricsExporter::default();
31+
// for example 3
32+
let my_view_change_aggregation = |i: &Instrument| {
33+
if i.name == "my_second_histogram" {
34+
Some(
35+
Stream::new().aggregation(Aggregation::ExplicitBucketHistogram {
36+
boundaries: vec![0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5],
37+
record_min_max: false,
38+
}),
39+
)
40+
} else {
41+
None
42+
}
43+
};
44+
45+
let exporter = opentelemetry_stdout::MetricsExporterBuilder::default()
46+
// uncomment the below lines to pretty print output.
47+
// .with_encoder(|writer, data|
48+
// Ok(serde_json::to_writer_pretty(writer, &data).unwrap()))
49+
.build();
3250
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
3351
MeterProvider::builder()
3452
.with_reader(reader)
@@ -38,6 +56,7 @@ fn init_meter_provider() -> MeterProvider {
3856
)]))
3957
.with_view(my_view_rename_and_unit)
4058
.with_view(my_view_drop_attributes)
59+
.with_view(my_view_change_aggregation)
4160
.build()
4261
}
4362

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

108+
// Example 3 - Change Aggregation configuration using View.
109+
// Histograms are by default aggregated using ExplicitBucketHistogram
110+
// with default buckets. The configured view will change the aggregation to
111+
// use a custom set of boundaries, and min/max values will not be recorded.
112+
let histogram2 = meter
113+
.f64_histogram("my_second_histogram")
114+
.with_unit(Unit::new("ms"))
115+
.with_description("My histogram example description")
116+
.init();
117+
118+
// Record measurements using the histogram instrument.
119+
// The values recorded are in the range of 1.2 to 1.5, warranting
120+
// the change of boundaries.
121+
histogram2.record(
122+
1.5,
123+
[
124+
KeyValue::new("mykey1", "myvalue1"),
125+
KeyValue::new("mykey2", "myvalue2"),
126+
KeyValue::new("mykey3", "myvalue3"),
127+
KeyValue::new("mykey4", "myvalue4"),
128+
]
129+
.as_ref(),
130+
);
131+
132+
histogram2.record(
133+
1.2,
134+
[
135+
KeyValue::new("mykey1", "myvalue1"),
136+
KeyValue::new("mykey2", "myvalue2"),
137+
KeyValue::new("mykey3", "myvalue3"),
138+
KeyValue::new("mykey4", "myvalue4"),
139+
]
140+
.as_ref(),
141+
);
142+
143+
histogram2.record(
144+
1.23,
145+
[
146+
KeyValue::new("mykey1", "myvalue1"),
147+
KeyValue::new("mykey2", "myvalue2"),
148+
KeyValue::new("mykey3", "myvalue3"),
149+
KeyValue::new("mykey4", "myvalue4"),
150+
]
151+
.as_ref(),
152+
);
153+
89154
// Metrics are exported by default every 30 seconds when using stdout exporter,
90155
// however shutting down the MeterProvider here instantly flushes
91156
// the metrics, instead of waiting for the 30 sec interval.

opentelemetry-sdk/src/metrics/aggregation.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ pub enum Aggregation {
4040
/// bucket with a boundary that is greater than or equal to the measurement. As
4141
/// an example, boundaries defined as:
4242
///
43-
/// vec![0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0];
43+
/// vec![0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 750.0,
44+
/// 1000.0, 2500.0, 5000.0, 7500.0, 10000.0];
4445
///
4546
/// Will define these buckets:
4647
///
47-
/// (-∞, 0], (0, 5.0], (5.0, 10.0], (10.0, 25.0], (25.0, 50.0], (50.0, 75.0],
48-
/// (75.0, 100.0], (100.0, 250.0], (250.0, 500.0], (500.0, 1000.0], (1000.0, +∞)
48+
/// (-∞, 0], (0, 5.0], (5.0, 10.0], (10.0, 25.0], (25.0, 50.0], (50.0,
49+
/// 75.0], (75.0, 100.0], (100.0, 250.0], (250.0, 500.0], (500.0,
50+
/// 750.0], (750.0, 1000.0], (1000.0, 2500.0], (2500.0, 5000.0],
51+
/// (5000.0, 7500.0], (7500.0, 10000.0], (10000.0, +∞)
4952
boundaries: Vec<f64>,
5053

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

0 commit comments

Comments
 (0)