Skip to content

Commit 20473c0

Browse files
authored
Merge pull request DataDog#1962 from DataDog/mcculls/useSystemAccessForMetricsJMX
Use SystemAccess to query JMX in health metrics
2 parents 765819a + 00dc98d commit 20473c0

5 files changed

Lines changed: 20 additions & 16 deletions

File tree

dd-trace-core/src/main/java/datadog/trace/core/monitor/CPUTimer.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package datadog.trace.core.monitor;
22

33
import com.timgroup.statsd.StatsDClient;
4-
import java.lang.management.ManagementFactory;
5-
import java.lang.management.ThreadMXBean;
4+
import datadog.trace.core.util.SystemAccess;
65

76
public class CPUTimer extends Timer {
87

9-
private final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
108
private final StatsDClient statsd;
119

1210
private final String name;
@@ -23,21 +21,25 @@ public class CPUTimer extends Timer {
2321
@Override
2422
public Recording start() {
2523
super.start();
26-
this.start = threadMXBean.getCurrentThreadCpuTime();
24+
this.start = SystemAccess.getCurrentThreadCpuTime();
2725
return this;
2826
}
2927

3028
@Override
3129
public void reset() {
32-
long cpuNanos = threadMXBean.getCurrentThreadCpuTime();
33-
this.cpuTime += (cpuNanos - start);
30+
long cpuNanos = SystemAccess.getCurrentThreadCpuTime();
31+
if (start > 0) {
32+
this.cpuTime += (cpuNanos - start);
33+
}
3434
this.start = cpuNanos;
3535
super.reset();
3636
}
3737

3838
@Override
3939
public void stop() {
40-
this.cpuTime += threadMXBean.getCurrentThreadCpuTime() - start;
40+
if (start > 0) {
41+
this.cpuTime += SystemAccess.getCurrentThreadCpuTime() - start;
42+
}
4143
super.stop();
4244
}
4345

dd-trace-core/src/main/java/datadog/trace/core/monitor/Monitoring.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package datadog.trace.core.monitor;
22

3-
import static java.lang.management.ManagementFactory.getThreadMXBean;
4-
53
import com.timgroup.statsd.NoOpStatsDClient;
64
import com.timgroup.statsd.StatsDClient;
75
import java.util.concurrent.TimeUnit;
@@ -57,10 +55,7 @@ public Recording newCPUTimer(final String name) {
5755
if (!enabled) {
5856
return NoOpRecording.NO_OP;
5957
}
60-
if (getThreadMXBean().isCurrentThreadCpuTimeSupported()) {
61-
return new CPUTimer(name, statsd, flushAfterNanos);
62-
}
63-
return newTimer(name);
58+
return new CPUTimer(name, statsd, flushAfterNanos);
6459
}
6560

6661
public Counter newCounter(final String name) {

dd-trace-core/src/main/java/datadog/trace/core/util/JmxSystemAccessProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
final class JmxSystemAccessProvider implements SystemAccessProvider {
1515
private final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
1616
private final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
17+
private final boolean cpuTimeSupported = threadMXBean.isCurrentThreadCpuTimeSupported();
1718

1819
public static final JmxSystemAccessProvider INSTANCE = new JmxSystemAccessProvider();
1920

@@ -23,7 +24,7 @@ final class JmxSystemAccessProvider implements SystemAccessProvider {
2324
*/
2425
@Override
2526
public long getThreadCpuTime() {
26-
return threadMXBean.getCurrentThreadCpuTime();
27+
return cpuTimeSupported ? threadMXBean.getCurrentThreadCpuTime() : Long.MIN_VALUE;
2728
}
2829

2930
/**

dd-trace-core/src/main/java/datadog/trace/core/util/SystemAccess.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public static void disableJmx() {
2121

2222
/** Enable JMX accesses */
2323
public static void enableJmx() {
24-
if (!Config.get().isProfilingEnabled()) {
25-
log.debug("Will not enable JMX access. Profiling is disabled.");
24+
if (!Config.get().isProfilingEnabled() && !Config.get().isHealthMetricsEnabled()) {
25+
log.debug("Will not enable JMX access. Profiling and metrics are both disabled.");
2626
return;
2727
}
2828
try {

dd-trace-core/src/test/groovy/datadog/trace/core/monitor/TimingTest.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package datadog.trace.core.monitor
22

33
import com.timgroup.statsd.StatsDClient
4+
import datadog.trace.agent.test.utils.ConfigUtils
5+
import datadog.trace.api.Config
6+
import datadog.trace.core.util.SystemAccess
47
import datadog.trace.util.test.DDSpecification
58
import org.junit.Assert
69
import org.junit.Assume
@@ -95,6 +98,7 @@ class TimingTest extends DDSpecification {
9598
Monitoring monitoring = new Monitoring(statsd, 100, MILLISECONDS)
9699
def timer = monitoring.newCPUTimer("my_timer")
97100
when:
101+
ConfigUtils.withConfigOverride("${Config.HEALTH_METRICS_ENABLED}", "true", { SystemAccess.enableJmx() })
98102
Recording recording = timer.start()
99103
Thread.sleep(200)
100104
recording.close()
@@ -105,6 +109,8 @@ class TimingTest extends DDSpecification {
105109
1 * statsd.gauge("my_timer", { it > MILLISECONDS.toNanos(200) }, { it[0] == "stat:max" && it[1].startsWith("thread:") })
106110
1 * statsd.gauge("my_timer.cpu", { it > 0 }, { it[0].startsWith("thread:") })
107111
0 * _
112+
cleanup:
113+
SystemAccess.disableJmx()
108114
}
109115
}
110116

0 commit comments

Comments
 (0)