@@ -228,6 +228,13 @@ mod tests {
228
228
updown_counter_aggregation_helper ( Temporality :: Delta ) ;
229
229
}
230
230
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
+
231
238
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
232
239
async fn observable_counter_aggregation_cumulative_non_zero_increment ( ) {
233
240
// Run this test with stdout enabled to see output.
@@ -1046,39 +1053,144 @@ mod tests {
1046
1053
test_context. flush_metrics ( ) ;
1047
1054
1048
1055
// 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 ) ;
1050
1058
// Expecting 2 time-series.
1051
- assert_eq ! ( histogram . data_points. len( ) , 2 ) ;
1059
+ assert_eq ! ( histogram_data . data_points. len( ) , 2 ) ;
1052
1060
if let Temporality :: Cumulative = temporality {
1053
1061
assert_eq ! (
1054
- histogram . temporality,
1062
+ histogram_data . temporality,
1055
1063
Temporality :: Cumulative ,
1056
1064
"Should produce cumulative"
1057
1065
) ;
1058
1066
} else {
1059
1067
assert_eq ! (
1060
- histogram . temporality,
1068
+ histogram_data . temporality,
1061
1069
Temporality :: Delta ,
1062
1070
"Should produce delta"
1063
1071
) ;
1064
1072
}
1065
1073
1066
1074
// find and validate key1=value2 datapoint
1067
1075
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" )
1069
1077
. expect ( "datapoint with key1=value1 expected" ) ;
1070
1078
assert_eq ! ( data_point1. count, values_kv1. len( ) as u64 ) ;
1071
1079
assert_eq ! ( data_point1. sum, values_kv1. iter( ) . sum:: <u64 >( ) ) ;
1072
1080
assert_eq ! ( data_point1. min. unwrap( ) , * values_kv1. iter( ) . min( ) . unwrap( ) ) ;
1073
1081
assert_eq ! ( data_point1. max. unwrap( ) , * values_kv1. iter( ) . max( ) . unwrap( ) ) ;
1074
1082
1075
1083
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" )
1077
1085
. expect ( "datapoint with key1=value2 expected" ) ;
1078
1086
assert_eq ! ( data_point2. count, values_kv2. len( ) as u64 ) ;
1079
1087
assert_eq ! ( data_point2. sum, values_kv2. iter( ) . sum:: <u64 >( ) ) ;
1080
1088
assert_eq ! ( data_point2. min. unwrap( ) , * values_kv2. iter( ) . min( ) . unwrap( ) ) ;
1081
1089
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 ) ;
1082
1194
}
1083
1195
1084
1196
fn counter_aggregation_helper ( temporality : Temporality ) {
@@ -1122,12 +1234,44 @@ mod tests {
1122
1234
let data_point1 = find_datapoint_with_key_value ( & sum. data_points , "key1" , "value2" )
1123
1235
. expect ( "datapoint with key1=value2 expected" ) ;
1124
1236
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
+ }
1125
1269
}
1126
1270
1127
1271
fn updown_counter_aggregation_helper ( temporality : Temporality ) {
1128
1272
// Arrange
1129
1273
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 ) ;
1131
1275
1132
1276
// Act
1133
1277
counter. add ( 1 , & [ KeyValue :: new ( "key1" , "value1" ) ] ) ;
@@ -1143,7 +1287,7 @@ mod tests {
1143
1287
test_context. flush_metrics ( ) ;
1144
1288
1145
1289
// 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 ) ;
1147
1291
// Expecting 2 time-series.
1148
1292
assert_eq ! ( sum. data_points. len( ) , 2 ) ;
1149
1293
assert ! (
@@ -1168,6 +1312,38 @@ mod tests {
1168
1312
let data_point1 = find_datapoint_with_key_value ( & sum. data_points , "key1" , "value2" )
1169
1313
. expect ( "datapoint with key1=value2 expected" ) ;
1170
1314
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
+ }
1171
1347
}
1172
1348
1173
1349
fn find_datapoint_with_key_value < ' a , T > (
0 commit comments