|
| 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 io.opentelemetry.instrumentation.jmx.rules.assertions.AttributeMatcherGroup; |
| 14 | +import java.time.Duration; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | +import org.junit.jupiter.params.ParameterizedTest; |
| 19 | +import org.junit.jupiter.params.provider.ValueSource; |
| 20 | +import org.testcontainers.containers.GenericContainer; |
| 21 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 22 | + |
| 23 | +class JvmTargetSystemTest extends TargetSystemTest { |
| 24 | + |
| 25 | + @ParameterizedTest |
| 26 | + @ValueSource( |
| 27 | + strings = { |
| 28 | + // openj9 image that might have slight differences for JVM metrics |
| 29 | + "tomcat:jdk8-adoptopenjdk-openj9", |
| 30 | + // basic tomcat image with standard hotspot jdk |
| 31 | + "tomcat:9.0" |
| 32 | + }) |
| 33 | + void testJvmMetrics(String image) { |
| 34 | + List<String> yamlFiles = Collections.singletonList("jvm.yaml"); |
| 35 | + |
| 36 | + yamlFiles.forEach(this::validateYamlSyntax); |
| 37 | + |
| 38 | + List<String> jvmArgs = new ArrayList<>(); |
| 39 | + jvmArgs.add(javaAgentJvmArgument()); |
| 40 | + jvmArgs.addAll(javaPropertiesToJvmArgs(otelConfigProperties(yamlFiles))); |
| 41 | + |
| 42 | + // testing with a basic tomcat image as test application to capture JVM metrics |
| 43 | + GenericContainer<?> target = |
| 44 | + new GenericContainer<>(image) |
| 45 | + .withEnv("CATALINA_OPTS", String.join(" ", jvmArgs)) |
| 46 | + .withStartupTimeout(Duration.ofMinutes(2)) |
| 47 | + .withExposedPorts(8080) |
| 48 | + .waitingFor(Wait.forListeningPorts(8080)); |
| 49 | + |
| 50 | + copyFilesToTarget(target, yamlFiles); |
| 51 | + |
| 52 | + startTarget(target); |
| 53 | + |
| 54 | + AttributeMatcher poolNameAttribute = attributeWithAnyValue("jvm.memory.pool.name"); |
| 55 | + |
| 56 | + AttributeMatcherGroup heapPoolAttributes = |
| 57 | + attributeGroup(attribute("jvm.memory.type", "heap"), poolNameAttribute); |
| 58 | + |
| 59 | + AttributeMatcherGroup nonHeapPoolAttributes = |
| 60 | + attributeGroup(attribute("jvm.memory.type", "non_heap"), poolNameAttribute); |
| 61 | + |
| 62 | + AttributeMatcher bufferPoolName = attributeWithAnyValue("jvm.buffer.pool.name"); |
| 63 | + verifyMetrics( |
| 64 | + MetricsVerifier.create() |
| 65 | + .add( |
| 66 | + "jvm.memory.used", |
| 67 | + metric -> |
| 68 | + metric |
| 69 | + .hasDescription("Measure of memory used.") |
| 70 | + .hasUnit("By") |
| 71 | + .isUpDownCounter() |
| 72 | + .hasDataPointsWithAttributes(heapPoolAttributes, nonHeapPoolAttributes)) |
| 73 | + .add( |
| 74 | + "jvm.memory.committed", |
| 75 | + metric -> |
| 76 | + metric |
| 77 | + .hasDescription("Measure of memory committed.") |
| 78 | + .hasUnit("By") |
| 79 | + .isUpDownCounter() |
| 80 | + .hasDataPointsWithAttributes(heapPoolAttributes, nonHeapPoolAttributes)) |
| 81 | + .add( |
| 82 | + "jvm.memory.limit", |
| 83 | + metric -> |
| 84 | + metric |
| 85 | + .hasDescription("Measure of max obtainable memory.") |
| 86 | + .hasUnit("By") |
| 87 | + .isUpDownCounter() |
| 88 | + .hasDataPointsWithAttributes(heapPoolAttributes, nonHeapPoolAttributes)) |
| 89 | + .add( |
| 90 | + "jvm.memory.init", |
| 91 | + metric -> |
| 92 | + metric |
| 93 | + .hasDescription("Measure of initial memory requested.") |
| 94 | + .hasUnit("By") |
| 95 | + .isUpDownCounter() |
| 96 | + .hasDataPointsWithAttributes(heapPoolAttributes, nonHeapPoolAttributes)) |
| 97 | + .add( |
| 98 | + "jvm.memory.used_after_last_gc", |
| 99 | + metric -> |
| 100 | + metric |
| 101 | + .hasDescription( |
| 102 | + "Measure of memory used, as measured after the most recent garbage collection event on this pool.") |
| 103 | + .hasUnit("By") |
| 104 | + .isUpDownCounter() |
| 105 | + // note: there is no GC for non-heap memory |
| 106 | + .hasDataPointsWithAttributes(heapPoolAttributes)) |
| 107 | + .add( |
| 108 | + "jvm.thread.count", |
| 109 | + metric -> |
| 110 | + metric |
| 111 | + .hasDescription("Number of executing platform threads.") |
| 112 | + .hasUnit("{thread}") |
| 113 | + .isUpDownCounter() |
| 114 | + .hasDataPointsWithoutAttributes()) |
| 115 | + .add( |
| 116 | + "jvm.class.loaded", |
| 117 | + metric -> |
| 118 | + metric |
| 119 | + .hasDescription("Number of classes loaded since JVM start.") |
| 120 | + .hasUnit("{class}") |
| 121 | + .isUpDownCounter() |
| 122 | + .hasDataPointsWithoutAttributes()) |
| 123 | + .add( |
| 124 | + "jvm.class.unloaded", |
| 125 | + metric -> |
| 126 | + metric |
| 127 | + .hasDescription("Number of classes unloaded since JVM start.") |
| 128 | + .hasUnit("{class}") |
| 129 | + .isUpDownCounter() |
| 130 | + .hasDataPointsWithoutAttributes()) |
| 131 | + .add( |
| 132 | + "jvm.class.count", |
| 133 | + metric -> |
| 134 | + metric |
| 135 | + .hasDescription("Number of classes currently loaded.") |
| 136 | + .hasUnit("{class}") |
| 137 | + .isUpDownCounter() |
| 138 | + .hasDataPointsWithoutAttributes()) |
| 139 | + .add( |
| 140 | + "jvm.cpu.count", |
| 141 | + metric -> |
| 142 | + metric |
| 143 | + .hasDescription( |
| 144 | + "Number of processors available to the Java virtual machine.") |
| 145 | + .hasUnit("{cpu}") |
| 146 | + .isUpDownCounter() |
| 147 | + .hasDataPointsWithoutAttributes()) |
| 148 | + .add( |
| 149 | + "jvm.cpu.time", |
| 150 | + metric -> |
| 151 | + metric |
| 152 | + .hasDescription("CPU time used by the process as reported by the JVM.") |
| 153 | + .hasUnit("s") |
| 154 | + .isCounter() |
| 155 | + .hasDataPointsWithoutAttributes()) |
| 156 | + .add( |
| 157 | + "jvm.cpu.recent_utilization", |
| 158 | + metric -> |
| 159 | + metric |
| 160 | + .hasDescription( |
| 161 | + "Recent CPU utilization for the process as reported by the JVM.") |
| 162 | + .hasUnit("1") |
| 163 | + .isGauge() |
| 164 | + .hasDataPointsWithoutAttributes()) |
| 165 | + .add( |
| 166 | + "jvm.system.cpu.load_1m", |
| 167 | + metric -> |
| 168 | + metric |
| 169 | + .hasDescription( |
| 170 | + "Average CPU load of the whole system for the last minute as reported by the JVM.") |
| 171 | + .hasUnit("{run_queue_item}") |
| 172 | + .isGauge() |
| 173 | + .hasDataPointsWithoutAttributes()) |
| 174 | + .add( |
| 175 | + "jvm.system.cpu.utilization", |
| 176 | + metric -> |
| 177 | + metric |
| 178 | + .hasDescription( |
| 179 | + "Recent CPU utilization for the whole system as reported by the JVM.") |
| 180 | + .hasUnit("1") |
| 181 | + .isGauge() |
| 182 | + .hasDataPointsWithoutAttributes()) |
| 183 | + .add( |
| 184 | + "jvm.buffer.memory.used", |
| 185 | + metric -> |
| 186 | + metric |
| 187 | + .hasDescription("Measure of memory used by buffers.") |
| 188 | + .hasUnit("By") |
| 189 | + .isUpDownCounter() |
| 190 | + .hasDataPointsWithOneAttribute(bufferPoolName)) |
| 191 | + .add( |
| 192 | + "jvm.buffer.memory.limit", |
| 193 | + metric -> |
| 194 | + metric |
| 195 | + .hasDescription("Measure of total memory capacity of buffers.") |
| 196 | + .hasUnit("By") |
| 197 | + .isUpDownCounter() |
| 198 | + .hasDataPointsWithOneAttribute(bufferPoolName)) |
| 199 | + .add( |
| 200 | + "jvm.buffer.count", |
| 201 | + metric -> |
| 202 | + metric |
| 203 | + .hasDescription("Number of buffers in the pool.") |
| 204 | + .hasUnit("{buffer}") |
| 205 | + .isUpDownCounter() |
| 206 | + .hasDataPointsWithOneAttribute(bufferPoolName))); |
| 207 | + } |
| 208 | +} |
0 commit comments