@@ -181,6 +181,10 @@ mod tests {
181
181
counter. add ( 100 , & [ KeyValue :: new ( "A" , v. to_string ( ) ) ] ) ;
182
182
}
183
183
184
+ // Empty attributes is specially treated and does not count towards the limit.
185
+ counter. add ( 3 , & [ ] ) ;
186
+ counter. add ( 3 , & [ ] ) ;
187
+
184
188
// All of the below will now go into overflow.
185
189
counter. add ( 100 , & [ KeyValue :: new ( "A" , "foo" ) ] ) ;
186
190
counter. add ( 100 , & [ KeyValue :: new ( "A" , "another" ) ] ) ;
@@ -189,13 +193,26 @@ mod tests {
189
193
190
194
let sum = test_context. get_aggregation :: < data:: Sum < u64 > > ( "my_counter" , None ) ;
191
195
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 ) ;
194
198
195
199
let data_point =
196
200
find_datapoint_with_key_value ( & sum. data_points , "otel.metric.overflow" , "true" )
197
201
. expect ( "overflow point expected" ) ;
198
202
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
+ ) ;
199
216
}
200
217
201
218
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
@@ -763,9 +780,9 @@ mod tests {
763
780
}
764
781
765
782
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
766
- async fn counter_aggregation_attribute_order ( ) {
783
+ async fn counter_aggregation_attribute_order_sorted_first ( ) {
767
784
// 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
769
786
770
787
// Arrange
771
788
let mut test_context = TestContext :: new ( Temporality :: Delta ) ;
@@ -774,14 +791,80 @@ mod tests {
774
791
// Act
775
792
// Add the same set of attributes in different order. (they are expected
776
793
// 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
+ ) ;
777
811
counter. add (
778
812
1 ,
779
813
& [
814
+ KeyValue :: new ( "B" , "b" ) ,
780
815
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" ) ,
781
831
KeyValue :: new ( "B" , "b" ) ,
832
+ KeyValue :: new ( "A" , "a" ) ,
833
+ ] ,
834
+ ) ;
835
+ counter. add (
836
+ 1 ,
837
+ & [
782
838
KeyValue :: new ( "C" , "c" ) ,
839
+ KeyValue :: new ( "A" , "a" ) ,
840
+ KeyValue :: new ( "B" , "b" ) ,
783
841
] ,
784
842
) ;
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
785
868
counter. add (
786
869
1 ,
787
870
& [
@@ -790,6 +873,14 @@ mod tests {
790
873
KeyValue :: new ( "B" , "b" ) ,
791
874
] ,
792
875
) ;
876
+ counter. add (
877
+ 1 ,
878
+ & [
879
+ KeyValue :: new ( "A" , "a" ) ,
880
+ KeyValue :: new ( "B" , "b" ) ,
881
+ KeyValue :: new ( "C" , "c" ) ,
882
+ ] ,
883
+ ) ;
793
884
counter. add (
794
885
1 ,
795
886
& [
0 commit comments