Skip to content

Commit e272d84

Browse files
committed
Tomcat test added
1 parent dfae1ac commit e272d84

File tree

2 files changed

+141
-14
lines changed

2 files changed

+141
-14
lines changed

instrumentation/jmx-metrics/javaagent/src/main/resources/jmx/rules/tomcat.yaml renamed to instrumentation/jmx-metrics/library/src/main/resources/jmx/rules/tomcat.yaml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ rules:
66
- beans:
77
- Catalina:type=GlobalRequestProcessor,name=*
88
- Tomcat:type=GlobalRequestProcessor,name=*
9-
prefix: http.server.tomcat.
9+
prefix: tomcat.
1010
metricAttribute:
11-
name: param(name)
11+
tomcat.request_processor.name: param(name)
1212
mapping:
1313
errorCount:
14-
metric: error_count
14+
metric: error.count
1515
type: counter
1616
unit: "{error}"
1717
desc: The number of errors.
@@ -21,7 +21,7 @@ rules:
2121
unit: "{request}"
2222
desc: The number of requests processed.
2323
maxTime:
24-
metric: request.max_time
24+
metric: request.duration.max
2525
type: gauge
2626
sourceUnit: ms
2727
unit: s
@@ -33,29 +33,29 @@ rules:
3333
unit: s
3434
desc: Total time for processing all requests.
3535
bytesReceived:
36-
metric: &metric traffic
36+
metric: &metric network.io
3737
type: &type counter
3838
unit: &unit By
3939
desc: &desc The number of bytes transmitted.
4040
metricAttribute:
41-
direction: const(received)
41+
tomcat.network.io.direction: const(received)
4242
bytesSent:
4343
metric: *metric
4444
type: *type
4545
unit: *unit
4646
desc: *desc
4747
metricAttribute:
48-
direction: const(sent)
48+
tomcat.network.io.direction: const(sent)
4949

5050
- beans:
5151
- Catalina:type=Manager,host=localhost,context=*
5252
- Tomcat:type=Manager,host=localhost,context=*
53-
prefix: http.server.tomcat.
53+
prefix: tomcat.
5454
metricAttribute:
55-
context: param(context)
55+
tomcat.web_app_context: param(context)
5656
mapping:
5757
activeSessions:
58-
metric: sessions.activeSessions
58+
metric: active_session.count
5959
type: updowncounter
6060
unit: "{session}"
6161
desc: The number of active sessions.
@@ -64,14 +64,14 @@ rules:
6464
- Catalina:type=ThreadPool,name=*
6565
- Tomcat:type=ThreadPool,name=*
6666
unit: "{thread}"
67-
prefix: http.server.tomcat.
67+
prefix: tomcat.
6868
type: updowncounter
6969
metricAttribute:
70-
name: param(name)
70+
tomcat.thread_pool.name: param(name)
7171
mapping:
7272
currentThreadCount:
73-
metric: &metric thread_count
74-
desc: &desc Thread Count of the Thread Pool.
73+
metric: &metric thread.count
74+
desc: &desc Thread count of the thread pool.
7575
metricAttribute:
7676
state: const(idle)
7777
currentThreadsBusy:
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.jmx.rules;
7+
8+
import static io.opentelemetry.instrumentation.jmx.rules.assertions.DataPointAttributes.attribute;
9+
import static io.opentelemetry.instrumentation.jmx.rules.assertions.DataPointAttributes.attributeGroup;
10+
import static io.opentelemetry.instrumentation.jmx.rules.assertions.DataPointAttributes.attributeWithAnyValue;
11+
12+
import java.time.Duration;
13+
import java.util.ArrayList;
14+
import java.util.Collections;
15+
import java.util.List;
16+
import org.junit.jupiter.params.ParameterizedTest;
17+
import org.junit.jupiter.params.provider.ValueSource;
18+
import org.testcontainers.containers.GenericContainer;
19+
import org.testcontainers.containers.wait.strategy.Wait;
20+
21+
public class TomcatIntegrationTest extends TargetSystemTest {
22+
23+
@ParameterizedTest
24+
@ValueSource(
25+
strings = {
26+
"tomcat:10.0",
27+
"tomcat:9.0"
28+
})
29+
void testCollectedMetrics(String dockerImageName) throws Exception {
30+
List<String> yamlFiles = Collections.singletonList("tomcat.yaml");
31+
32+
yamlFiles.forEach(this::validateYamlSyntax);
33+
34+
List<String> jvmArgs = new ArrayList<>();
35+
jvmArgs.add(javaAgentJvmArgument());
36+
jvmArgs.addAll(javaPropertiesToJvmArgs(otelConfigProperties(yamlFiles)));
37+
38+
// testing with a basic tomcat image as test application to capture JVM metrics
39+
GenericContainer<?> target =
40+
new GenericContainer<>(dockerImageName)
41+
.withEnv("CATALINA_OPTS", String.join(" ", jvmArgs))
42+
.withStartupTimeout(Duration.ofMinutes(2))
43+
.withExposedPorts(8080)
44+
.waitingFor(Wait.forListeningPorts(8080));
45+
46+
copyFilesToTarget(target, yamlFiles);
47+
48+
startTarget(target);
49+
50+
verifyMetrics(createMetricsVerifier());
51+
}
52+
53+
private static MetricsVerifier createMetricsVerifier() {
54+
return MetricsVerifier.create()
55+
.add(
56+
"tomcat.error.count",
57+
metric ->
58+
metric
59+
.hasDescription("The number of errors.")
60+
.hasUnit("{error}")
61+
.isCounter()
62+
.hasDataPointsWithOneAttribute(attribute("tomcat.request_processor.name", "\"http-nio-8080\"")))
63+
.add(
64+
"tomcat.request.count",
65+
metric ->
66+
metric
67+
.hasDescription("The number of requests processed.")
68+
.hasUnit("{request}")
69+
.isCounter()
70+
.hasDataPointsWithOneAttribute(attribute("tomcat.request_processor.name", "\"http-nio-8080\"")))
71+
.add(
72+
"tomcat.request.duration.max",
73+
metric ->
74+
metric
75+
.hasDescription("The longest request processing time.")
76+
.hasUnit("s")
77+
.isGauge()
78+
.hasDataPointsWithOneAttribute(attribute("tomcat.request_processor.name", "\"http-nio-8080\"")))
79+
.add(
80+
"tomcat.request.processing_time",
81+
metric ->
82+
metric
83+
.hasDescription("Total time for processing all requests.")
84+
.hasUnit("s")
85+
.isCounter()
86+
.hasDataPointsWithOneAttribute(attribute("tomcat.request_processor.name", "\"http-nio-8080\"")))
87+
.add(
88+
"tomcat.network.io",
89+
metric ->
90+
metric
91+
.hasDescription("The number of bytes transmitted and received")
92+
.hasUnit("By")
93+
.isCounter()
94+
.hasDataPointsWithAttributes(
95+
attributeGroup(
96+
attribute("tomcat.network.io.direction", "sent"),
97+
attribute("tomcat.request_processor.name", "\"http-nio-8080\"")),
98+
attributeGroup(
99+
attribute("tomcat.network.io.direction", "received"),
100+
attribute("tomcat.request_processor.name", "\"http-nio-8080\""))))
101+
102+
.add(
103+
"tomcat.active_session.count",
104+
metric ->
105+
metric
106+
.hasDescription("The number of active sessions.")
107+
.hasUnit("{session}")
108+
.isUpDownCounter()
109+
.hasDataPointsWithOneAttribute(attributeWithAnyValue("tomcat.web_app_context")))
110+
111+
.add(
112+
"tomcat.thread.count",
113+
metric ->
114+
metric
115+
.hasDescription("Thread count of the thread pool.")
116+
.hasUnit("{thread}")
117+
.isUpDownCounter()
118+
.hasDataPointsWithAttributes(
119+
attributeGroup(
120+
attribute("state", "idle"),
121+
attribute("tomcat.thread_pool.name", "\"http-nio-8080\"")),
122+
attributeGroup(
123+
attribute("state", "busy"),
124+
attribute("tomcat.thread_pool.name", "\"http-nio-8080\""))));
125+
126+
}
127+
}

0 commit comments

Comments
 (0)