88import static java .util .logging .Level .FINE ;
99import static java .util .logging .Level .INFO ;
1010
11+ import io .opentelemetry .instrumentation .jmx .yaml .StateMapping ;
1112import java .util .ArrayList ;
1213import java .util .List ;
1314import java .util .logging .Level ;
@@ -103,7 +104,7 @@ private static void verifyAndAddNameSegment(List<String> segments, StringBuilder
103104 segments .add (newSegment );
104105 }
105106
106- public BeanAttributeExtractor (String baseName , String ... nameChain ) {
107+ private BeanAttributeExtractor (String baseName , String ... nameChain ) {
107108 if (baseName == null || nameChain == null ) {
108109 throw new IllegalArgumentException ("null argument for BeanAttributeExtractor" );
109110 }
@@ -289,10 +290,11 @@ private String extractStringAttribute(MBeanServerConnection connection, ObjectNa
289290 }
290291
291292 /**
292- * Wraps provided extractor to filter-out negative values by replacing them with {@literal null}.
293+ * Provides a bean attribute extractor to filter-out negative values by replacing them with
294+ * {@literal null}.
293295 *
294- * @param extractor extractor to wrap
295- * @return extractor filtering-out negative values
296+ * @param extractor original extractor
297+ * @return equivalent extractor filtering-out negative values
296298 */
297299 public static BeanAttributeExtractor filterNegativeValues (BeanAttributeExtractor extractor ) {
298300 return new BeanAttributeExtractor (extractor .baseName , extractor .nameChain ) {
@@ -310,4 +312,25 @@ protected Number extractNumericalAttribute(
310312 }
311313 };
312314 }
315+
316+ public static BeanAttributeExtractor forStateMetric (
317+ BeanAttributeExtractor extractor , String key , StateMapping stateMapping ) {
318+ return new BeanAttributeExtractor (extractor .baseName , extractor .nameChain ) {
319+ @ Override
320+ protected Object getSampleValue (MBeanServerConnection connection , ObjectName objectName ) {
321+ // metric actual type is sampled in the discovery process, so we have to
322+ // make this extractor as extracting integers.
323+ return 0 ;
324+ }
325+
326+ @ Nullable
327+ @ Override
328+ protected Number extractNumericalAttribute (
329+ MBeanServerConnection connection , ObjectName objectName ) {
330+ String rawStateValue = extractor .extractValue (connection , objectName );
331+ String mappedStateValue = stateMapping .getStateValue (rawStateValue );
332+ return key .equals (mappedStateValue ) ? 1 : 0 ;
333+ }
334+ };
335+ }
313336}
0 commit comments