|
| 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 io.opentelemetry.instrumentation.jmx.rules.assertions.AttributeMatcher; |
| 13 | +import java.time.Duration; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import org.junit.jupiter.params.ParameterizedTest; |
| 18 | +import org.junit.jupiter.params.provider.CsvSource; |
| 19 | +import org.testcontainers.containers.GenericContainer; |
| 20 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 21 | + |
| 22 | +public class TomcatIntegrationTest extends TargetSystemTest { |
| 23 | + |
| 24 | + @ParameterizedTest |
| 25 | + @CsvSource({ |
| 26 | + "tomcat:10.0, https://tomcat.apache.org/tomcat-10.0-doc/appdev/sample/sample.war", |
| 27 | + "tomcat:9.0, https://tomcat.apache.org/tomcat-9.0-doc/appdev/sample/sample.war" |
| 28 | + }) |
| 29 | + void testCollectedMetrics(String dockerImageName, String sampleWebApplicationUrl) |
| 30 | + throws Exception { |
| 31 | + List<String> yamlFiles = Collections.singletonList("tomcat.yaml"); |
| 32 | + |
| 33 | + yamlFiles.forEach(this::validateYamlSyntax); |
| 34 | + |
| 35 | + List<String> jvmArgs = new ArrayList<>(); |
| 36 | + jvmArgs.add(javaAgentJvmArgument()); |
| 37 | + jvmArgs.addAll(javaPropertiesToJvmArgs(otelConfigProperties(yamlFiles))); |
| 38 | + |
| 39 | + // testing with a basic tomcat image as test application to capture JVM metrics |
| 40 | + GenericContainer<?> target = |
| 41 | + new GenericContainer<>(dockerImageName) |
| 42 | + .withEnv("CATALINA_OPTS", String.join(" ", jvmArgs)) |
| 43 | + .withStartupTimeout(Duration.ofMinutes(2)) |
| 44 | + .withExposedPorts(8080) |
| 45 | + .waitingFor(Wait.forListeningPorts(8080)); |
| 46 | + |
| 47 | + copyFilesToTarget(target, yamlFiles); |
| 48 | + |
| 49 | + startTarget(target); |
| 50 | + |
| 51 | + // Deploy example web application to the tomcat to enable reporting tomcat.session.active.count |
| 52 | + // metric |
| 53 | + target.execInContainer("rm", "-fr", "/usr/local/tomcat/webapps/ROOT"); |
| 54 | + target.execInContainer( |
| 55 | + "curl", sampleWebApplicationUrl, "-o", "/usr/local/tomcat/webapps/ROOT.war"); |
| 56 | + |
| 57 | + verifyMetrics(createMetricsVerifier()); |
| 58 | + } |
| 59 | + |
| 60 | + private static MetricsVerifier createMetricsVerifier() { |
| 61 | + AttributeMatcher requestProcessorNameAttribute = |
| 62 | + attribute("tomcat.request.processor.name", "\"http-nio-8080\""); |
| 63 | + AttributeMatcher threadPoolNameAttribute = |
| 64 | + attribute("tomcat.thread.pool.name", "\"http-nio-8080\""); |
| 65 | + |
| 66 | + return MetricsVerifier.create() |
| 67 | + .add( |
| 68 | + "tomcat.error.count", |
| 69 | + metric -> |
| 70 | + metric |
| 71 | + .hasDescription("The number of errors.") |
| 72 | + .hasUnit("{error}") |
| 73 | + .isCounter() |
| 74 | + .hasDataPointsWithOneAttribute(requestProcessorNameAttribute)) |
| 75 | + .add( |
| 76 | + "tomcat.request.count", |
| 77 | + metric -> |
| 78 | + metric |
| 79 | + .hasDescription("The number of requests processed.") |
| 80 | + .hasUnit("{request}") |
| 81 | + .isCounter() |
| 82 | + .hasDataPointsWithOneAttribute(requestProcessorNameAttribute)) |
| 83 | + .add( |
| 84 | + "tomcat.request.duration.max", |
| 85 | + metric -> |
| 86 | + metric |
| 87 | + .hasDescription("The longest request processing time.") |
| 88 | + .hasUnit("s") |
| 89 | + .isGauge() |
| 90 | + .hasDataPointsWithOneAttribute(requestProcessorNameAttribute)) |
| 91 | + .add( |
| 92 | + "tomcat.request.duration.sum", |
| 93 | + metric -> |
| 94 | + metric |
| 95 | + .hasDescription("Total time of processing all requests.") |
| 96 | + .hasUnit("s") |
| 97 | + .isCounter() |
| 98 | + .hasDataPointsWithOneAttribute(requestProcessorNameAttribute)) |
| 99 | + .add( |
| 100 | + "tomcat.network.io", |
| 101 | + metric -> |
| 102 | + metric |
| 103 | + .hasDescription("The number of bytes transmitted.") |
| 104 | + .hasUnit("By") |
| 105 | + .isCounter() |
| 106 | + .hasDataPointsWithAttributes( |
| 107 | + attributeGroup( |
| 108 | + attribute("network.io.direction", "receive"), |
| 109 | + requestProcessorNameAttribute), |
| 110 | + attributeGroup( |
| 111 | + attribute("network.io.direction", "transmit"), |
| 112 | + requestProcessorNameAttribute))) |
| 113 | + .add( |
| 114 | + "tomcat.session.active.count", |
| 115 | + metric -> |
| 116 | + metric |
| 117 | + .hasDescription("The number of currently active sessions.") |
| 118 | + .hasUnit("{session}") |
| 119 | + .isUpDownCounter() |
| 120 | + .hasDataPointsWithOneAttribute(attributeWithAnyValue("tomcat.context"))) |
| 121 | + .add( |
| 122 | + "tomcat.session.active.limit", |
| 123 | + metric -> |
| 124 | + metric |
| 125 | + .hasDescription("Maximum possible number of active sessions.") |
| 126 | + .hasUnit("{session}") |
| 127 | + .isUpDownCounter() |
| 128 | + .hasDataPointsWithOneAttribute(attributeWithAnyValue("tomcat.context"))) |
| 129 | + .add( |
| 130 | + "tomcat.thread.count", |
| 131 | + metric -> |
| 132 | + metric |
| 133 | + .hasDescription("Total thread count of the thread pool.") |
| 134 | + .hasUnit("{thread}") |
| 135 | + .isUpDownCounter() |
| 136 | + .hasDataPointsWithOneAttribute(threadPoolNameAttribute)) |
| 137 | + .add( |
| 138 | + "tomcat.thread.limit", |
| 139 | + metric -> |
| 140 | + metric |
| 141 | + .hasDescription("Maximum possible number of threads in the thread pool.") |
| 142 | + .hasUnit("{thread}") |
| 143 | + .isUpDownCounter() |
| 144 | + .hasDataPointsWithOneAttribute(threadPoolNameAttribute)) |
| 145 | + .add( |
| 146 | + "tomcat.thread.busy.count", |
| 147 | + metric -> |
| 148 | + metric |
| 149 | + .hasDescription("Number of busy threads in the thread pool.") |
| 150 | + .hasUnit("{thread}") |
| 151 | + .isUpDownCounter() |
| 152 | + .hasDataPointsWithOneAttribute(threadPoolNameAttribute)); |
| 153 | + } |
| 154 | +} |
0 commit comments