Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public class DashboardConfig {
*/
public static final String CONFIG_AUTO_REMOVE_MACHINE_MILLIS = "sentinel.dashboard.autoRemoveMachineMillis";

/**
* The ttl of metric data in the Memory
* default 1 day
*/
public static final String MAX_METRIC_LIVE_TIME_MS_OF_FIVE_MINUTES = "sentinel.dashboard.store.maxMetricLiveTimeOfFiveMinutes";

private static final ConcurrentMap<String, Object> cacheMap = new ConcurrentHashMap<>();

@NonNull
Expand Down Expand Up @@ -125,7 +131,13 @@ public static int getHideAppNoMachineMillis() {
public static int getRemoveAppNoMachineMillis() {
return getConfigInt(CONFIG_REMOVE_APP_NO_MACHINE_MILLIS, 0, 120000);
}



public static int getMaxMetricLiveTimeMsOfFiveMinutes() {
return getConfigInt(MAX_METRIC_LIVE_TIME_MS_OF_FIVE_MINUTES, 86400000, 300000);
}


public static int getAutoRemoveMachineMillis() {
return getConfigInt(CONFIG_AUTO_REMOVE_MACHINE_MILLIS, 0, 300000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
*/
package com.alibaba.csp.sentinel.dashboard.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.csp.sentinel.dashboard.domain.Result;
import com.alibaba.csp.sentinel.dashboard.repository.metric.MetricsExtRepository;
import com.alibaba.csp.sentinel.dashboard.repository.metric.MetricsRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -48,18 +43,23 @@ public class MetricController {

private static Logger logger = LoggerFactory.getLogger(MetricController.class);

private static final long maxQueryIntervalMs = 1000 * 60 * 60;
private static final long minute1 = 1000 * 60;
private static final long minute5 = minute1 * 5;
private static final long hour1 = minute1 * 60;
private static final long day1 = hour1 * 24;
private static final long maxQueryIntervalMs = hour1;

@Autowired
private MetricsRepository<MetricEntity> metricStore;
private MetricsExtRepository<MetricEntity, Integer> metricStore;

@ResponseBody
@RequestMapping("/queryTopResourceMetric.json")
public Result<?> queryTopResourceMetric(final String app,
Integer pageIndex,
Integer pageSize,
Boolean desc,
Long startTime, Long endTime, String searchKey) {
Long startTime, Long endTime,
Integer cycle, String searchKey) {
if (StringUtil.isEmpty(app)) {
return Result.ofFail(-1, "app can't be null or empty");
}
Expand All @@ -75,15 +75,19 @@ public Result<?> queryTopResourceMetric(final String app,
if (desc == null) {
desc = true;
}
if (cycle == null || cycle <= 0) {
cycle = 1;
}
if (endTime == null) {
endTime = System.currentTimeMillis();
}
if (startTime == null) {
startTime = endTime - 1000 * 60 * 5;
if(startTime == null) {
startTime = endTime - ( cycle == 1 ? minute5 : day1);
}
if (endTime - startTime > maxQueryIntervalMs) {
if (cycle == 1 && endTime - startTime > maxQueryIntervalMs) {
return Result.ofFail(-1, "time intervalMs is too big, must <= 1h");
}

List<String> resources = metricStore.listResourcesOfApp(app);
logger.debug("queryTopResourceMetric(), resources.size()={}", resources.size());

Expand Down Expand Up @@ -113,7 +117,7 @@ public Result<?> queryTopResourceMetric(final String app,
long time = System.currentTimeMillis();
for (final String resource : topResource) {
List<MetricEntity> entities = metricStore.queryByAppAndResourceBetween(
app, resource, startTime, endTime);
app, resource, startTime, endTime, cycle);
logger.debug("resource={}, entities.size()={}", resource, entities == null ? "null" : entities.size());
List<MetricVo> vos = MetricVo.fromMetricEntities(entities, resource);
Iterable<MetricVo> vosSorted = sortMetricVoAndDistinct(vos);
Expand All @@ -135,6 +139,108 @@ public Result<?> queryTopResourceMetric(final String app,
return Result.ofSuccess(resultMap);
}


/**
*
* @param app
* @param pageIndex
* @param pageSize
* @param desc
* @param timeRange minutes of search range, 0 means all
* @param summaryOrderBy
* @param startTime
* @param endTime
* @param searchKey
* @return
*/
@ResponseBody
@RequestMapping("/queryResourceSummary.json")
public Result<?> queryResourceSummary(final String app,
Integer pageIndex,
Integer pageSize,
Boolean desc,
Integer timeRange,
Integer summaryOrderBy,
Long startTime, Long endTime,
String searchKey) {
if (StringUtil.isEmpty(app)) {
return Result.ofFail(-1, "app can't be null or empty");
}
if (pageIndex == null || pageIndex <= 0) {
pageIndex = 1;
}
if (pageSize == null) {
pageSize = 6;
}
if (pageSize >= 20) {
pageSize = 20;
}
if (desc == null) {
desc = true;
}
if (endTime == null) {
endTime = System.currentTimeMillis();
}
if (Objects.isNull(timeRange) || timeRange <= 0) {
startTime = 0L;
} else {
startTime = endTime - (minute1 * timeRange);
}

int resourcesTimeLimit = timeRange == 0 ? Integer.MAX_VALUE : (int) (timeRange * minute1);
List<String> resources = metricStore.listResourcesOfApp(app, resourcesTimeLimit);
logger.debug("queryResourceSummary(), resources.size()={}", resources.size());

if (resources == null || resources.isEmpty()) {
return Result.ofSuccess(null);
}
if (!desc) {
Collections.reverse(resources);
}
if (StringUtil.isNotEmpty(searchKey)) {
List<String> searched = new ArrayList<>();
for (String resource : resources) {
if (resource.contains(searchKey)) {
searched.add(resource);
}
}
resources = searched;
}
int totalPage = (resources.size() + pageSize - 1) / pageSize;
List<String> topResource = new ArrayList<>();
if (pageIndex <= totalPage) {
topResource = resources.subList((pageIndex - 1) * pageSize,
Math.min(pageIndex * pageSize, resources.size()));
}
ArrayList<MetricVo> resourceArray = new ArrayList<>();
logger.debug("topResource={}", topResource);
long time = System.currentTimeMillis();
for (final String resource : topResource) {
List<MetricEntity> entities = metricStore.queryByAppAndResourceBetween(
app, resource, startTime, endTime, 300);
logger.debug("resource={}, entities.size()={}", resource, entities == null ? "null" : entities.size());
if(entities.size() == 0) {
continue;
}

MetricEntity metricEntity = new MetricEntity();
entities.forEach(entity -> metricEntity.merge(entity));
resourceArray.add(MetricVo.fromMetricEntity(metricEntity));
}

Iterable<MetricVo> vosSorted = sortMetricVo(resourceArray, summaryOrderBy);
logger.debug("queryResourceSummary() total query time={} ms", System.currentTimeMillis() - time);
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("totalCount", resources.size());
resultMap.put("totalPage", totalPage);
resultMap.put("pageIndex", pageIndex);
resultMap.put("pageSize", pageSize);

resultMap.put("metric", vosSorted);
return Result.ofSuccess(resultMap);
}


@ResponseBody
@RequestMapping("/queryByAppAndResource.json")
public Result<?> queryByAppAndResource(String app, String identity, Long startTime, Long endTime) {
Expand Down Expand Up @@ -172,4 +278,35 @@ private Iterable<MetricVo> sortMetricVoAndDistinct(List<MetricVo> vos) {
}
return map.values();
}

/**
*
* @param resourceArray
* @param summaryOrderBy 1通过qps升序, 2通过qps降序,
* 3拒绝QPS升序, 4拒绝QPS降序,
* 5异常QPS升序, 6异常QPS降序
* @return
*/
private Iterable<MetricVo> sortMetricVo(ArrayList<MetricVo> resourceArray, Integer summaryOrderBy) {

if(Objects.isNull(summaryOrderBy) ) {
return resourceArray;
}
resourceArray.sort((v1, v2) -> {
if(summaryOrderBy == 1 || summaryOrderBy == 2) {
return (int) (v1.getPassQps() - v2.getPassQps());
} else if(summaryOrderBy == 3 || summaryOrderBy == 4) {
return (int) (v1.getBlockQps() - v2.getBlockQps());
} else {
return (int) (v1.getExceptionQps() - v2.getExceptionQps());
}
});

// 偶数都为倒序
if(summaryOrderBy % 2 == 0) {
Collections.reverse(resourceArray);
}
return resourceArray;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.alibaba.csp.sentinel.dashboard.datasource.entity;

import java.util.Date;
import java.util.Objects;

/**
* @author leyou
Expand Down Expand Up @@ -216,4 +217,31 @@ public String toString() {
'}';
}

public void merge(MetricEntity entity) {
this.app = entity.app;
this.gmtCreate = entity.gmtCreate;
this.gmtModified = entity.gmtModified;
this.resource = entity.resource;
this.resourceCode = entity.resourceCode;
this.count += entity.count;
if(Objects.isNull(this.exceptionQps)) {
this.exceptionQps = 0L;
}
if(Objects.isNull(this.passQps)) {
this.passQps = 0L;
}
if(Objects.isNull(this.successQps)) {
this.successQps = 0L;
}
if(Objects.isNull(this.blockQps)) {
this.blockQps = 0L;
}
this.timestamp = entity.timestamp;

this.exceptionQps += entity.exceptionQps;
this.passQps += + entity.passQps;
this.successQps += entity.successQps;
this.blockQps += entity.blockQps;
this.rt += entity.rt;
}
}
Loading