Skip to content

Commit d1a195b

Browse files
committed
add more assertions in tests
1 parent 6f20784 commit d1a195b

File tree

1 file changed

+72
-23
lines changed
  • instrumentation/jmx-metrics/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine

1 file changed

+72
-23
lines changed

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

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ void testConf2() {
7575

7676
JmxRule def1 = defs.get(0);
7777
assertThat(def1.getBeans()).containsExactly("OBJECT:NAME1=*", "OBJECT:NAME2=*");
78-
assertThat(def1.getMetricAttribute()).hasSize(2)
78+
assertThat(def1.getMetricAttribute())
79+
.hasSize(2)
7980
.containsEntry("LABEL_KEY1", "param(PARAMETER)")
8081
.containsEntry("LABEL_KEY2", "beanattr(ATTRIBUTE)");
8182

8283
Map<String, Metric> attr = def1.getMapping();
83-
assertThat(attr).hasSize(4)
84+
assertThat(attr)
85+
.hasSize(4)
8486
.containsKeys("ATTRIBUTE1", "ATTRIBUTE2", "ATTRIBUTE3", "ATTRIBUTE4");
8587

8688
Metric m1 = attr.get("ATTRIBUTE1");
@@ -90,6 +92,12 @@ void testConf2() {
9092
assertThat(m1.getUnit()).isEqualTo("UNIT1");
9193
assertThat(m1.getMetricAttribute()).containsExactly(entry("LABEL_KEY3", "const(CONSTANT)"));
9294

95+
Metric m2 = attr.get("ATTRIBUTE2");
96+
assertThat(m2).isNotNull();
97+
assertThat(m2.getMetric()).isEqualTo("METRIC_NAME2");
98+
assertThat(m2.getDesc()).isEqualTo("DESCRIPTION2");
99+
assertThat(m2.getUnit()).isEqualTo("UNIT2");
100+
93101
JmxRule def2 = defs.get(1);
94102
assertThat(def2.getBeans()).containsExactly("OBJECT:NAME3=*");
95103
assertThat(def2.getMetricAttribute()).isNull();
@@ -98,7 +106,6 @@ void testConf2() {
98106
Metric m3 = def2.getMapping().get("ATTRIBUTE3");
99107
assertThat(m3.getMetric()).isEqualTo("METRIC_NAME3");
100108
assertThat(m3.getUnit()).isNull();
101-
102109
}
103110

104111
private static final String CONF3 =
@@ -124,9 +131,15 @@ void testConf3() {
124131
assertThat(def1.getMetricAttribute()).isNull();
125132

126133
Map<String, Metric> attr = def1.getMapping();
127-
assertThat(attr).hasSize(5).containsKey("ATTRIBUTE33");
134+
assertThat(attr)
135+
.hasSize(5)
136+
.containsKeys("ATTRIBUTE31", "ATTRIBUTE32", "ATTRIBUTE33", "ATTRIBUTE34", "ATTRIBUTE35");
137+
assertThat(attr.get("ATTRIBUTE32")).isNull();
128138
assertThat(attr.get("ATTRIBUTE33")).isNull();
129-
assertThat(attr.get("ATTRIBUTE34")).isNotNull();
139+
Metric attribute34 = attr.get("ATTRIBUTE34");
140+
assertThat(attribute34).isNotNull();
141+
assertThat(attribute34.getMetric()).isEqualTo("METRIC_NAME34");
142+
assertThat(attr.get("ATTRIBUTE35")).isNull();
130143
}
131144

132145
/*
@@ -163,31 +176,57 @@ void testConf4() throws Exception {
163176
List<JmxRule> defs = config.getRules();
164177
assertThat(defs).hasSize(1);
165178

166-
MetricDef metricDef = defs.get(0).buildMetricDef();
179+
JmxRule jmxDef = defs.get(0);
180+
assertThat(jmxDef.getUnit()).isEqualTo("DEFAULT_UNIT");
181+
assertThat(jmxDef.getMetricType()).isEqualTo(MetricInfo.Type.UPDOWNCOUNTER);
182+
183+
MetricDef metricDef = jmxDef.buildMetricDef();
167184
assertThat(metricDef).isNotNull();
168-
assertThat(metricDef.getMetricExtractors()).hasSize(3);
169185

170186
assertThat(metricDef.getMetricExtractors())
187+
.hasSize(3)
171188
.anySatisfy(
172189
m -> {
173190
assertThat(m.getMetricValueExtractor().getAttributeName()).isEqualTo("A.b");
174-
assertThat(m.getAttributes()).hasSize(3);
175-
176-
MetricInfo mb1 = m.getInfo();
177-
assertThat(mb1.getMetricName()).isEqualTo("PREFIX.METRIC_NAME1");
178-
assertThat(mb1.getDescription()).isEqualTo("DESCRIPTION1");
179-
assertThat(mb1.getUnit()).isEqualTo("UNIT1");
180-
assertThat(mb1.getType()).isEqualTo(MetricInfo.Type.COUNTER);
191+
assertThat(m.getAttributes())
192+
.hasSize(3)
193+
.extracting("attributeName")
194+
.contains("LABEL_KEY1", "LABEL_KEY2", "LABEL_KEY3");
195+
196+
MetricInfo metricInfo = m.getInfo();
197+
assertThat(metricInfo.getMetricName()).isEqualTo("PREFIX.METRIC_NAME1");
198+
assertThat(metricInfo.getDescription()).isEqualTo("DESCRIPTION1");
199+
assertThat(metricInfo.getUnit()).isEqualTo("UNIT1");
200+
assertThat(metricInfo.getType()).isEqualTo(MetricInfo.Type.COUNTER);
201+
})
202+
.anySatisfy(
203+
m -> {
204+
assertThat(m.getMetricValueExtractor().getAttributeName()).isEqualTo("ATTRIBUTE2");
205+
assertThat(m.getAttributes())
206+
.hasSize(2)
207+
.extracting("attributeName")
208+
.contains("LABEL_KEY1", "LABEL_KEY2");
209+
210+
MetricInfo metricInfo = m.getInfo();
211+
assertThat(metricInfo.getMetricName()).isEqualTo("PREFIX.METRIC_NAME2");
212+
assertThat(metricInfo.getDescription()).isEqualTo("DESCRIPTION2");
213+
assertThat(metricInfo.getUnit()).isEqualTo("UNIT2");
181214
})
182215
.anySatisfy(
183216
m -> {
184217
assertThat(m.getMetricValueExtractor().getAttributeName()).isEqualTo("ATTRIBUTE3");
185218

186-
MetricInfo mb3 = m.getInfo();
187-
assertThat(mb3.getMetricName()).isEqualTo("PREFIX.ATTRIBUTE3");
219+
MetricInfo metricInfo = m.getInfo();
220+
assertThat(metricInfo.getMetricName()).isEqualTo("PREFIX.ATTRIBUTE3");
221+
assertThat(metricInfo.getDescription()).isNull();
222+
188223
// syntax extension - defining a default unit and type
189-
assertThat(mb3.getType()).isEqualTo(MetricInfo.Type.UPDOWNCOUNTER);
190-
assertThat(mb3.getUnit()).isEqualTo("DEFAULT_UNIT");
224+
assertThat(metricInfo.getType())
225+
.describedAs("default type should match jmx rule definition")
226+
.isEqualTo(jmxDef.getMetricType());
227+
assertThat(metricInfo.getUnit())
228+
.describedAs("default unit should match jmx rule definition")
229+
.isEqualTo(jmxDef.getUnit());
191230
});
192231
}
193232

@@ -244,11 +283,13 @@ void testConf6() throws Exception {
244283
MetricExtractor m1 = metricDef.getMetricExtractors()[0];
245284
assertThat(m1.getMetricValueExtractor().getAttributeName()).isEqualTo("ATTRIBUTE");
246285
// MetricAttribute set at the metric level should override the one set at the definition level
247-
assertThat(m1.getAttributes()).hasSize(1);
248-
assertThat(m1.getInfo().getMetricName()).isEqualTo("ATTRIBUTE");
286+
assertThat(m1.getAttributes()).hasSize(1)
287+
.satisfiesExactly(a -> checkConstantMetricAttribute(a, "key1", "value2"));
288+
289+
assertThat(m1.getInfo().getMetricName())
290+
.describedAs("metric name should default to JMX attribute name")
291+
.isEqualTo("ATTRIBUTE");
249292

250-
MetricAttribute l1 = m1.getAttributes()[0];
251-
assertThat(l1.acquireAttributeValue(null, null)).isEqualTo("value2");
252293
}
253294

254295
private static final String CONF7 =
@@ -276,8 +317,10 @@ void testConf7() throws Exception {
276317
// Test that the MBean attribute is correctly parsed
277318
MetricExtractor m1 = metricDef.getMetricExtractors()[0];
278319
assertThat(m1.getMetricValueExtractor().getAttributeName()).isEqualTo("ATTRIBUTE");
279-
assertThat(m1.getAttributes()).hasSize(2);
280320
assertThat(m1.getInfo().getMetricName()).isEqualTo("ATTRIBUTE");
321+
assertThat(m1.getAttributes()).hasSize(2)
322+
.anySatisfy(a -> checkConstantMetricAttribute(a, "key1", "value1"))
323+
.anySatisfy(a -> checkConstantMetricAttribute(a, "key2", "value2"));
281324
}
282325

283326
private static final String EMPTY_CONF = "---\n";
@@ -317,6 +360,12 @@ void testEmptyConf() {
317360
assertThat(config.getRules()).isEmpty();
318361
}
319362

363+
private static void checkConstantMetricAttribute(MetricAttribute attribute, String expectedName,
364+
String expectedValue) {
365+
assertThat(attribute.getAttributeName()).isEqualTo(expectedName);
366+
assertThat(attribute.acquireAttributeValue(null, null)).isEqualTo(expectedValue);
367+
}
368+
320369
private static JmxConfig parseConf(String s) {
321370
InputStream is = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
322371
JmxConfig jmxConfig = parser.loadConfig(is);

0 commit comments

Comments
 (0)