2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
using System . Diagnostics . Metrics ;
5
+ using System . Reflection ;
5
6
using OpenTelemetry . Internal ;
6
7
using OpenTelemetry . Tests ;
7
8
using Xunit ;
@@ -919,6 +920,34 @@ public void ViewConflict_OneInstrument_DifferentDescription()
919
920
Assert . Equal ( 10 , metricPoint2 . GetSumLong ( ) ) ;
920
921
}
921
922
923
+ [ Fact ]
924
+ public void CardinalityLimitofMatchingViewTakesPrecedenceOverMetricProviderWhenBothWereSet ( )
925
+ {
926
+ using var meter = new Meter ( Utils . GetCurrentMethodName ( ) ) ;
927
+ var exportedItems = new List < Metric > ( ) ;
928
+
929
+ using var container = this . BuildMeterProvider ( out var meterProvider , builder => builder
930
+ . AddMeter ( meter . Name )
931
+ . SetMaxMetricPointsPerMetricStream ( 3 )
932
+ . AddView ( ( instrument ) =>
933
+ {
934
+ return new MetricStreamConfiguration ( ) { Name = "MetricStreamA" , CardinalityLimit = 10000 } ;
935
+ } )
936
+ . AddInMemoryExporter ( exportedItems ) ) ;
937
+
938
+ var counter = meter . CreateCounter < long > ( "counter" ) ;
939
+ counter . Add ( 100 ) ;
940
+
941
+ meterProvider . ForceFlush ( MaxTimeToAllowForFlush ) ;
942
+
943
+ var metric = exportedItems [ 0 ] ;
944
+
945
+ var aggregatorStore = typeof ( Metric ) . GetField ( "aggStore" , BindingFlags . Instance | BindingFlags . NonPublic ) . GetValue ( metric ) as AggregatorStore ;
946
+ var maxMetricPointsAttribute = ( int ) typeof ( AggregatorStore ) . GetField ( "maxMetricPoints" , BindingFlags . Instance | BindingFlags . NonPublic ) . GetValue ( aggregatorStore ) ;
947
+
948
+ Assert . Equal ( 10000 , maxMetricPointsAttribute ) ;
949
+ }
950
+
922
951
[ Fact ]
923
952
public void ViewConflict_TwoDistinctInstruments_ThreeStreams ( )
924
953
{
@@ -930,13 +959,18 @@ public void ViewConflict_TwoDistinctInstruments_ThreeStreams()
930
959
. AddMeter ( meter . Name )
931
960
. AddView ( ( instrument ) =>
932
961
{
933
- return new MetricStreamConfiguration ( ) { Name = "MetricStreamA" , Description = "description" } ;
962
+ return new MetricStreamConfiguration ( ) { Name = "MetricStreamA" , Description = "description" , CardinalityLimit = 256 } ;
934
963
} )
935
964
. AddView ( ( instrument ) =>
936
965
{
937
966
return instrument . Description == "description1"
938
- ? new MetricStreamConfiguration ( ) { Name = "MetricStreamB" }
939
- : new MetricStreamConfiguration ( ) { Name = "MetricStreamC" } ;
967
+ ? new MetricStreamConfiguration ( ) { Name = "MetricStreamB" , CardinalityLimit = 3 }
968
+ : new MetricStreamConfiguration ( ) { Name = "MetricStreamC" , CardinalityLimit = 200000 } ;
969
+ } )
970
+ . AddView ( ( instrument ) =>
971
+ {
972
+ // This view is ignored as the passed in CardinalityLimit is out of range.
973
+ return new MetricStreamConfiguration ( ) { Name = "MetricStreamD" , CardinalityLimit = - 1 } ;
940
974
} )
941
975
. AddInMemoryExporter ( exportedItems ) ) ;
942
976
@@ -953,12 +987,24 @@ public void ViewConflict_TwoDistinctInstruments_ThreeStreams()
953
987
var metricB = exportedItems [ 1 ] ;
954
988
var metricC = exportedItems [ 2 ] ;
955
989
990
+ var aggregatorStoreA = typeof ( Metric ) . GetField ( "aggStore" , BindingFlags . Instance | BindingFlags . NonPublic ) . GetValue ( metricA ) as AggregatorStore ;
991
+ var maxMetricPointsAttributeA = ( int ) typeof ( AggregatorStore ) . GetField ( "maxMetricPoints" , BindingFlags . Instance | BindingFlags . NonPublic ) . GetValue ( aggregatorStoreA ) ;
992
+
993
+ Assert . Equal ( 256 , maxMetricPointsAttributeA ) ;
956
994
Assert . Equal ( "MetricStreamA" , metricA . Name ) ;
957
995
Assert . Equal ( 20 , GetAggregatedValue ( metricA ) ) ;
958
996
997
+ var aggregatorStoreB = typeof ( Metric ) . GetField ( "aggStore" , BindingFlags . Instance | BindingFlags . NonPublic ) . GetValue ( metricB ) as AggregatorStore ;
998
+ var maxMetricPointsAttributeB = ( int ) typeof ( AggregatorStore ) . GetField ( "maxMetricPoints" , BindingFlags . Instance | BindingFlags . NonPublic ) . GetValue ( aggregatorStoreB ) ;
999
+
1000
+ Assert . Equal ( 3 , maxMetricPointsAttributeB ) ;
959
1001
Assert . Equal ( "MetricStreamB" , metricB . Name ) ;
960
1002
Assert . Equal ( 10 , GetAggregatedValue ( metricB ) ) ;
961
1003
1004
+ var aggregatorStoreC = typeof ( Metric ) . GetField ( "aggStore" , BindingFlags . Instance | BindingFlags . NonPublic ) . GetValue ( metricC ) as AggregatorStore ;
1005
+ var maxMetricPointsAttributeC = ( int ) typeof ( AggregatorStore ) . GetField ( "maxMetricPoints" , BindingFlags . Instance | BindingFlags . NonPublic ) . GetValue ( aggregatorStoreC ) ;
1006
+
1007
+ Assert . Equal ( 200000 , maxMetricPointsAttributeC ) ;
962
1008
Assert . Equal ( "MetricStreamC" , metricC . Name ) ;
963
1009
Assert . Equal ( 10 , GetAggregatedValue ( metricC ) ) ;
964
1010
0 commit comments