Skip to content

Commit 1733da5

Browse files
authored
more stable metrics sdk tests (#1768)
1 parent 06ed50d commit 1733da5

File tree

1 file changed

+16
-2
lines changed
  • opentelemetry-sdk/src/metrics

1 file changed

+16
-2
lines changed

opentelemetry-sdk/src/metrics/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ mod tests {
784784
counter.add(50, &[]);
785785
test_context.flush_metrics();
786786
let _ = test_context.get_aggregation::<data::Sum<u64>>("my_counter", None);
787+
test_context.reset_metrics();
787788

788789
counter.add(5, &[]);
789790
test_context.flush_metrics();
@@ -810,6 +811,7 @@ mod tests {
810811
counter.add(50, &[]);
811812
test_context.flush_metrics();
812813
let _ = test_context.get_aggregation::<data::Sum<u64>>("my_counter", None);
814+
test_context.reset_metrics();
813815

814816
counter.add(5, &[]);
815817
test_context.flush_metrics();
@@ -836,6 +838,7 @@ mod tests {
836838
counter.add(50, &[]);
837839
test_context.flush_metrics();
838840
let _ = test_context.get_aggregation::<data::Sum<u64>>("my_counter", None);
841+
test_context.reset_metrics();
839842

840843
counter.add(50, &[KeyValue::new("a", "b")]);
841844
test_context.flush_metrics();
@@ -886,6 +889,7 @@ mod tests {
886889
.expect("datapoint with key1=value2 expected");
887890
assert_eq!(data_point1.value, 3);
888891

892+
test_context.exporter.reset();
889893
// flush again, and validate that nothing is flushed
890894
// as delta temporality.
891895
test_context.flush_metrics();
@@ -1072,6 +1076,10 @@ mod tests {
10721076
self.meter_provider.force_flush().unwrap();
10731077
}
10741078

1079+
fn reset_metrics(&self) {
1080+
self.exporter.reset();
1081+
}
1082+
10751083
fn get_aggregation<T: data::Aggregation>(
10761084
&mut self,
10771085
counter_name: &str,
@@ -1087,8 +1095,14 @@ mod tests {
10871095
"no metrics were exported"
10881096
);
10891097

1090-
// Get the latest resource metric in case of multiple flushes/exports
1091-
let resource_metric = self.resource_metrics.last().unwrap();
1098+
assert!(
1099+
self.resource_metrics.len() == 1,
1100+
"Expected single resource metrics."
1101+
);
1102+
let resource_metric = self
1103+
.resource_metrics
1104+
.first()
1105+
.expect("This should contain exactly one resource metric, as validated above.");
10921106

10931107
assert!(
10941108
!resource_metric.scope_metrics.is_empty(),

0 commit comments

Comments
 (0)