Skip to content

Commit 567bcf0

Browse files
committed
Add some gauge coverage
1 parent ed4d9a5 commit 567bcf0

File tree

1 file changed

+66
-0
lines changed
  • opentelemetry-sdk/src/metrics

1 file changed

+66
-0
lines changed

opentelemetry-sdk/src/metrics/mod.rs

+66
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ mod tests {
228228
updown_counter_aggregation_helper(Temporality::Delta);
229229
}
230230

231+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
232+
async fn gauge_aggregation() {
233+
// Run this test with stdout enabled to see output.
234+
// cargo test gauge_aggregation --features=metrics,testing -- --nocapture
235+
gauge_aggregation_helper();
236+
}
237+
231238
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
232239
async fn observable_counter_aggregation_cumulative_non_zero_increment() {
233240
// Run this test with stdout enabled to see output.
@@ -1127,6 +1134,65 @@ mod tests {
11271134
}
11281135
}
11291136

1137+
fn gauge_aggregation_helper() {
1138+
// Arrange
1139+
let mut test_context = TestContext::new(Temporality::Delta);
1140+
let gauge = test_context.meter().i64_gauge("my_gauge").init();
1141+
1142+
// Act
1143+
gauge.record(1, &[KeyValue::new("key1", "value1")]);
1144+
gauge.record(2, &[KeyValue::new("key1", "value1")]);
1145+
gauge.record(1, &[KeyValue::new("key1", "value1")]);
1146+
gauge.record(3, &[KeyValue::new("key1", "value1")]);
1147+
gauge.record(4, &[KeyValue::new("key1", "value1")]);
1148+
1149+
gauge.record(11, &[KeyValue::new("key1", "value2")]);
1150+
gauge.record(13, &[KeyValue::new("key1", "value2")]);
1151+
gauge.record(6, &[KeyValue::new("key1", "value2")]);
1152+
1153+
test_context.flush_metrics();
1154+
1155+
// Assert
1156+
let gauge_data_point = test_context.get_aggregation::<data::Gauge<i64>>("my_gauge", None);
1157+
// Expecting 2 time-series.
1158+
assert_eq!(gauge_data_point.data_points.len(), 2);
1159+
1160+
// find and validate key1=value2 datapoint
1161+
let data_point1 =
1162+
find_datapoint_with_key_value(&gauge_data_point.data_points, "key1", "value1")
1163+
.expect("datapoint with key1=value1 expected");
1164+
assert_eq!(data_point1.value, 4);
1165+
1166+
let data_point1 =
1167+
find_datapoint_with_key_value(&gauge_data_point.data_points, "key1", "value2")
1168+
.expect("datapoint with key1=value2 expected");
1169+
assert_eq!(data_point1.value, 6);
1170+
1171+
// Reset and report more measurements
1172+
test_context.reset_metrics();
1173+
gauge.record(1, &[KeyValue::new("key1", "value1")]);
1174+
gauge.record(2, &[KeyValue::new("key1", "value1")]);
1175+
gauge.record(11, &[KeyValue::new("key1", "value1")]);
1176+
gauge.record(3, &[KeyValue::new("key1", "value1")]);
1177+
gauge.record(41, &[KeyValue::new("key1", "value1")]);
1178+
1179+
gauge.record(34, &[KeyValue::new("key1", "value2")]);
1180+
gauge.record(12, &[KeyValue::new("key1", "value2")]);
1181+
gauge.record(54, &[KeyValue::new("key1", "value2")]);
1182+
1183+
test_context.flush_metrics();
1184+
1185+
let sum = test_context.get_aggregation::<data::Gauge<i64>>("my_gauge", None);
1186+
assert_eq!(sum.data_points.len(), 2);
1187+
let data_point1 = find_datapoint_with_key_value(&sum.data_points, "key1", "value1")
1188+
.expect("datapoint with key1=value1 expected");
1189+
assert_eq!(data_point1.value, 41);
1190+
1191+
let data_point1 = find_datapoint_with_key_value(&sum.data_points, "key1", "value2")
1192+
.expect("datapoint with key1=value2 expected");
1193+
assert_eq!(data_point1.value, 54);
1194+
}
1195+
11301196
fn counter_aggregation_helper(temporality: Temporality) {
11311197
// Arrange
11321198
let mut test_context = TestContext::new(temporality);

0 commit comments

Comments
 (0)