Skip to content

Commit 577a951

Browse files
authored
Merge branch 'main' into trace-set-resource
2 parents 1cde293 + 78943fd commit 577a951

File tree

1 file changed

+184
-8
lines changed
  • opentelemetry-sdk/src/metrics

1 file changed

+184
-8
lines changed

opentelemetry-sdk/src/metrics/mod.rs

+184-8
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.
@@ -1046,39 +1053,144 @@ mod tests {
10461053
test_context.flush_metrics();
10471054

10481055
// Assert
1049-
let histogram = test_context.get_aggregation::<data::Histogram<u64>>("my_histogram", None);
1056+
let histogram_data =
1057+
test_context.get_aggregation::<data::Histogram<u64>>("my_histogram", None);
10501058
// Expecting 2 time-series.
1051-
assert_eq!(histogram.data_points.len(), 2);
1059+
assert_eq!(histogram_data.data_points.len(), 2);
10521060
if let Temporality::Cumulative = temporality {
10531061
assert_eq!(
1054-
histogram.temporality,
1062+
histogram_data.temporality,
10551063
Temporality::Cumulative,
10561064
"Should produce cumulative"
10571065
);
10581066
} else {
10591067
assert_eq!(
1060-
histogram.temporality,
1068+
histogram_data.temporality,
10611069
Temporality::Delta,
10621070
"Should produce delta"
10631071
);
10641072
}
10651073

10661074
// find and validate key1=value2 datapoint
10671075
let data_point1 =
1068-
find_histogram_datapoint_with_key_value(&histogram.data_points, "key1", "value1")
1076+
find_histogram_datapoint_with_key_value(&histogram_data.data_points, "key1", "value1")
10691077
.expect("datapoint with key1=value1 expected");
10701078
assert_eq!(data_point1.count, values_kv1.len() as u64);
10711079
assert_eq!(data_point1.sum, values_kv1.iter().sum::<u64>());
10721080
assert_eq!(data_point1.min.unwrap(), *values_kv1.iter().min().unwrap());
10731081
assert_eq!(data_point1.max.unwrap(), *values_kv1.iter().max().unwrap());
10741082

10751083
let data_point2 =
1076-
find_histogram_datapoint_with_key_value(&histogram.data_points, "key1", "value2")
1084+
find_histogram_datapoint_with_key_value(&histogram_data.data_points, "key1", "value2")
10771085
.expect("datapoint with key1=value2 expected");
10781086
assert_eq!(data_point2.count, values_kv2.len() as u64);
10791087
assert_eq!(data_point2.sum, values_kv2.iter().sum::<u64>());
10801088
assert_eq!(data_point2.min.unwrap(), *values_kv2.iter().min().unwrap());
10811089
assert_eq!(data_point2.max.unwrap(), *values_kv2.iter().max().unwrap());
1090+
1091+
// Reset and report more measurements
1092+
test_context.reset_metrics();
1093+
for value in values_kv1.iter() {
1094+
histogram.record(*value, &[KeyValue::new("key1", "value1")]);
1095+
}
1096+
1097+
for value in values_kv2.iter() {
1098+
histogram.record(*value, &[KeyValue::new("key1", "value2")]);
1099+
}
1100+
1101+
test_context.flush_metrics();
1102+
1103+
let histogram_data =
1104+
test_context.get_aggregation::<data::Histogram<u64>>("my_histogram", None);
1105+
assert_eq!(histogram_data.data_points.len(), 2);
1106+
let data_point1 =
1107+
find_histogram_datapoint_with_key_value(&histogram_data.data_points, "key1", "value1")
1108+
.expect("datapoint with key1=value1 expected");
1109+
if temporality == Temporality::Cumulative {
1110+
assert_eq!(data_point1.count, 2 * (values_kv1.len() as u64));
1111+
assert_eq!(data_point1.sum, 2 * (values_kv1.iter().sum::<u64>()));
1112+
assert_eq!(data_point1.min.unwrap(), *values_kv1.iter().min().unwrap());
1113+
assert_eq!(data_point1.max.unwrap(), *values_kv1.iter().max().unwrap());
1114+
} else {
1115+
assert_eq!(data_point1.count, values_kv1.len() as u64);
1116+
assert_eq!(data_point1.sum, values_kv1.iter().sum::<u64>());
1117+
assert_eq!(data_point1.min.unwrap(), *values_kv1.iter().min().unwrap());
1118+
assert_eq!(data_point1.max.unwrap(), *values_kv1.iter().max().unwrap());
1119+
}
1120+
1121+
let data_point1 =
1122+
find_histogram_datapoint_with_key_value(&histogram_data.data_points, "key1", "value2")
1123+
.expect("datapoint with key1=value1 expected");
1124+
if temporality == Temporality::Cumulative {
1125+
assert_eq!(data_point1.count, 2 * (values_kv2.len() as u64));
1126+
assert_eq!(data_point1.sum, 2 * (values_kv2.iter().sum::<u64>()));
1127+
assert_eq!(data_point1.min.unwrap(), *values_kv2.iter().min().unwrap());
1128+
assert_eq!(data_point1.max.unwrap(), *values_kv2.iter().max().unwrap());
1129+
} else {
1130+
assert_eq!(data_point1.count, values_kv2.len() as u64);
1131+
assert_eq!(data_point1.sum, values_kv2.iter().sum::<u64>());
1132+
assert_eq!(data_point1.min.unwrap(), *values_kv2.iter().min().unwrap());
1133+
assert_eq!(data_point1.max.unwrap(), *values_kv2.iter().max().unwrap());
1134+
}
1135+
}
1136+
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);
10821194
}
10831195

10841196
fn counter_aggregation_helper(temporality: Temporality) {
@@ -1122,12 +1234,44 @@ mod tests {
11221234
let data_point1 = find_datapoint_with_key_value(&sum.data_points, "key1", "value2")
11231235
.expect("datapoint with key1=value2 expected");
11241236
assert_eq!(data_point1.value, 3);
1237+
1238+
// Reset and report more measurements
1239+
test_context.reset_metrics();
1240+
counter.add(1, &[KeyValue::new("key1", "value1")]);
1241+
counter.add(1, &[KeyValue::new("key1", "value1")]);
1242+
counter.add(1, &[KeyValue::new("key1", "value1")]);
1243+
counter.add(1, &[KeyValue::new("key1", "value1")]);
1244+
counter.add(1, &[KeyValue::new("key1", "value1")]);
1245+
1246+
counter.add(1, &[KeyValue::new("key1", "value2")]);
1247+
counter.add(1, &[KeyValue::new("key1", "value2")]);
1248+
counter.add(1, &[KeyValue::new("key1", "value2")]);
1249+
1250+
test_context.flush_metrics();
1251+
1252+
let sum = test_context.get_aggregation::<data::Sum<u64>>("my_counter", None);
1253+
assert_eq!(sum.data_points.len(), 2);
1254+
let data_point1 = find_datapoint_with_key_value(&sum.data_points, "key1", "value1")
1255+
.expect("datapoint with key1=value1 expected");
1256+
if temporality == Temporality::Cumulative {
1257+
assert_eq!(data_point1.value, 10);
1258+
} else {
1259+
assert_eq!(data_point1.value, 5);
1260+
}
1261+
1262+
let data_point1 = find_datapoint_with_key_value(&sum.data_points, "key1", "value2")
1263+
.expect("datapoint with key1=value2 expected");
1264+
if temporality == Temporality::Cumulative {
1265+
assert_eq!(data_point1.value, 6);
1266+
} else {
1267+
assert_eq!(data_point1.value, 3);
1268+
}
11251269
}
11261270

11271271
fn updown_counter_aggregation_helper(temporality: Temporality) {
11281272
// Arrange
11291273
let mut test_context = TestContext::new(temporality);
1130-
let counter = test_context.i64_up_down_counter("test", "my_counter", None);
1274+
let counter = test_context.i64_up_down_counter("test", "my_updown_counter", None);
11311275

11321276
// Act
11331277
counter.add(1, &[KeyValue::new("key1", "value1")]);
@@ -1143,7 +1287,7 @@ mod tests {
11431287
test_context.flush_metrics();
11441288

11451289
// Assert
1146-
let sum = test_context.get_aggregation::<data::Sum<i64>>("my_counter", None);
1290+
let sum = test_context.get_aggregation::<data::Sum<i64>>("my_updown_counter", None);
11471291
// Expecting 2 time-series.
11481292
assert_eq!(sum.data_points.len(), 2);
11491293
assert!(
@@ -1168,6 +1312,38 @@ mod tests {
11681312
let data_point1 = find_datapoint_with_key_value(&sum.data_points, "key1", "value2")
11691313
.expect("datapoint with key1=value2 expected");
11701314
assert_eq!(data_point1.value, 3);
1315+
1316+
// Reset and report more measurements
1317+
test_context.reset_metrics();
1318+
counter.add(1, &[KeyValue::new("key1", "value1")]);
1319+
counter.add(1, &[KeyValue::new("key1", "value1")]);
1320+
counter.add(1, &[KeyValue::new("key1", "value1")]);
1321+
counter.add(1, &[KeyValue::new("key1", "value1")]);
1322+
counter.add(1, &[KeyValue::new("key1", "value1")]);
1323+
1324+
counter.add(1, &[KeyValue::new("key1", "value2")]);
1325+
counter.add(1, &[KeyValue::new("key1", "value2")]);
1326+
counter.add(1, &[KeyValue::new("key1", "value2")]);
1327+
1328+
test_context.flush_metrics();
1329+
1330+
let sum = test_context.get_aggregation::<data::Sum<i64>>("my_updown_counter", None);
1331+
assert_eq!(sum.data_points.len(), 2);
1332+
let data_point1 = find_datapoint_with_key_value(&sum.data_points, "key1", "value1")
1333+
.expect("datapoint with key1=value1 expected");
1334+
if temporality == Temporality::Cumulative {
1335+
assert_eq!(data_point1.value, 10);
1336+
} else {
1337+
assert_eq!(data_point1.value, 5);
1338+
}
1339+
1340+
let data_point1 = find_datapoint_with_key_value(&sum.data_points, "key1", "value2")
1341+
.expect("datapoint with key1=value2 expected");
1342+
if temporality == Temporality::Cumulative {
1343+
assert_eq!(data_point1.value, 6);
1344+
} else {
1345+
assert_eq!(data_point1.value, 3);
1346+
}
11711347
}
11721348

11731349
fn find_datapoint_with_key_value<'a, T>(

0 commit comments

Comments
 (0)