
cn.patterncat.metrics.jvm.ThreadMetricSet Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2016 Centro, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package cn.patterncat.metrics.jvm;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricSet;
import com.codahale.metrics.jvm.ThreadDeadlockDetector;
import com.google.common.base.CaseFormat;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* 从/com/codahale/metrics/jvm/ThreadStatesGaugeSet.java拷贝过来
* 改了几个metrics的名字
*/
public class ThreadMetricSet implements MetricSet {
private final static int STACK_TRACE_DEPTH = 0;
private ThreadMXBean threadMXBean;
private ThreadDeadlockDetector deadlockDetector;
private Gauge currentThreadsGauge;
private Gauge peakThreadsGauge;
private Gauge daemonThreadsGauge;
private Gauge deadlockedThreadsGauge;
private Map> threadsGaugesByThreadStates;
private Map metricsByNames;
ThreadMetricSet() {
this.threadMXBean = ManagementFactory.getThreadMXBean();
this.deadlockDetector = new ThreadDeadlockDetector();
Map metricsByNames = new HashMap<>();
this.currentThreadsGauge = new Gauge() {
@Override
public Integer getValue() {
return threadMXBean.getThreadCount();
}
};
metricsByNames.put("currentCount", currentThreadsGauge);
this.peakThreadsGauge = new Gauge() {
@Override
public Integer getValue() {
return threadMXBean.getPeakThreadCount();
}
};
metricsByNames.put("peakCount", peakThreadsGauge);
this.daemonThreadsGauge = new Gauge() {
@Override
public Integer getValue() {
return threadMXBean.getDaemonThreadCount();
}
};
metricsByNames.put("daemonCount", daemonThreadsGauge);
this.deadlockedThreadsGauge = new Gauge() {
@Override
public Integer getValue() {
return deadlockDetector.getDeadlockedThreads().size();
}
};
metricsByNames.put("deadlockCount", deadlockedThreadsGauge);
Map> threadsGaugesByThreadStates = new HashMap<>();
for (final Thread.State state : Thread.State.values()) {
String metricName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, state.toString());
Gauge threadsGauge = new Gauge() {
@Override
public Integer getValue() {
return getThreadCount(state);
}
};
threadsGaugesByThreadStates.put(state, threadsGauge);
metricsByNames.put(metricName, threadsGauge);
}
this.threadsGaugesByThreadStates = threadsGaugesByThreadStates;
this.metricsByNames = metricsByNames;
}
@Override
public Map getMetrics() {
return Collections.unmodifiableMap(metricsByNames);
}
Gauge getDeadlockedThreadsGauge() {
return deadlockedThreadsGauge;
}
Gauge getDaemonThreadsGauge() {
return daemonThreadsGauge;
}
Gauge getPeakThreadsGauge() {
return peakThreadsGauge;
}
Gauge getCurrentThreadsGauge() {
return currentThreadsGauge;
}
Map> getThreadsGaugesByThreadStates() {
return Collections.unmodifiableMap(threadsGaugesByThreadStates);
}
private int getThreadCount(Thread.State state) {
final ThreadInfo[] allThreads = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), STACK_TRACE_DEPTH);
int count = 0;
for (ThreadInfo info : allThreads) {
if (info != null && info.getThreadState() == state) {
count++;
}
}
return count;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy