Skip to content

Commit 10efe16

Browse files
robsundaySylvainJugebreedx-splk
authored
[jmx-scraper] Assertions refactoring - Tomcat integration test converted (#1589)
Co-authored-by: Sylvain Juge <[email protected]> Co-authored-by: jason plumb <[email protected]>
1 parent 6c3add9 commit 10efe16

File tree

5 files changed

+96
-145
lines changed

5 files changed

+96
-145
lines changed

jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/TomcatIntegrationTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ class TomcatIntegrationTest extends AbstractIntegrationTest {
5353
void endToEnd() {
5454
waitAndAssertMetrics(
5555
metric ->
56-
assertGauge(metric, "tomcat.sessions", "The number of active sessions.", "sessions"),
56+
assertGauge(metric, "tomcat.sessions", "The number of active sessions.", "{session}"),
5757
metric ->
5858
assertSumWithAttributes(
5959
metric,
6060
"tomcat.errors",
6161
"The number of errors encountered.",
62-
"errors",
62+
"{error}",
6363
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))),
6464
metric ->
6565
assertSumWithAttributes(
@@ -73,7 +73,7 @@ void endToEnd() {
7373
metric,
7474
"tomcat.traffic",
7575
"The number of bytes transmitted and received.",
76-
"by",
76+
"By",
7777
attrs ->
7878
attrs.containsOnly(
7979
entry("proto_handler", "\"http-nio-8080\""), entry("direction", "sent")),
@@ -86,7 +86,7 @@ void endToEnd() {
8686
metric,
8787
"tomcat.threads",
8888
"The number of threads",
89-
"threads",
89+
"{thread}",
9090
attrs ->
9191
attrs.containsOnly(
9292
entry("proto_handler", "\"http-nio-8080\""), entry("state", "idle")),
@@ -105,7 +105,7 @@ void endToEnd() {
105105
metric,
106106
"tomcat.request_count",
107107
"The total requests.",
108-
"requests",
108+
"{request}",
109109
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))));
110110
}
111111
}

jmx-metrics/src/main/resources/target-systems/tomcat.groovy

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717

1818
def beantomcatmanager = otel.mbeans("Catalina:type=Manager,host=localhost,context=*")
19-
otel.instrument(beantomcatmanager, "tomcat.sessions", "The number of active sessions.", "sessions", "activeSessions", otel.&longValueCallback)
19+
otel.instrument(beantomcatmanager, "tomcat.sessions", "The number of active sessions.", "{session}", "activeSessions", otel.&longValueCallback)
2020

2121
def beantomcatrequestProcessor = otel.mbeans("Catalina:type=GlobalRequestProcessor,name=*")
22-
otel.instrument(beantomcatrequestProcessor, "tomcat.errors", "The number of errors encountered.", "errors",
22+
otel.instrument(beantomcatrequestProcessor, "tomcat.errors", "The number of errors encountered.", "{error}",
2323
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
2424
"errorCount", otel.&longCounterCallback)
25-
otel.instrument(beantomcatrequestProcessor, "tomcat.request_count", "The total requests.", "requests",
25+
otel.instrument(beantomcatrequestProcessor, "tomcat.request_count", "The total requests.", "{request}",
2626
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
2727
"requestCount", otel.&longCounterCallback)
2828
otel.instrument(beantomcatrequestProcessor, "tomcat.max_time", "Maximum time to process a request.", "ms",
@@ -32,24 +32,24 @@ otel.instrument(beantomcatrequestProcessor, "tomcat.processing_time", "The total
3232
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
3333
"processingTime", otel.&longCounterCallback)
3434
otel.instrument(beantomcatrequestProcessor, "tomcat.traffic",
35-
"The number of bytes transmitted and received.", "by",
35+
"The number of bytes transmitted and received.", "By",
3636
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name")}],
3737
["bytesReceived":["direction" : {"received"}], "bytesSent": ["direction" : {"sent"}]],
3838
otel.&longCounterCallback)
3939

4040
def beantomcatconnectors = otel.mbeans("Catalina:type=ThreadPool,name=*")
41-
otel.instrument(beantomcatconnectors, "tomcat.threads", "The number of threads", "threads",
41+
otel.instrument(beantomcatconnectors, "tomcat.threads", "The number of threads", "{thread}",
4242
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
4343
["currentThreadCount":["state":{"idle"}],"currentThreadsBusy":["state":{"busy"}]], otel.&longValueCallback)
4444

4545
def beantomcatnewmanager = otel.mbeans("Tomcat:type=Manager,host=localhost,context=*")
46-
otel.instrument(beantomcatnewmanager, "tomcat.sessions", "The number of active sessions.", "sessions", "activeSessions", otel.&longValueCallback)
46+
otel.instrument(beantomcatnewmanager, "tomcat.sessions", "The number of active sessions.", "{session}", "activeSessions", otel.&longValueCallback)
4747

4848
def beantomcatnewrequestProcessor = otel.mbeans("Tomcat:type=GlobalRequestProcessor,name=*")
49-
otel.instrument(beantomcatnewrequestProcessor, "tomcat.errors", "The number of errors encountered.", "errors",
49+
otel.instrument(beantomcatnewrequestProcessor, "tomcat.errors", "The number of errors encountered.", "{error}",
5050
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
5151
"errorCount", otel.&longCounterCallback)
52-
otel.instrument(beantomcatnewrequestProcessor, "tomcat.request_count", "The total requests.", "requests",
52+
otel.instrument(beantomcatnewrequestProcessor, "tomcat.request_count", "The total requests.", "{request}",
5353
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
5454
"requestCount", otel.&longCounterCallback)
5555
otel.instrument(beantomcatnewrequestProcessor, "tomcat.max_time", "Maximum time to process a request.", "ms",
@@ -59,12 +59,12 @@ otel.instrument(beantomcatnewrequestProcessor, "tomcat.processing_time", "The to
5959
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
6060
"processingTime", otel.&longCounterCallback)
6161
otel.instrument(beantomcatnewrequestProcessor, "tomcat.traffic",
62-
"The number of bytes transmitted and received.", "by",
62+
"The number of bytes transmitted and received.", "By",
6363
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name")}],
6464
["bytesReceived":["direction" : {"received"}], "bytesSent": ["direction" : {"sent"}]],
6565
otel.&longCounterCallback)
6666

6767
def beantomcatnewconnectors = otel.mbeans("Tomcat:type=ThreadPool,name=*")
68-
otel.instrument(beantomcatnewconnectors, "tomcat.threads", "The number of threads", "threads",
68+
otel.instrument(beantomcatnewconnectors, "tomcat.threads", "The number of threads", "{thread}",
6969
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
7070
["currentThreadCount":["state":{"idle"}],"currentThreadsBusy":["state":{"busy"}]], otel.&longValueCallback)

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/MetricAssertions.java

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ class MetricAssertions {
2222

2323
private MetricAssertions() {}
2424

25-
static void assertGauge(Metric metric, String name, String description, String unit) {
26-
assertThat(metric.getName()).isEqualTo(name);
27-
assertThat(metric)
28-
.hasDescription(description)
29-
.hasUnit(unit)
30-
.isGauge()
31-
.hasDataPointsWithoutAttributes();
32-
}
33-
3425
static void assertSum(Metric metric, String name, String description, String unit) {
3526
assertSum(metric, name, description, unit, /* isMonotonic= */ true);
3627
}
@@ -73,37 +64,6 @@ static void assertSumWithAttributes(
7364
assertAttributedPoints(metric.getSum().getDataPointsList(), attributeGroupAssertions);
7465
}
7566

76-
@SafeVarargs
77-
static void assertSumWithAttributesMultiplePoints(
78-
Metric metric,
79-
String name,
80-
String description,
81-
String unit,
82-
boolean isMonotonic,
83-
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {
84-
85-
assertThat(metric.getName()).isEqualTo(name);
86-
assertThat(metric).hasDescription(description).hasUnit(unit);
87-
88-
assertThat(metric.hasSum()).isTrue();
89-
assertThat(metric.getSum().getIsMonotonic()).isEqualTo(isMonotonic);
90-
assertAttributedMultiplePoints(metric.getSum().getDataPointsList(), attributeGroupAssertions);
91-
}
92-
93-
@SafeVarargs
94-
static void assertGaugeWithAttributes(
95-
Metric metric,
96-
String name,
97-
String description,
98-
String unit,
99-
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {
100-
assertThat(metric.getName()).isEqualTo(name);
101-
102-
assertThat(metric).hasDescription(description).hasUnit(unit).isGauge();
103-
104-
assertAttributedPoints(metric.getGauge().getDataPointsList(), attributeGroupAssertions);
105-
}
106-
10767
@SuppressWarnings("unchecked")
10868
private static void assertAttributedPoints(
10969
List<NumberDataPoint> points,
@@ -122,22 +82,4 @@ private static void assertAttributedPoints(
12282
KeyValue::getKey, keyValue -> keyValue.getValue().getStringValue())))
12383
.satisfiesExactlyInAnyOrder(assertions);
12484
}
125-
126-
@SuppressWarnings("unchecked")
127-
private static void assertAttributedMultiplePoints(
128-
List<NumberDataPoint> points,
129-
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {
130-
131-
points.stream()
132-
.map(NumberDataPoint::getAttributesList)
133-
.forEach(
134-
kvList -> {
135-
Map<String, String> kvMap =
136-
kvList.stream()
137-
.collect(
138-
Collectors.toMap(KeyValue::getKey, kv -> kv.getValue().getStringValue()));
139-
Arrays.stream(attributeGroupAssertions)
140-
.forEach(assertion -> assertion.accept(assertThat(kvMap)));
141-
});
142-
}
14385
}

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/TomcatIntegrationTest.java

Lines changed: 74 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
package io.opentelemetry.contrib.jmxscraper.target_systems;
77

8-
import static io.opentelemetry.contrib.jmxscraper.target_systems.MetricAssertions.assertGaugeWithAttributes;
9-
import static io.opentelemetry.contrib.jmxscraper.target_systems.MetricAssertions.assertSumWithAttributes;
10-
import static org.assertj.core.api.Assertions.entry;
8+
import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute;
9+
import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup;
10+
import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeWithAnyValue;
1111

1212
import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer;
1313
import java.nio.file.Path;
@@ -46,67 +46,76 @@ protected JmxScraperContainer customizeScraperContainer(
4646
}
4747

4848
@Override
49-
protected void verifyMetrics() {
50-
waitAndAssertMetrics(
51-
metric ->
52-
assertGaugeWithAttributes(
53-
metric,
54-
"tomcat.sessions",
55-
"The number of active sessions",
56-
"sessions",
57-
attrs -> attrs.containsKey("context")),
58-
metric ->
59-
assertSumWithAttributes(
60-
metric,
61-
"tomcat.errors",
62-
"The number of errors encountered",
63-
"errors",
64-
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))),
65-
metric ->
66-
assertSumWithAttributes(
67-
metric,
68-
"tomcat.processing_time",
69-
"The total processing time",
70-
"ms",
71-
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))),
72-
metric ->
73-
assertSumWithAttributes(
74-
metric,
75-
"tomcat.traffic",
76-
"The number of bytes transmitted and received",
77-
"by",
78-
attrs ->
79-
attrs.containsOnly(
80-
entry("proto_handler", "\"http-nio-8080\""), entry("direction", "sent")),
81-
attrs ->
82-
attrs.containsOnly(
83-
entry("proto_handler", "\"http-nio-8080\""),
84-
entry("direction", "received"))),
85-
metric ->
86-
assertGaugeWithAttributes(
87-
metric,
88-
"tomcat.threads",
89-
"The number of threads",
90-
"threads",
91-
attrs ->
92-
attrs.containsOnly(
93-
entry("proto_handler", "\"http-nio-8080\""), entry("state", "idle")),
94-
attrs ->
95-
attrs.containsOnly(
96-
entry("proto_handler", "\"http-nio-8080\""), entry("state", "busy"))),
97-
metric ->
98-
assertGaugeWithAttributes(
99-
metric,
100-
"tomcat.max_time",
101-
"Maximum time to process a request",
102-
"ms",
103-
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))),
104-
metric ->
105-
assertSumWithAttributes(
106-
metric,
107-
"tomcat.request_count",
108-
"The total requests",
109-
"requests",
110-
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))));
49+
protected MetricsVerifier createMetricsVerifier() {
50+
return MetricsVerifier.create()
51+
.add(
52+
"tomcat.sessions",
53+
metric ->
54+
metric
55+
.hasDescription("The number of active sessions")
56+
.hasUnit("{session}")
57+
.isGauge()
58+
.hasDataPointsWithOneAttribute(attributeWithAnyValue("context")))
59+
.add(
60+
"tomcat.errors",
61+
metric ->
62+
metric
63+
.hasDescription("The number of errors encountered")
64+
.hasUnit("{error}")
65+
.isCounter()
66+
.hasDataPointsWithOneAttribute(attribute("proto_handler", "\"http-nio-8080\"")))
67+
.add(
68+
"tomcat.processing_time",
69+
metric ->
70+
metric
71+
.hasDescription("The total processing time")
72+
.hasUnit("ms")
73+
.isCounter()
74+
.hasDataPointsWithOneAttribute(attribute("proto_handler", "\"http-nio-8080\"")))
75+
.add(
76+
"tomcat.traffic",
77+
metric ->
78+
metric
79+
.hasDescription("The number of bytes transmitted and received")
80+
.hasUnit("By")
81+
.isCounter()
82+
.hasDataPointsWithAttributes(
83+
attributeGroup(
84+
attribute("direction", "sent"),
85+
attribute("proto_handler", "\"http-nio-8080\"")),
86+
attributeGroup(
87+
attribute("direction", "received"),
88+
attribute("proto_handler", "\"http-nio-8080\""))))
89+
.add(
90+
"tomcat.threads",
91+
metric ->
92+
metric
93+
.hasDescription("The number of threads")
94+
.hasUnit("{thread}")
95+
.isGauge()
96+
.hasDataPointsWithAttributes(
97+
attributeGroup(
98+
attribute("state", "idle"),
99+
attribute("proto_handler", "\"http-nio-8080\"")),
100+
attributeGroup(
101+
attribute("state", "busy"),
102+
attribute("proto_handler", "\"http-nio-8080\""))))
103+
.add(
104+
"tomcat.max_time",
105+
metric ->
106+
metric
107+
.hasDescription("Maximum time to process a request")
108+
.hasUnit("ms")
109+
.isGauge()
110+
.hasDataPointsWithOneAttribute(attribute("proto_handler", "\"http-nio-8080\"")))
111+
.add(
112+
"tomcat.request_count",
113+
metric ->
114+
metric
115+
.hasDescription("The total requests")
116+
.hasUnit("{request}")
117+
.isCounter()
118+
.hasDataPointsWithOneAttribute(
119+
attribute("proto_handler", "\"http-nio-8080\"")));
111120
}
112121
}

jmx-scraper/src/main/resources/tomcat.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ rules:
1515
activeSessions:
1616
metric: tomcat.sessions
1717
type: gauge
18-
unit: sessions
18+
unit: "{session}"
1919
desc: The number of active sessions
2020

2121
- beans:
@@ -28,12 +28,12 @@ rules:
2828
errorCount:
2929
metric: errors
3030
type: counter
31-
unit: errors
31+
unit: "{error}"
3232
desc: The number of errors encountered
3333
requestCount:
3434
metric: request_count
3535
type: counter
36-
unit: requests
36+
unit: "{request}"
3737
desc: The total requests
3838
maxTime:
3939
metric: max_time
@@ -48,14 +48,14 @@ rules:
4848
bytesSent:
4949
metric: traffic
5050
type: counter
51-
unit: by
51+
unit: By
5252
desc: The number of bytes transmitted and received
5353
metricAttribute:
5454
direction: const(sent)
5555
bytesReceived:
5656
metric: traffic
5757
type: counter
58-
unit: by
58+
unit: By
5959
desc: The number of bytes transmitted and received
6060
metricAttribute:
6161
direction: const(received)
@@ -71,13 +71,13 @@ rules:
7171
metric: threads
7272
desc: The number of threads
7373
type: gauge
74-
unit: threads
74+
unit: "{thread}"
7575
metricAttribute:
7676
state: const(idle)
7777
currentThreadsBusy:
7878
metric: threads
7979
desc: The number of threads
8080
type: gauge
81-
unit: threads
81+
unit: "{thread}"
8282
metricAttribute:
8383
state: const(busy)

0 commit comments

Comments
 (0)