Skip to content

Commit ebf58ff

Browse files
committed
minor code refactor to use lists instead of arrays
1 parent 1bb4302 commit ebf58ff

File tree

6 files changed

+39
-31
lines changed

6 files changed

+39
-31
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
package io.opentelemetry.instrumentation.jmx.engine;
77

8+
import java.util.ArrayList;
9+
import java.util.Collections;
10+
import java.util.List;
811
import javax.annotation.Nullable;
12+
import javax.management.MalformedObjectNameException;
913
import javax.management.ObjectName;
1014
import javax.management.QueryExp;
1115

@@ -16,7 +20,7 @@
1620
public class BeanGroup {
1721
// How to specify the MBean(s)
1822
@Nullable private final QueryExp queryExp;
19-
private final ObjectName[] namePatterns;
23+
private final List<ObjectName> namePatterns;
2024

2125
/**
2226
* Constructor for BeanGroup.
@@ -25,17 +29,29 @@ public class BeanGroup {
2529
* @param namePatterns an array of ObjectNames used to look for MBeans; usually they will be
2630
* patterns. If multiple patterns are provided, they work as logical OR.
2731
*/
28-
public BeanGroup(@Nullable QueryExp queryExp, ObjectName... namePatterns) {
32+
private BeanGroup(@Nullable QueryExp queryExp, List<ObjectName> namePatterns) {
2933
this.queryExp = queryExp;
3034
this.namePatterns = namePatterns;
3135
}
3236

37+
public static BeanGroup forSingleBean(String bean) throws MalformedObjectNameException {
38+
return new BeanGroup(null, Collections.singletonList(new ObjectName(bean)));
39+
}
40+
41+
public static BeanGroup forBeans(List<String> beans) throws MalformedObjectNameException {
42+
List<ObjectName> list = new ArrayList<>();
43+
for (String name : beans) {
44+
list.add(new ObjectName(name));
45+
}
46+
return new BeanGroup(null, list);
47+
}
48+
3349
@Nullable
3450
QueryExp getQueryExp() {
3551
return queryExp;
3652
}
3753

38-
ObjectName[] getNamePatterns() {
54+
List<ObjectName> getNamePatterns() {
3955
return namePatterns;
4056
}
4157
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.instrumentation.jmx.engine;
77

8+
import java.util.List;
9+
810
/**
911
* A class providing a complete definition on how to create an Open Telemetry metric out of the JMX
1012
* system: how to extract values from MBeans and how to model, name and decorate them with
@@ -74,7 +76,7 @@ public class MetricDef {
7476
private final BeanGroup beans;
7577

7678
// Describes how to get the metric values and their attributes, and how to report them
77-
private final MetricExtractor[] metricExtractors;
79+
private final List<MetricExtractor> metricExtractors;
7880

7981
/**
8082
* Constructor for MetricDef.
@@ -84,7 +86,7 @@ public class MetricDef {
8486
* MetricExtractor is provided, they should use unique metric names or unique metric
8587
* attributes
8688
*/
87-
public MetricDef(BeanGroup beans, MetricExtractor... metricExtractors) {
89+
public MetricDef(BeanGroup beans, List<MetricExtractor> metricExtractors) {
8890
this.beans = beans;
8991
this.metricExtractors = metricExtractors;
9092
}
@@ -93,7 +95,7 @@ BeanGroup getBeanGroup() {
9395
return beans;
9496
}
9597

96-
MetricExtractor[] getMetricExtractors() {
98+
List<MetricExtractor> getMetricExtractors() {
9799
return metricExtractors;
98100
}
99101
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.instrumentation.jmx.engine;
77

8+
import java.util.List;
89
import javax.annotation.Nullable;
910

1011
/**
@@ -22,14 +23,14 @@ public class MetricExtractor {
2223
private final BeanAttributeExtractor attributeExtractor;
2324

2425
// Defines the Measurement attributes to be used when reporting the metric value.
25-
private final MetricAttribute[] attributes;
26+
private final List<MetricAttribute> attributes;
2627

2728
@Nullable private volatile DetectionStatus status;
2829

2930
public MetricExtractor(
3031
BeanAttributeExtractor attributeExtractor,
3132
MetricInfo metricInfo,
32-
MetricAttribute... attributes) {
33+
List<MetricAttribute> attributes) {
3334
this.attributeExtractor = attributeExtractor;
3435
this.metricInfo = metricInfo;
3536
this.attributes = attributes;
@@ -43,7 +44,7 @@ BeanAttributeExtractor getMetricValueExtractor() {
4344
return attributeExtractor;
4445
}
4546

46-
MetricAttribute[] getAttributes() {
47+
List<MetricAttribute> getAttributes() {
4748
return attributes;
4849
}
4950

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,8 @@ static Consumer<ObservableLongMeasurement> longTypeCallback(MetricExtractor extr
180180
*/
181181
static Attributes createMetricAttributes(
182182
MBeanServerConnection connection, ObjectName objectName, MetricExtractor extractor) {
183-
MetricAttribute[] metricAttributes = extractor.getAttributes();
184183
AttributesBuilder attrBuilder = Attributes.builder();
185-
for (MetricAttribute metricAttribute : metricAttributes) {
184+
for (MetricAttribute metricAttribute : extractor.getAttributes()) {
186185
String attributeValue = metricAttribute.acquireAttributeValue(connection, objectName);
187186
if (attributeValue != null) {
188187
attrBuilder = attrBuilder.put(metricAttribute.getAttributeName(), attributeValue);

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,9 @@ private static Map<String, Metric> validateAttributeMapping(Map<String, Metric>
128128
public MetricDef buildMetricDef() throws Exception {
129129
BeanGroup group;
130130
if (bean != null) {
131-
group = new BeanGroup(null, new ObjectName(bean));
131+
group = BeanGroup.forSingleBean(bean);
132132
} else if (beans != null && !beans.isEmpty()) {
133-
ObjectName[] objectNames = new ObjectName[beans.size()];
134-
int k = 0;
135-
for (String oneBean : beans) {
136-
objectNames[k++] = new ObjectName(oneBean);
137-
}
138-
group = new BeanGroup(null, objectNames);
133+
group = BeanGroup.forBeans(beans);
139134
} else {
140135
throw new IllegalStateException("No ObjectName specified");
141136
}
@@ -172,9 +167,7 @@ public MetricDef buildMetricDef() throws Exception {
172167
StateMapping stateMapping = getEffectiveStateMapping(m, this);
173168

174169
if (stateMapping.isEmpty()) {
175-
metricExtractors.add(
176-
new MetricExtractor(
177-
attrExtractor, metricInfo, attributeList.toArray(new MetricAttribute[0])));
170+
metricExtractors.add(new MetricExtractor(attrExtractor, metricInfo, attributeList));
178171
} else {
179172

180173
// generate one metric extractor per state metric key
@@ -184,7 +177,7 @@ public MetricDef buildMetricDef() throws Exception {
184177
}
185178
}
186179

187-
return new MetricDef(group, metricExtractors.toArray(new MetricExtractor[0]));
180+
return new MetricDef(group, metricExtractors);
188181
}
189182

190183
private static List<MetricExtractor> createStateMappingExtractors(
@@ -237,10 +230,7 @@ protected Number extractNumericalAttribute(
237230
MetricInfo.Type.UPDOWNCOUNTER);
238231

239232
extractors.add(
240-
new MetricExtractor(
241-
stateMetricExtractor,
242-
stateMetricInfo,
243-
stateMetricAttributes.toArray(new MetricAttribute[0])));
233+
new MetricExtractor(stateMetricExtractor, stateMetricInfo, stateMetricAttributes));
244234
}
245235
return extractors;
246236
}

instrumentation/jmx-metrics/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine/RuleParserTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ void testConf5() throws Exception {
254254
assertThat(metricDef).isNotNull();
255255
assertThat(metricDef.getMetricExtractors()).hasSize(1);
256256

257-
MetricExtractor m1 = metricDef.getMetricExtractors()[0];
257+
MetricExtractor m1 = metricDef.getMetricExtractors().get(0);
258258
assertThat(m1.getMetricValueExtractor().getAttributeName()).isEqualTo("ATTRIBUTE");
259259
assertThat(m1.getAttributes()).isEmpty();
260260

@@ -286,7 +286,7 @@ void testConf6() throws Exception {
286286
assertThat(metricDef).isNotNull();
287287
assertThat(metricDef.getMetricExtractors()).hasSize(1);
288288

289-
MetricExtractor m1 = metricDef.getMetricExtractors()[0];
289+
MetricExtractor m1 = metricDef.getMetricExtractors().get(0);
290290
assertThat(m1.getMetricValueExtractor().getAttributeName()).isEqualTo("ATTRIBUTE");
291291
// MetricAttribute set at the metric level should override the one set at the definition level
292292
assertThat(m1.getAttributes())
@@ -321,7 +321,7 @@ void testConf7() throws Exception {
321321
assertThat(metricDef.getMetricExtractors()).hasSize(1);
322322

323323
// Test that the MBean attribute is correctly parsed
324-
MetricExtractor m1 = metricDef.getMetricExtractors()[0];
324+
MetricExtractor m1 = metricDef.getMetricExtractors().get(0);
325325
assertThat(m1.getMetricValueExtractor().getAttributeName()).isEqualTo("ATTRIBUTE");
326326
assertThat(m1.getInfo().getMetricName()).isEqualTo("ATTRIBUTE");
327327
assertThat(m1.getAttributes())
@@ -350,7 +350,7 @@ void testConf8() throws Exception {
350350
assertThat(metricDef).isNotNull();
351351
assertThat(metricDef.getMetricExtractors()).hasSize(1);
352352

353-
MetricExtractor m1 = metricDef.getMetricExtractors()[0];
353+
MetricExtractor m1 = metricDef.getMetricExtractors().get(0);
354354
assertThat(m1.getMetricValueExtractor().getAttributeName()).isEqualTo("Attr.with.dot");
355355
assertThat(m1.getAttributes()).isEmpty();
356356

@@ -426,7 +426,7 @@ void testStateMetricConf() throws Exception {
426426
assertThat(me.getInfo().getType()).isEqualTo(MetricInfo.Type.UPDOWNCOUNTER);
427427

428428
assertThat(me.getAttributes()).hasSize(1);
429-
MetricAttribute stateAttribute = me.getAttributes()[0];
429+
MetricAttribute stateAttribute = me.getAttributes().get(0);
430430
assertThat(stateAttribute.getAttributeName()).isEqualTo("state_attribute");
431431
String stateAttributeValue =
432432
stateAttribute.acquireAttributeValue(mockConnection, objectName);

0 commit comments

Comments
 (0)