Enable runtime metrics on agent startup.
ADD
instrumentation/runtime-metrics/javaagent/runtime-metrics-javaagent.gradle
+apply from: "$rootDir/gradle/instrumentation.gradle"
+
+dependencies {
+ api project(':javaagent-spi')
+ implementation project(':instrumentation:runtime-metrics:library')
+}

ADD
instrumentation/runtime-metrics/javaagent/src/main/java/io/instrumentation/javaagent/instrumentation/runtimemetrics/RuntimeMetricsInstaller.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.instrumentation.javaagent.instrumentation.runtimemetrics;
+
+import com.google.auto.service.AutoService;
+import io.opentelemetry.instrumentation.api.config.Config;
+import io.opentelemetry.instrumentation.runtimemetrics.GarbageCollector;
+import io.opentelemetry.instrumentation.runtimemetrics.MemoryPools;
+import io.opentelemetry.javaagent.spi.ComponentInstaller;
+import java.util.Collections;
+
+/** {@link ComponentInstaller} to enable runtime metrics during agent startup. */
+@AutoService(ComponentInstaller.class)
+public class RuntimeMetricsInstaller implements ComponentInstaller {
+ @Override
+ public void afterByteBuddyAgent() {
+ if (Config.get().isInstrumentationEnabled(Collections.singleton("runtime-metrics"), true)) {
+ GarbageCollector.registerObservers();
+ MemoryPools.registerObservers();
+ }
+ }
+}

ADD
instrumentation/runtime-metrics/javaagent/src/test/groovy/io/instrumentation/javaagent/instrumentation/runtimemetrics/RuntimeMetricsTest.groovy
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.instrumentation.javaagent.instrumentation.runtimemetrics
+
+import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
+import spock.util.concurrent.PollingConditions
+
+class RuntimeMetricsTest extends AgentInstrumentationSpecification {
+
+ def "test runtime metrics is enabled"() {
+ when:
+ def conditions = new PollingConditions(timeout: 10, initialDelay: 1.5, factor: 1.25)
+
+ then:
+ conditions.eventually {
+ assert getMetrics().any { it.name == "runtime.jvm.gc.collection" }
+ assert getMetrics().any { it.name == "runtime.jvm.memory.area" }
+ assert getMetrics().any { it.name == "runtime.jvm.memory.pool" }
+ }
+ }
+}

MODIFY
instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/GarbageCollector.java
instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/GarbageCollector.java
@@ -32,7 +32,7 @@ public final class GarbageCollector {
private static final String GC_LABEL_KEY = "gc";
/** Register all observers provided by this module. */
- public void registerObservers() {
+ public static void registerObservers() {
List<GarbageCollectorMXBean> garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans();
Meter meter = GlobalMetricsProvider.getMeter(GarbageCollector.class.getName());
List<Labels> labelSets = new ArrayList<>(garbageCollectors.size());
@@ -52,4 +52,6 @@ public final class GarbageCollector {
})
.build();
}
+
+ private GarbageCollector() {}
}

MODIFY
instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/MemoryPools.java
instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/MemoryPools.java
@@ -137,4 +137,6 @@ public final class MemoryPools {
observer.observe(usage.getMax(), maxLabels);
}
}
+
+ private MemoryPools() {}
}

MODIFY
settings.gradle
settings.gradle
@@ -194,6 +194,7 @@ include ':instrumentation:reactor-netty-1.0:javaagent'
include ':instrumentation:rediscala-1.8:javaagent'
include ':instrumentation:redisson-3.0:javaagent'
include ':instrumentation:rmi:javaagent'
+include ':instrumentation:runtime-metrics:javaagent'
include ':instrumentation:runtime-metrics:library'
include ':instrumentation:rxjava-1.0:library'
include ':instrumentation:rxjava-2.0:library'

Enable runtime metrics on agent startup. (#2311)