2424import io .opentelemetry .api .trace .SpanContext ;
2525import io .opentelemetry .api .trace .TraceFlags ;
2626import io .opentelemetry .api .trace .TraceState ;
27+ import io .opentelemetry .internal .testing .slf4j .SuppressLogger ;
2728import io .opentelemetry .sdk .common .InstrumentationScopeInfo ;
2829import io .opentelemetry .sdk .metrics .data .AggregationTemporality ;
2930import io .opentelemetry .sdk .metrics .data .ExponentialHistogramBuckets ;
6162import java .io .IOException ;
6263import java .lang .reflect .InvocationTargetException ;
6364import java .lang .reflect .Method ;
65+ import java .lang .reflect .Modifier ;
6466import java .nio .charset .StandardCharsets ;
6567import java .util .ArrayList ;
6668import java .util .Arrays ;
7880import org .junit .jupiter .params .provider .Arguments ;
7981import org .junit .jupiter .params .provider .MethodSource ;
8082
83+ @ SuppressLogger (Otel2PrometheusConverter .class )
8184class Otel2PrometheusConverterTest {
8285
8386 private static final Pattern PATTERN =
@@ -1062,7 +1065,7 @@ void validateCacheIsBounded() {
10621065 void mergeInfoSnapshotsWithSameName () throws Exception {
10631066 InfoSnapshot merged =
10641067 (InfoSnapshot )
1065- invokePrivateStatic (
1068+ invokePrivate (
10661069 "merge" ,
10671070 new Class <?>[] {MetricSnapshot .class , MetricSnapshot .class },
10681071 makeInfoSnapshot ("a" ),
@@ -1074,7 +1077,7 @@ void mergeInfoSnapshotsWithSameName() throws Exception {
10741077 @ Test
10751078 void mergeConflictingTypesReturnsNull () throws Exception {
10761079 Object merged =
1077- invokePrivateStatic (
1080+ invokePrivate (
10781081 "merge" ,
10791082 new Class <?>[] {MetricSnapshot .class , MetricSnapshot .class },
10801083 makeInfoSnapshot ("a" ),
@@ -1088,7 +1091,7 @@ void mergeConflictingTypesReturnsNull() throws Exception {
10881091 @ Test
10891092 void mergeMetadataReturnsNullForDifferentUnits () throws Exception {
10901093 Object merged =
1091- invokePrivateStatic (
1094+ invokePrivate (
10921095 "mergeMetadata" ,
10931096 new Class <?>[] {MetricMetadata .class , MetricMetadata .class },
10941097 MetricMetadata .builder ().name ("sample" ).unit (new Unit ("seconds" )).build (),
@@ -1100,24 +1103,23 @@ void mergeMetadataReturnsNullForDifferentUnits() throws Exception {
11001103 @ Test
11011104 void convertLegacyLabelNameRejectsEmptyName () {
11021105 assertThatThrownBy (
1103- () -> invokePrivateStatic ("convertLegacyLabelName" , new Class <?>[] {String .class }, "" ))
1106+ () -> invokePrivate ("convertLegacyLabelName" , new Class <?>[] {String .class }, "" ))
11041107 .hasCauseInstanceOf (IllegalArgumentException .class )
11051108 .hasRootCauseMessage ("label name is empty" );
11061109 }
11071110
11081111 @ Test
11091112 void stripReservedMetricSuffixesHandlesReservedNameOnly () throws Exception {
11101113 assertThat (
1111- invokePrivateStatic (
1112- "stripReservedMetricSuffixes" , new Class <?>[] {String .class }, "_total" ))
1114+ invokePrivate ("stripReservedMetricSuffixes" , new Class <?>[] {String .class }, "_total" ))
11131115 .isEqualTo ("total" );
11141116 }
11151117
11161118 @ Test
11171119 void validateNormalizedMetricNameRejectsEmptyName () {
11181120 assertThatThrownBy (
11191121 () ->
1120- invokePrivateStatic (
1122+ invokePrivate (
11211123 "validateNormalizedMetricName" ,
11221124 new Class <?>[] {String .class , String .class },
11231125 "orig" ,
@@ -1130,7 +1132,7 @@ void validateNormalizedMetricNameRejectsEmptyName() {
11301132 void convertExponentialHistogramBucketsReturnsEmptyForNoBuckets () throws Exception {
11311133 NativeHistogramBuckets buckets =
11321134 (NativeHistogramBuckets )
1133- invokePrivateStatic (
1135+ invokePrivate (
11341136 "convertExponentialHistogramBuckets" ,
11351137 new Class <?>[] {ExponentialHistogramBuckets .class , int .class },
11361138 ImmutableExponentialHistogramBuckets .create (0 , 0 , Collections .emptyList ()),
@@ -1142,7 +1144,7 @@ void convertExponentialHistogramBucketsReturnsEmptyForNoBuckets() throws Excepti
11421144 @ Test
11431145 void typeStringUsesLowerCaseClassName () throws Exception {
11441146 assertThat (
1145- invokePrivateStatic (
1147+ invokePrivate (
11461148 "typeString" , new Class <?>[] {MetricSnapshot .class }, makeInfoSnapshot ("a" )))
11471149 .isEqualTo ("info" );
11481150 }
@@ -1233,12 +1235,15 @@ private static InfoSnapshot makeInfoSnapshot(String id) {
12331235 Labels .of (new String [] {"id" }, new String [] {id }))));
12341236 }
12351237
1236- private static Object invokePrivateStatic (
1237- String methodName , Class <?>[] parameterTypes , Object ... args ) throws Exception {
1238+ private Object invokePrivate ( String methodName , Class <?>[] parameterTypes , Object ... args )
1239+ throws Exception {
12381240 Method method = Otel2PrometheusConverter .class .getDeclaredMethod (methodName , parameterTypes );
12391241 method .setAccessible (true );
1242+ // Support both static and instance methods: static methods receive null, instance methods
1243+ // receive the converter instance so that instance fields (e.g. throttlingLogger) are available.
1244+ Object instance = Modifier .isStatic (method .getModifiers ()) ? null : converter ;
12401245 try {
1241- return method .invoke (null , args );
1246+ return method .invoke (instance , args );
12421247 } catch (InvocationTargetException e ) {
12431248 throw e ;
12441249 }
0 commit comments