Skip to content

Commit c3afca4

Browse files
authored
Merge pull request #1173 from ligangty/metrics
Add system level gauges for metrics
2 parents 2055952 + f7e2e5f commit c3afca4

5 files changed

Lines changed: 155 additions & 3 deletions

File tree

addons/promote/common/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@
6161
<groupId>org.commonjava.atlas</groupId>
6262
<artifactId>atlas-identities</artifactId>
6363
</dependency>
64+
<dependency>
65+
<groupId>org.commonjava.indy</groupId>
66+
<artifactId>indy-subsys-metrics-reporter</artifactId>
67+
</dependency>
6468
</dependencies>
6569

6670
<build>

addons/promote/common/src/main/java/org/commonjava/indy/promote/data/PromotionManager.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.commonjava.indy.data.IndyDataException;
3131
import org.commonjava.indy.data.StoreDataManager;
3232
import org.commonjava.indy.measure.annotation.Measure;
33+
import org.commonjava.indy.metrics.IndyMetricsManager;
3334
import org.commonjava.indy.model.core.ArtifactStore;
3435
import org.commonjava.indy.model.core.Group;
3536
import org.commonjava.indy.model.core.HostedRepository;
@@ -64,6 +65,7 @@
6465
import java.io.InputStream;
6566
import java.util.ArrayList;
6667
import java.util.Collections;
68+
import java.util.HashMap;
6769
import java.util.HashSet;
6870
import java.util.List;
6971
import java.util.Map;
@@ -93,6 +95,9 @@ public class PromotionManager
9395

9496
private final Logger logger = LoggerFactory.getLogger( getClass() );
9597

98+
@Inject
99+
private IndyMetricsManager metricsManager;
100+
96101
@Inject
97102
private PromoteConfig config;
98103

@@ -511,8 +516,10 @@ private PathsPromoteResult doPathsPromotion( PathsPromoteRequest request, String
511516
}
512517
else if ( validation.isValid() )
513518
{
514-
return runPathPromotions( request, pending, Collections.emptySet(), Collections.emptySet(), contents,
515-
validation, validationRequest );
519+
PathsPromoteResult result = runPathPromotions( request, pending, Collections.emptySet(), Collections.emptySet(), contents,
520+
validation, validationRequest );
521+
doPathPromoteMetrics( contents.size(), result );
522+
return result;
516523
}
517524
else
518525
{
@@ -521,6 +528,22 @@ else if ( validation.isValid() )
521528
}
522529
}
523530

531+
private void doPathPromoteMetrics( int total, PathsPromoteResult result )
532+
{
533+
try
534+
{
535+
Map<String, Integer> pathPromoteMetrics = new HashMap<>();
536+
pathPromoteMetrics.put( "total", total );
537+
pathPromoteMetrics.put( "complete", result.getCompletedPaths().size() );
538+
pathPromoteMetrics.put( "skipped", result.getSkippedPaths().size() );
539+
metricsManager.addGauges( this.getClass(), "path.promote", pathPromoteMetrics );
540+
}
541+
catch ( Throwable e )
542+
{
543+
logger.warn( "Failed to get path promote files metrics. Reason: {} ", e.getMessage() );
544+
}
545+
}
546+
524547
private Future<PathsPromoteResult> submitPathsPromoteRequest( PathsPromoteRequest request, final String baseUrl )
525548
throws IndyWorkflowException
526549
{
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
import java.lang.management.ManagementFactory;
25+
import java.lang.management.OperatingSystemMXBean;
26+
import java.util.Collections;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
/**
31+
* A set of gauges for system gauges, including stats on cpu, process, physical mem, swap mem.
32+
*/
33+
public class SystemGaugesSet
34+
implements MetricSet
35+
{
36+
private final Logger logger = LoggerFactory.getLogger( this.getClass() );
37+
38+
private final OperatingSystemMXBean operatingSystemMXBean;
39+
40+
public SystemGaugesSet()
41+
{
42+
this( ManagementFactory.getOperatingSystemMXBean() );
43+
}
44+
45+
public SystemGaugesSet( OperatingSystemMXBean mxBean )
46+
{
47+
this.operatingSystemMXBean = mxBean;
48+
}
49+
50+
public Map<String, Metric> getMetrics()
51+
{
52+
if ( !( operatingSystemMXBean instanceof com.sun.management.OperatingSystemMXBean ) )
53+
{
54+
return Collections.emptyMap();
55+
}
56+
57+
final com.sun.management.OperatingSystemMXBean osMxBean =
58+
(com.sun.management.OperatingSystemMXBean) operatingSystemMXBean;
59+
60+
final Map<String, Metric> gauges = new HashMap<>();
61+
62+
try
63+
{
64+
gauges.put( "process.cpu.load", (Gauge<Double>) osMxBean::getProcessCpuLoad );
65+
gauges.put( "system.cpu.load", (Gauge<Double>) osMxBean::getSystemCpuLoad );
66+
gauges.put( "system.load.avg", (Gauge<Double>) osMxBean::getSystemLoadAverage );
67+
gauges.put( "process.cpu.time.ms", (Gauge<Long>) () -> osMxBean.getProcessCpuTime() * 1000 );
68+
69+
gauges.put( "mem.total.swap", (Gauge<Long>) osMxBean::getTotalSwapSpaceSize );
70+
gauges.put( "mem.total.physical", (Gauge<Long>) osMxBean::getTotalPhysicalMemorySize );
71+
gauges.put( "mem.free.physical", (Gauge<Long>) osMxBean::getFreePhysicalMemorySize );
72+
gauges.put( "mem.free.swap", (Gauge<Long>) osMxBean::getFreeSwapSpaceSize );
73+
}
74+
catch ( Throwable e )
75+
{
76+
logger.warn( "Can not get system level metrics. Reason: {}", e.getMessage() );
77+
}
78+
79+
return Collections.unmodifiableMap( gauges );
80+
81+
}
82+
83+
}
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: 12 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;
@@ -42,11 +41,13 @@
4241

4342
import static com.codahale.metrics.MetricRegistry.name;
4443
import static org.apache.commons.lang.StringUtils.isNotBlank;
44+
import static org.commonjava.indy.metrics.IndyMetricsConstants.DEFAULT;
4545
import static org.commonjava.indy.metrics.IndyMetricsConstants.EXCEPTION;
4646
import static org.commonjava.indy.metrics.IndyMetricsConstants.METER;
4747
import static org.commonjava.indy.metrics.IndyMetricsConstants.SKIP_METRIC;
4848
import static org.commonjava.indy.metrics.IndyMetricsConstants.TIMER;
4949
import static org.commonjava.indy.metrics.jvm.IndyJVMInstrumentation.registerJvmMetric;
50+
import static org.commonjava.indy.metrics.system.SystemInstrumentation.registerSystemMetric;
5051
import static org.commonjava.indy.model.core.StoreType.remote;
5152
import static org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor.MAVEN_PKG_KEY;
5253

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

100101
logger.info( "Init metrics subsystem..." );
101102

103+
registerSystemMetric( config.getNodePrefix(), metricRegistry );
102104
registerJvmMetric( config.getNodePrefix(), metricRegistry );
103105

104106
// Health checks
@@ -251,4 +253,13 @@ public <T> T wrapWithStandardMetrics( final Supplier<T> method, final Supplier<S
251253
}
252254
}
253255

256+
public <T> void addGauges( Class<?> className, String method, Map<String, T> gauges )
257+
{
258+
String defaultName = IndyMetricsConstants.getDefaultName( className, method );
259+
gauges.forEach( ( k, t ) -> {
260+
String name = IndyMetricsConstants.getName( config.getNodePrefix(), DEFAULT, defaultName, k );
261+
metricRegistry.gauge( name, () -> () -> t );
262+
} );
263+
}
264+
254265
}

0 commit comments

Comments
 (0)