|
| 1 | +package opentelemetry147.metrics; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import datadog.trace.agent.jmxfetch.JvmOtlpRuntimeMetrics; |
| 8 | +import datadog.trace.bootstrap.otel.metrics.data.OtelMetricRegistry; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.TreeSet; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +// Forked test: runs in an isolated JVM and starts JvmOtlpRuntimeMetrics with the experimental |
| 18 | +// flag OFF, verifying that Development-status instruments are not registered and that the |
| 19 | +// jvm.gc.cause attribute is omitted from jvm.gc.duration data points. The JvmOtlpRuntimeMetrics |
| 20 | +// class uses a one-shot AtomicBoolean to guard registration, so this scenario must run in its |
| 21 | +// own JVM separate from the always-on JvmOtlpRuntimeMetricsTest. |
| 22 | +class JvmOtlpRuntimeMetricsForkedTest { |
| 23 | + |
| 24 | + @BeforeAll |
| 25 | + static void setUp() { |
| 26 | + System.setProperty("dd.metrics.otel.enabled", "true"); |
| 27 | + JvmOtlpRuntimeMetrics.start(false); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void registersOnlyStableMetricsWhenExperimentalDisabled() { |
| 32 | + JvmOtlpRuntimeMetricsTest.MetricCollector collector = |
| 33 | + new JvmOtlpRuntimeMetricsTest.MetricCollector(); |
| 34 | + OtelMetricRegistry.INSTANCE.collectMetrics(collector); |
| 35 | + |
| 36 | + Set<String> names = collector.metricNames; |
| 37 | + |
| 38 | + List<String> expectedStableMetrics = |
| 39 | + Arrays.asList( |
| 40 | + "jvm.memory.used", |
| 41 | + "jvm.memory.committed", |
| 42 | + "jvm.memory.limit", |
| 43 | + "jvm.memory.used_after_last_gc", |
| 44 | + "jvm.thread.count", |
| 45 | + "jvm.class.loaded", |
| 46 | + "jvm.class.count", |
| 47 | + "jvm.class.unloaded", |
| 48 | + "jvm.cpu.time", |
| 49 | + "jvm.cpu.count", |
| 50 | + "jvm.cpu.recent_utilization", |
| 51 | + "jvm.gc.duration"); |
| 52 | + for (String metric : expectedStableMetrics) { |
| 53 | + assertTrue( |
| 54 | + names.contains(metric), |
| 55 | + "Expected stable metric '" + metric + "' not found. Got: " + new TreeSet<>(names)); |
| 56 | + } |
| 57 | + |
| 58 | + List<String> developmentMetrics = |
| 59 | + Arrays.asList( |
| 60 | + "jvm.memory.init", |
| 61 | + "jvm.buffer.memory.used", |
| 62 | + "jvm.buffer.memory.limit", |
| 63 | + "jvm.buffer.count", |
| 64 | + "jvm.system.cpu.utilization", |
| 65 | + "jvm.system.cpu.load_1m", |
| 66 | + "jvm.file_descriptor.count", |
| 67 | + "jvm.file_descriptor.limit"); |
| 68 | + for (String metric : developmentMetrics) { |
| 69 | + assertFalse( |
| 70 | + names.contains(metric), |
| 71 | + "Development metric '" |
| 72 | + + metric |
| 73 | + + "' should not be registered when experimental disabled. Got: " |
| 74 | + + new TreeSet<>(names)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void jvmGcDurationDataPointsOmitGcCauseWhenExperimentalDisabled() throws InterruptedException { |
| 80 | + System.gc(); |
| 81 | + |
| 82 | + List<JvmOtlpRuntimeMetricsTest.DataPointEntry> points = null; |
| 83 | + long deadlineNanos = System.nanoTime() + TimeUnit.SECONDS.toNanos(2); |
| 84 | + while (System.nanoTime() < deadlineNanos) { |
| 85 | + JvmOtlpRuntimeMetricsTest.MetricCollector collector = |
| 86 | + new JvmOtlpRuntimeMetricsTest.MetricCollector(); |
| 87 | + OtelMetricRegistry.INSTANCE.collectMetrics(collector); |
| 88 | + points = collector.points.get("jvm.gc.duration"); |
| 89 | + if (points != null && !points.isEmpty()) { |
| 90 | + break; |
| 91 | + } |
| 92 | + Thread.sleep(50); |
| 93 | + } |
| 94 | + |
| 95 | + assertNotNull(points, "jvm.gc.duration should have data points after System.gc()"); |
| 96 | + assertFalse(points.isEmpty(), "jvm.gc.duration should have at least one data point"); |
| 97 | + assertTrue( |
| 98 | + points.stream() |
| 99 | + .allMatch( |
| 100 | + p -> |
| 101 | + p.attrs.containsKey("jvm.gc.name") |
| 102 | + && p.attrs.containsKey("jvm.gc.action") |
| 103 | + && !p.attrs.containsKey("jvm.gc.cause")), |
| 104 | + "jvm.gc.duration data points must carry jvm.gc.name and jvm.gc.action, but not jvm.gc.cause" |
| 105 | + + " when experimental disabled"); |
| 106 | + } |
| 107 | +} |
0 commit comments