Skip to content

Commit 9d40db7

Browse files
committed
Add system level gauges for metrics
1 parent 2055952 commit 9d40db7

3 files changed

Lines changed: 105 additions & 1 deletion

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright (C) 2013~2019 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.indy.metrics.system;
17+
18+
import com.codahale.metrics.Gauge;
19+
import com.codahale.metrics.Metric;
20+
import com.codahale.metrics.MetricSet;
21+
22+
import java.lang.management.ManagementFactory;
23+
import java.lang.management.OperatingSystemMXBean;
24+
import java.util.Collections;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
/**
29+
* A set of gauges for system gauges, including stats on cpu, process, physical mem, swap mem.
30+
*/
31+
public class SystemGaugesSet
32+
implements MetricSet
33+
{
34+
private final OperatingSystemMXBean operatingSystemMXBean;
35+
36+
public SystemGaugesSet()
37+
{
38+
this( ManagementFactory.getOperatingSystemMXBean() );
39+
}
40+
41+
public SystemGaugesSet( OperatingSystemMXBean mxBean )
42+
{
43+
this.operatingSystemMXBean = mxBean;
44+
}
45+
46+
public Map<String, Metric> getMetrics()
47+
{
48+
if ( !( operatingSystemMXBean instanceof com.sun.management.OperatingSystemMXBean ) )
49+
{
50+
return Collections.emptyMap();
51+
}
52+
53+
final com.sun.management.OperatingSystemMXBean osMxBean =
54+
(com.sun.management.OperatingSystemMXBean) operatingSystemMXBean;
55+
56+
final Map<String, Metric> gauges = new HashMap<>();
57+
58+
gauges.put( "process.cpu.load", (Gauge<Double>) osMxBean::getProcessCpuLoad );
59+
gauges.put( "system.cpu.load", (Gauge<Double>) osMxBean::getSystemCpuLoad );
60+
gauges.put( "system.load.avg", (Gauge<Double>) osMxBean::getSystemLoadAverage );
61+
gauges.put( "process.cpu.time.ms", (Gauge<Long>) () -> osMxBean.getProcessCpuTime() * 1000 );
62+
63+
gauges.put( "mem.total.swap", (Gauge<Long>) osMxBean::getTotalSwapSpaceSize );
64+
gauges.put( "mem.total.physical", (Gauge<Long>) osMxBean::getTotalPhysicalMemorySize );
65+
gauges.put( "mem.free.physical", (Gauge<Long>) osMxBean::getFreePhysicalMemorySize );
66+
gauges.put( "mem.free.swap", (Gauge<Long>) osMxBean::getFreeSwapSpaceSize );
67+
68+
return Collections.unmodifiableMap( gauges );
69+
70+
}
71+
72+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (C) 2013~2019 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.indy.metrics.system;
17+
18+
import com.codahale.metrics.MetricRegistry;
19+
20+
import static com.codahale.metrics.MetricRegistry.name;
21+
22+
public class SystemInstrumentation
23+
{
24+
private static final String SYSTEM = "system";
25+
26+
public static void registerSystemMetric( String nodePrefix, MetricRegistry registry )
27+
{
28+
registry.register( name( nodePrefix, SYSTEM ), new SystemGaugesSet() );
29+
30+
}
31+
}

subsys/metrics/reporter/src/main/java/org/commonjava/indy/metrics/IndyMetricsManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import javax.annotation.PostConstruct;
3333
import javax.enterprise.context.ApplicationScoped;
34-
import javax.enterprise.inject.Any;
3534
import javax.enterprise.inject.Instance;
3635
import javax.enterprise.inject.Produces;
3736
import javax.inject.Inject;
@@ -47,6 +46,7 @@
4746
import static org.commonjava.indy.metrics.IndyMetricsConstants.SKIP_METRIC;
4847
import static org.commonjava.indy.metrics.IndyMetricsConstants.TIMER;
4948
import static org.commonjava.indy.metrics.jvm.IndyJVMInstrumentation.registerJvmMetric;
49+
import static org.commonjava.indy.metrics.system.SystemInstrumentation.registerSystemMetric;
5050
import static org.commonjava.indy.model.core.StoreType.remote;
5151
import static org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor.MAVEN_PKG_KEY;
5252

@@ -99,6 +99,7 @@ public void init()
9999

100100
logger.info( "Init metrics subsystem..." );
101101

102+
registerSystemMetric( config.getNodePrefix(), metricRegistry );
102103
registerJvmMetric( config.getNodePrefix(), metricRegistry );
103104

104105
// Health checks

0 commit comments

Comments
 (0)