Skip to content

Commit c684cb1

Browse files
committed
minor refactor: make constructor private
1 parent ffda1aa commit c684cb1

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

instrumentation/jmx-metrics/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanAttributeExtractor.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static java.util.logging.Level.FINE;
99
import static java.util.logging.Level.INFO;
1010

11+
import io.opentelemetry.instrumentation.jmx.yaml.StateMapping;
1112
import java.util.ArrayList;
1213
import java.util.List;
1314
import 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
}

instrumentation/jmx-metrics/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/JmxRule.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Map;
2020
import java.util.Set;
2121
import javax.annotation.Nullable;
22-
import javax.management.MBeanServerConnection;
2322
import javax.management.MalformedObjectNameException;
2423
import javax.management.ObjectName;
2524

@@ -214,25 +213,8 @@ private static List<MetricExtractor> createStateMappingExtractors(
214213
}
215214
}
216215

217-
BeanAttributeExtractor stateMetricExtractor =
218-
new BeanAttributeExtractor(attrExtractor.getAttributeName()) {
219-
220-
@Override
221-
protected Object getSampleValue(
222-
MBeanServerConnection connection, ObjectName objectName) {
223-
// metric actual type is sampled in the discovery process, so we have to
224-
// make this extractor as extracting integers.
225-
return 0;
226-
}
227-
228-
@Override
229-
protected Number extractNumericalAttribute(
230-
MBeanServerConnection connection, ObjectName objectName) {
231-
String rawStateValue = attrExtractor.extractValue(connection, objectName);
232-
String mappedStateValue = stateMapping.getStateValue(rawStateValue);
233-
return key.equals(mappedStateValue) ? 1 : 0;
234-
}
235-
};
216+
BeanAttributeExtractor extractor =
217+
BeanAttributeExtractor.forStateMetric(attrExtractor, key, stateMapping);
236218

237219
// state metric are always up/down counters
238220
MetricInfo stateMetricInfo =
@@ -243,8 +225,7 @@ protected Number extractNumericalAttribute(
243225
metricInfo.getUnit(),
244226
MetricInfo.Type.UPDOWNCOUNTER);
245227

246-
extractors.add(
247-
new MetricExtractor(stateMetricExtractor, stateMetricInfo, stateMetricAttributes));
228+
extractors.add(new MetricExtractor(extractor, stateMetricInfo, stateMetricAttributes));
248229
}
249230
return extractors;
250231
}

0 commit comments

Comments
 (0)