File tree Expand file tree Collapse file tree
main/java/datadog/trace/core
test/groovy/datadog/trace/core/monitor Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package datadog .trace .core .monitor ;
22
33import com .timgroup .statsd .StatsDClient ;
4- import java .lang .management .ManagementFactory ;
5- import java .lang .management .ThreadMXBean ;
4+ import datadog .trace .core .util .SystemAccess ;
65
76public 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
Original file line number Diff line number Diff line change 11package datadog .trace .core .monitor ;
22
3- import static java .lang .management .ManagementFactory .getThreadMXBean ;
4-
53import com .timgroup .statsd .NoOpStatsDClient ;
64import com .timgroup .statsd .StatsDClient ;
75import 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 ) {
Original file line number Diff line number Diff line change 1414final 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 /**
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff line change 11package datadog.trace.core.monitor
22
33import 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
47import datadog.trace.util.test.DDSpecification
58import org.junit.Assert
69import 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
You can’t perform that action at this time.
0 commit comments