|
@@ -5,23 +5,18 @@ import java.util.Collections;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
|
|
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
|
|
import org.springframework.boot.actuate.endpoint.annotation.Selector;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
|
-import com.codahale.metrics.ConsoleReporter;
|
|
|
-import com.google.common.collect.Lists;
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
|
-import cn.com.qmth.examcloud.commons.util.Calculator;
|
|
|
import cn.com.qmth.examcloud.commons.util.FreeMarkerUtil;
|
|
|
import cn.com.qmth.examcloud.commons.util.ResourceLoader;
|
|
|
-import cn.com.qmth.examcloud.web.support.ApiInfo;
|
|
|
-import cn.com.qmth.examcloud.web.support.ApiInfoHolder;
|
|
|
|
|
|
/**
|
|
|
* 接口状态
|
|
@@ -34,6 +29,9 @@ import cn.com.qmth.examcloud.web.support.ApiInfoHolder;
|
|
|
@Endpoint(id = "api-status")
|
|
|
public class ApiStatusEndpoint {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ ApiStatusInfoCollector apiStatusInfoCollector;
|
|
|
+
|
|
|
/**
|
|
|
* url: /actuator/api-status
|
|
|
*
|
|
@@ -45,10 +43,6 @@ public class ApiStatusEndpoint {
|
|
|
return get("");
|
|
|
}
|
|
|
|
|
|
- private static ConsoleReporter consoleReporter = ConsoleReporter
|
|
|
- .forRegistry(MetricRegistryHolder.getDefalut()).convertRatesTo(TimeUnit.SECONDS)
|
|
|
- .convertDurationsTo(TimeUnit.MILLISECONDS).build();
|
|
|
-
|
|
|
/**
|
|
|
* url: /actuator/api-status/ignored?order=XX
|
|
|
*
|
|
@@ -61,81 +55,10 @@ public class ApiStatusEndpoint {
|
|
|
|
|
|
System.out.println("order by " + order);
|
|
|
ReportorHolder.getApiDataReportor().report();
|
|
|
- consoleReporter.report();
|
|
|
List<ReportInfo> reportInfoList = ReportorHolder.getApiDataReportor().getReportInfoList();
|
|
|
|
|
|
ReportInfo reportInfo = reportInfoList.get(0);
|
|
|
- List<HistogramInfo> histogramInfoList = reportInfo.getHistogramInfoList();
|
|
|
- List<MeterInfo> meterInfoList = reportInfo.getMeterInfoList();
|
|
|
-
|
|
|
- Map<String, MeterInfo> apiErrMeterInfoMap = Maps.newHashMap();
|
|
|
- for (MeterInfo meterInfo : meterInfoList) {
|
|
|
- String key = meterInfo.getKey();
|
|
|
- if (key.startsWith(MetricNames.API_ERROR_METER.name())) {
|
|
|
- String mapping = key.substring(MetricNames.API_ERROR_METER.name().length() + 1);
|
|
|
- apiErrMeterInfoMap.put(mapping, meterInfo);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Map<String, HistogramInfo> apiHistogramInfoMap = Maps.newHashMap();
|
|
|
- for (HistogramInfo histogramInfo : histogramInfoList) {
|
|
|
- String key = histogramInfo.getKey();
|
|
|
- if (key.startsWith(MetricNames.API_HISTOGRAM.name())) {
|
|
|
- String mapping = key.substring(MetricNames.API_HISTOGRAM.name().length() + 1);
|
|
|
- apiHistogramInfoMap.put(mapping, histogramInfo);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- List<ApiStatusInfo> list = Lists.newArrayList();
|
|
|
-
|
|
|
- for (MeterInfo cur : meterInfoList) {
|
|
|
- String key = cur.getKey();
|
|
|
- if (key.startsWith(MetricNames.API_METER.name())) {
|
|
|
-
|
|
|
- String mapping = key.substring(MetricNames.API_METER.name().length() + 1);
|
|
|
-
|
|
|
- ApiStatusInfo apiStatusInfo = new ApiStatusInfo();
|
|
|
- list.add(apiStatusInfo);
|
|
|
-
|
|
|
- apiStatusInfo.setMapping(mapping);
|
|
|
-
|
|
|
- ApiInfo apiInfo = ApiInfoHolder.getApiInfo(mapping);
|
|
|
- if (null != apiInfo) {
|
|
|
- apiStatusInfo.setDescription(apiInfo.getDescription());
|
|
|
- }
|
|
|
-
|
|
|
- apiStatusInfo.setCount(cur.getCount());
|
|
|
- apiStatusInfo.setMeanRate(cur.getMeanRate());
|
|
|
- apiStatusInfo.setOneMinuteRate(cur.getOneMinuteRate());
|
|
|
- apiStatusInfo.setFiveMinuteRate(cur.getFiveMinuteRate());
|
|
|
- apiStatusInfo.setFifteenMinuteRate(cur.getFifteenMinuteRate());
|
|
|
-
|
|
|
- HistogramInfo histogramInfo = apiHistogramInfoMap.get(mapping);
|
|
|
-
|
|
|
- apiStatusInfo.setMin(histogramInfo.getMin());
|
|
|
- apiStatusInfo.setMax(histogramInfo.getMax());
|
|
|
- apiStatusInfo.setMean(histogramInfo.getMean());
|
|
|
-
|
|
|
- apiStatusInfo.setP50(histogramInfo.getP50());
|
|
|
- apiStatusInfo.setP75(histogramInfo.getP75());
|
|
|
- apiStatusInfo.setP95(histogramInfo.getP95());
|
|
|
- apiStatusInfo.setP98(histogramInfo.getP98());
|
|
|
- apiStatusInfo.setP99(histogramInfo.getP99());
|
|
|
-
|
|
|
- MeterInfo apiErrMeterInfo = apiErrMeterInfoMap.get(mapping);
|
|
|
- if (null == apiErrMeterInfo) {
|
|
|
- apiStatusInfo.setErrorCount(0L);
|
|
|
- apiStatusInfo.setErrorMeanRate(0L);
|
|
|
- apiStatusInfo.setErrorPercent(0D);
|
|
|
- } else {
|
|
|
- apiStatusInfo.setErrorCount(apiErrMeterInfo.getCount());
|
|
|
- apiStatusInfo.setErrorMeanRate(apiErrMeterInfo.getMeanRate());
|
|
|
- apiStatusInfo.setErrorPercent(
|
|
|
- getPercent(apiErrMeterInfo.getMeanRate(), cur.getMeanRate()));
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
+ List<ApiStatusInfo> list = apiStatusInfoCollector.refresh(reportInfo);
|
|
|
|
|
|
Collections.sort(list, new Comparator<ApiStatusInfo>() {
|
|
|
@Override
|
|
@@ -171,12 +94,4 @@ public class ApiStatusEndpoint {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
- private Double getPercent(double v1, double v2) {
|
|
|
- if (0 == v1 || 0 == v2) {
|
|
|
- return 0d;
|
|
|
- }
|
|
|
- double ret = Calculator.divide(v1, v2, 3);
|
|
|
- return ret;
|
|
|
- }
|
|
|
-
|
|
|
}
|