Skip to content

Commit

Permalink
[jmx-scraper] Assertions refactoring - Tomcat integration test conver…
Browse files Browse the repository at this point in the history
…ted (#1589)

Co-authored-by: Sylvain Juge <[email protected]>
Co-authored-by: jason plumb <[email protected]>
  • Loading branch information
3 people authored Dec 11, 2024
1 parent 6c3add9 commit 10efe16
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ class TomcatIntegrationTest extends AbstractIntegrationTest {
void endToEnd() {
waitAndAssertMetrics(
metric ->
assertGauge(metric, "tomcat.sessions", "The number of active sessions.", "sessions"),
assertGauge(metric, "tomcat.sessions", "The number of active sessions.", "{session}"),
metric ->
assertSumWithAttributes(
metric,
"tomcat.errors",
"The number of errors encountered.",
"errors",
"{error}",
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))),
metric ->
assertSumWithAttributes(
Expand All @@ -73,7 +73,7 @@ void endToEnd() {
metric,
"tomcat.traffic",
"The number of bytes transmitted and received.",
"by",
"By",
attrs ->
attrs.containsOnly(
entry("proto_handler", "\"http-nio-8080\""), entry("direction", "sent")),
Expand All @@ -86,7 +86,7 @@ void endToEnd() {
metric,
"tomcat.threads",
"The number of threads",
"threads",
"{thread}",
attrs ->
attrs.containsOnly(
entry("proto_handler", "\"http-nio-8080\""), entry("state", "idle")),
Expand All @@ -105,7 +105,7 @@ void endToEnd() {
metric,
"tomcat.request_count",
"The total requests.",
"requests",
"{request}",
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))));
}
}
20 changes: 10 additions & 10 deletions jmx-metrics/src/main/resources/target-systems/tomcat.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@


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

def beantomcatrequestProcessor = otel.mbeans("Catalina:type=GlobalRequestProcessor,name=*")
otel.instrument(beantomcatrequestProcessor, "tomcat.errors", "The number of errors encountered.", "errors",
otel.instrument(beantomcatrequestProcessor, "tomcat.errors", "The number of errors encountered.", "{error}",
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
"errorCount", otel.&longCounterCallback)
otel.instrument(beantomcatrequestProcessor, "tomcat.request_count", "The total requests.", "requests",
otel.instrument(beantomcatrequestProcessor, "tomcat.request_count", "The total requests.", "{request}",
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
"requestCount", otel.&longCounterCallback)
otel.instrument(beantomcatrequestProcessor, "tomcat.max_time", "Maximum time to process a request.", "ms",
Expand All @@ -32,24 +32,24 @@ otel.instrument(beantomcatrequestProcessor, "tomcat.processing_time", "The total
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
"processingTime", otel.&longCounterCallback)
otel.instrument(beantomcatrequestProcessor, "tomcat.traffic",
"The number of bytes transmitted and received.", "by",
"The number of bytes transmitted and received.", "By",
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name")}],
["bytesReceived":["direction" : {"received"}], "bytesSent": ["direction" : {"sent"}]],
otel.&longCounterCallback)

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

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

def beantomcatnewrequestProcessor = otel.mbeans("Tomcat:type=GlobalRequestProcessor,name=*")
otel.instrument(beantomcatnewrequestProcessor, "tomcat.errors", "The number of errors encountered.", "errors",
otel.instrument(beantomcatnewrequestProcessor, "tomcat.errors", "The number of errors encountered.", "{error}",
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
"errorCount", otel.&longCounterCallback)
otel.instrument(beantomcatnewrequestProcessor, "tomcat.request_count", "The total requests.", "requests",
otel.instrument(beantomcatnewrequestProcessor, "tomcat.request_count", "The total requests.", "{request}",
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
"requestCount", otel.&longCounterCallback)
otel.instrument(beantomcatnewrequestProcessor, "tomcat.max_time", "Maximum time to process a request.", "ms",
Expand All @@ -59,12 +59,12 @@ otel.instrument(beantomcatnewrequestProcessor, "tomcat.processing_time", "The to
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
"processingTime", otel.&longCounterCallback)
otel.instrument(beantomcatnewrequestProcessor, "tomcat.traffic",
"The number of bytes transmitted and received.", "by",
"The number of bytes transmitted and received.", "By",
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name")}],
["bytesReceived":["direction" : {"received"}], "bytesSent": ["direction" : {"sent"}]],
otel.&longCounterCallback)

def beantomcatnewconnectors = otel.mbeans("Tomcat:type=ThreadPool,name=*")
otel.instrument(beantomcatnewconnectors, "tomcat.threads", "The number of threads", "threads",
otel.instrument(beantomcatnewconnectors, "tomcat.threads", "The number of threads", "{thread}",
["proto_handler" : { mbean -> mbean.name().getKeyProperty("name") }],
["currentThreadCount":["state":{"idle"}],"currentThreadsBusy":["state":{"busy"}]], otel.&longValueCallback)
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ class MetricAssertions {

private MetricAssertions() {}

static void assertGauge(Metric metric, String name, String description, String unit) {
assertThat(metric.getName()).isEqualTo(name);
assertThat(metric)
.hasDescription(description)
.hasUnit(unit)
.isGauge()
.hasDataPointsWithoutAttributes();
}

static void assertSum(Metric metric, String name, String description, String unit) {
assertSum(metric, name, description, unit, /* isMonotonic= */ true);
}
Expand Down Expand Up @@ -73,37 +64,6 @@ static void assertSumWithAttributes(
assertAttributedPoints(metric.getSum().getDataPointsList(), attributeGroupAssertions);
}

@SafeVarargs
static void assertSumWithAttributesMultiplePoints(
Metric metric,
String name,
String description,
String unit,
boolean isMonotonic,
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {

assertThat(metric.getName()).isEqualTo(name);
assertThat(metric).hasDescription(description).hasUnit(unit);

assertThat(metric.hasSum()).isTrue();
assertThat(metric.getSum().getIsMonotonic()).isEqualTo(isMonotonic);
assertAttributedMultiplePoints(metric.getSum().getDataPointsList(), attributeGroupAssertions);
}

@SafeVarargs
static void assertGaugeWithAttributes(
Metric metric,
String name,
String description,
String unit,
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {
assertThat(metric.getName()).isEqualTo(name);

assertThat(metric).hasDescription(description).hasUnit(unit).isGauge();

assertAttributedPoints(metric.getGauge().getDataPointsList(), attributeGroupAssertions);
}

@SuppressWarnings("unchecked")
private static void assertAttributedPoints(
List<NumberDataPoint> points,
Expand All @@ -122,22 +82,4 @@ private static void assertAttributedPoints(
KeyValue::getKey, keyValue -> keyValue.getValue().getStringValue())))
.satisfiesExactlyInAnyOrder(assertions);
}

@SuppressWarnings("unchecked")
private static void assertAttributedMultiplePoints(
List<NumberDataPoint> points,
Consumer<MapAssert<String, String>>... attributeGroupAssertions) {

points.stream()
.map(NumberDataPoint::getAttributesList)
.forEach(
kvList -> {
Map<String, String> kvMap =
kvList.stream()
.collect(
Collectors.toMap(KeyValue::getKey, kv -> kv.getValue().getStringValue()));
Arrays.stream(attributeGroupAssertions)
.forEach(assertion -> assertion.accept(assertThat(kvMap)));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

package io.opentelemetry.contrib.jmxscraper.target_systems;

import static io.opentelemetry.contrib.jmxscraper.target_systems.MetricAssertions.assertGaugeWithAttributes;
import static io.opentelemetry.contrib.jmxscraper.target_systems.MetricAssertions.assertSumWithAttributes;
import static org.assertj.core.api.Assertions.entry;
import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute;
import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup;
import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeWithAnyValue;

import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer;
import java.nio.file.Path;
Expand Down Expand Up @@ -46,67 +46,76 @@ protected JmxScraperContainer customizeScraperContainer(
}

@Override
protected void verifyMetrics() {
waitAndAssertMetrics(
metric ->
assertGaugeWithAttributes(
metric,
"tomcat.sessions",
"The number of active sessions",
"sessions",
attrs -> attrs.containsKey("context")),
metric ->
assertSumWithAttributes(
metric,
"tomcat.errors",
"The number of errors encountered",
"errors",
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))),
metric ->
assertSumWithAttributes(
metric,
"tomcat.processing_time",
"The total processing time",
"ms",
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))),
metric ->
assertSumWithAttributes(
metric,
"tomcat.traffic",
"The number of bytes transmitted and received",
"by",
attrs ->
attrs.containsOnly(
entry("proto_handler", "\"http-nio-8080\""), entry("direction", "sent")),
attrs ->
attrs.containsOnly(
entry("proto_handler", "\"http-nio-8080\""),
entry("direction", "received"))),
metric ->
assertGaugeWithAttributes(
metric,
"tomcat.threads",
"The number of threads",
"threads",
attrs ->
attrs.containsOnly(
entry("proto_handler", "\"http-nio-8080\""), entry("state", "idle")),
attrs ->
attrs.containsOnly(
entry("proto_handler", "\"http-nio-8080\""), entry("state", "busy"))),
metric ->
assertGaugeWithAttributes(
metric,
"tomcat.max_time",
"Maximum time to process a request",
"ms",
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))),
metric ->
assertSumWithAttributes(
metric,
"tomcat.request_count",
"The total requests",
"requests",
attrs -> attrs.containsOnly(entry("proto_handler", "\"http-nio-8080\""))));
protected MetricsVerifier createMetricsVerifier() {
return MetricsVerifier.create()
.add(
"tomcat.sessions",
metric ->
metric
.hasDescription("The number of active sessions")
.hasUnit("{session}")
.isGauge()
.hasDataPointsWithOneAttribute(attributeWithAnyValue("context")))
.add(
"tomcat.errors",
metric ->
metric
.hasDescription("The number of errors encountered")
.hasUnit("{error}")
.isCounter()
.hasDataPointsWithOneAttribute(attribute("proto_handler", "\"http-nio-8080\"")))
.add(
"tomcat.processing_time",
metric ->
metric
.hasDescription("The total processing time")
.hasUnit("ms")
.isCounter()
.hasDataPointsWithOneAttribute(attribute("proto_handler", "\"http-nio-8080\"")))
.add(
"tomcat.traffic",
metric ->
metric
.hasDescription("The number of bytes transmitted and received")
.hasUnit("By")
.isCounter()
.hasDataPointsWithAttributes(
attributeGroup(
attribute("direction", "sent"),
attribute("proto_handler", "\"http-nio-8080\"")),
attributeGroup(
attribute("direction", "received"),
attribute("proto_handler", "\"http-nio-8080\""))))
.add(
"tomcat.threads",
metric ->
metric
.hasDescription("The number of threads")
.hasUnit("{thread}")
.isGauge()
.hasDataPointsWithAttributes(
attributeGroup(
attribute("state", "idle"),
attribute("proto_handler", "\"http-nio-8080\"")),
attributeGroup(
attribute("state", "busy"),
attribute("proto_handler", "\"http-nio-8080\""))))
.add(
"tomcat.max_time",
metric ->
metric
.hasDescription("Maximum time to process a request")
.hasUnit("ms")
.isGauge()
.hasDataPointsWithOneAttribute(attribute("proto_handler", "\"http-nio-8080\"")))
.add(
"tomcat.request_count",
metric ->
metric
.hasDescription("The total requests")
.hasUnit("{request}")
.isCounter()
.hasDataPointsWithOneAttribute(
attribute("proto_handler", "\"http-nio-8080\"")));
}
}
14 changes: 7 additions & 7 deletions jmx-scraper/src/main/resources/tomcat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rules:
activeSessions:
metric: tomcat.sessions
type: gauge
unit: sessions
unit: "{session}"
desc: The number of active sessions

- beans:
Expand All @@ -28,12 +28,12 @@ rules:
errorCount:
metric: errors
type: counter
unit: errors
unit: "{error}"
desc: The number of errors encountered
requestCount:
metric: request_count
type: counter
unit: requests
unit: "{request}"
desc: The total requests
maxTime:
metric: max_time
Expand All @@ -48,14 +48,14 @@ rules:
bytesSent:
metric: traffic
type: counter
unit: by
unit: By
desc: The number of bytes transmitted and received
metricAttribute:
direction: const(sent)
bytesReceived:
metric: traffic
type: counter
unit: by
unit: By
desc: The number of bytes transmitted and received
metricAttribute:
direction: const(received)
Expand All @@ -71,13 +71,13 @@ rules:
metric: threads
desc: The number of threads
type: gauge
unit: threads
unit: "{thread}"
metricAttribute:
state: const(idle)
currentThreadsBusy:
metric: threads
desc: The number of threads
type: gauge
unit: threads
unit: "{thread}"
metricAttribute:
state: const(busy)

0 comments on commit 10efe16

Please sign in to comment.