Skip to content

Commit 8300586

Browse files
committed
Nit additions to Metrics tests
1 parent 1719244 commit 8300586

File tree

1 file changed

+95
-4
lines changed
  • opentelemetry-sdk/src/metrics

1 file changed

+95
-4
lines changed

opentelemetry-sdk/src/metrics/mod.rs

+95-4
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ mod tests {
181181
counter.add(100, &[KeyValue::new("A", v.to_string())]);
182182
}
183183

184+
// Empty attributes is specially treated and does not count towards the limit.
185+
counter.add(3, &[]);
186+
counter.add(3, &[]);
187+
184188
// All of the below will now go into overflow.
185189
counter.add(100, &[KeyValue::new("A", "foo")]);
186190
counter.add(100, &[KeyValue::new("A", "another")]);
@@ -189,13 +193,26 @@ mod tests {
189193

190194
let sum = test_context.get_aggregation::<data::Sum<u64>>("my_counter", None);
191195

192-
// Expecting 2001 metric points. (2000 + 1 overflow)
193-
assert_eq!(sum.data_points.len(), 2001);
196+
// Expecting 2002 metric points. (2000 + 1 overflow + Empty attributes)
197+
assert_eq!(sum.data_points.len(), 2002);
194198

195199
let data_point =
196200
find_datapoint_with_key_value(&sum.data_points, "otel.metric.overflow", "true")
197201
.expect("overflow point expected");
198202
assert_eq!(data_point.value, 300);
203+
204+
// TODO: This is relying on the current behavior of the SDK that 0th
205+
// point is empty attributes, but it is not guaranteed to be the case in
206+
// the future.
207+
let empty_attrs_data_point = &sum.data_points[0];
208+
assert!(
209+
empty_attrs_data_point.attributes.is_empty(),
210+
"Non-empty attribute set"
211+
);
212+
assert_eq!(
213+
empty_attrs_data_point.value, 6,
214+
"Empty attributes value should be 3+3=6"
215+
);
199216
}
200217

201218
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
@@ -763,9 +780,9 @@ mod tests {
763780
}
764781

765782
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
766-
async fn counter_aggregation_attribute_order() {
783+
async fn counter_aggregation_attribute_order_sorted_first() {
767784
// Run this test with stdout enabled to see output.
768-
// cargo test counter_aggregation_attribute_order --features=testing -- --nocapture
785+
// cargo test counter_aggregation_attribute_order_sorted_first --features=testing -- --nocapture
769786

770787
// Arrange
771788
let mut test_context = TestContext::new(Temporality::Delta);
@@ -774,14 +791,80 @@ mod tests {
774791
// Act
775792
// Add the same set of attributes in different order. (they are expected
776793
// to be treated as same attributes)
794+
// start with sorted order
795+
counter.add(
796+
1,
797+
&[
798+
KeyValue::new("A", "a"),
799+
KeyValue::new("B", "b"),
800+
KeyValue::new("C", "c"),
801+
],
802+
);
803+
counter.add(
804+
1,
805+
&[
806+
KeyValue::new("A", "a"),
807+
KeyValue::new("C", "c"),
808+
KeyValue::new("B", "b"),
809+
],
810+
);
777811
counter.add(
778812
1,
779813
&[
814+
KeyValue::new("B", "b"),
780815
KeyValue::new("A", "a"),
816+
KeyValue::new("C", "c"),
817+
],
818+
);
819+
counter.add(
820+
1,
821+
&[
822+
KeyValue::new("B", "b"),
823+
KeyValue::new("C", "c"),
824+
KeyValue::new("A", "a"),
825+
],
826+
);
827+
counter.add(
828+
1,
829+
&[
830+
KeyValue::new("C", "c"),
781831
KeyValue::new("B", "b"),
832+
KeyValue::new("A", "a"),
833+
],
834+
);
835+
counter.add(
836+
1,
837+
&[
782838
KeyValue::new("C", "c"),
839+
KeyValue::new("A", "a"),
840+
KeyValue::new("B", "b"),
783841
],
784842
);
843+
test_context.flush_metrics();
844+
845+
let sum = test_context.get_aggregation::<data::Sum<u64>>("my_counter", None);
846+
847+
// Expecting 1 time-series.
848+
assert_eq!(sum.data_points.len(), 1);
849+
850+
// validate the sole datapoint
851+
let data_point1 = &sum.data_points[0];
852+
assert_eq!(data_point1.value, 6);
853+
}
854+
855+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
856+
async fn counter_aggregation_attribute_order_unsorted_first() {
857+
// Run this test with stdout enabled to see output.
858+
// cargo test counter_aggregation_attribute_order_unsorted_first --features=testing -- --nocapture
859+
860+
// Arrange
861+
let mut test_context = TestContext::new(Temporality::Delta);
862+
let counter = test_context.u64_counter("test", "my_counter", None);
863+
864+
// Act
865+
// Add the same set of attributes in different order. (they are expected
866+
// to be treated as same attributes)
867+
// start with unsorted order
785868
counter.add(
786869
1,
787870
&[
@@ -790,6 +873,14 @@ mod tests {
790873
KeyValue::new("B", "b"),
791874
],
792875
);
876+
counter.add(
877+
1,
878+
&[
879+
KeyValue::new("A", "a"),
880+
KeyValue::new("B", "b"),
881+
KeyValue::new("C", "c"),
882+
],
883+
);
793884
counter.add(
794885
1,
795886
&[

0 commit comments

Comments
 (0)