网关重启适配

This commit is contained in:
huangweixiong 2022-08-24 15:00:40 +08:00
parent c51e335f9f
commit 55e3b4a44e
1 changed files with 22 additions and 11 deletions

View File

@ -1,6 +1,7 @@
package io.renren.modules.gateway.controller;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.gateway.dao.ApiCountHistoryDao;
@ -32,6 +33,7 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
@ -73,10 +75,7 @@ public class MonitorControllerV2 {
@Autowired
private ApiCountHistoryDao apiCountHistoryDao;
@GetMapping("/queryGroupByAbility")
@ApiOperation("统计数据按能力归集")
public Result queryGroupByAbility(Long start, Long end) throws InterruptedException, ExecutionException, TimeoutException {
public List queryMetricCount(String queryFormat, Long start, Long end, String metricElement, int limit) throws InterruptedException, ExecutionException, TimeoutException {
String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}&time={time}";
//查询重启记录时间节点
@ -98,7 +97,7 @@ public class MonitorControllerV2 {
HashMap<String, Long> map = new HashMap<String, Long>();
Long startTime = timePoint.get(i);
Long endTime = timePoint.get(i+1);
String query = String.format("topk(10, sum(label_replace(increase(apigateway_http_status[%ds:1s]), \"groupInfo\", \"$2\", \"matched_uri\", \"/juapi/(.*?)/(.*?)/.*\")) by (groupInfo))", endTime - startTime);
String query = String.format(queryFormat, endTime - startTime);
futures.add(CompletableFuture.supplyAsync(() -> {
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query, endTime);
@ -129,13 +128,13 @@ public class MonitorControllerV2 {
for (HashMap hashMap : result) {
Map metric = (Map) hashMap.get("metric");
List value = (List) hashMap.get("value");
if (metric == null || metric.get("groupInfo") == null || value == null || value.size() != 2) {
if (metric == null || metric.get(metricElement) == null || value == null || value.size() != 2) {
continue;
}
try {
metric.put("count", Double.valueOf((String) value.get(1)).intValue());
Long groupInfo = Long.valueOf((String) metric.get("groupInfo"));
metric.put("groupInfo", groupInfo);
Long groupInfo = Long.valueOf((String) metric.get(metricElement));
metric.put(metricElement, groupInfo);
results.add(metric);
} catch (Exception e) {
//忽略
@ -152,7 +151,7 @@ public class MonitorControllerV2 {
for (CompletableFuture<List<?>> future : futures) {
List<Map<String, Object>> list = (List<Map<String, Object>>) future.get(30, TimeUnit.SECONDS);
list.forEach(item -> {
Long groupInfo = (Long) item.get("groupInfo");
Long groupInfo = (Long) item.get(metricElement);
if (filterMap.containsKey(groupInfo)) {
Map<String, Object> objectMap = filterMap.get(groupInfo);
Integer count = (Integer) objectMap.get("count");
@ -167,11 +166,22 @@ public class MonitorControllerV2 {
List<Map<String, Object>> results = filterMap.values().stream()
.sorted(Comparator.comparingInt(item -> (int) ((Map) item).get("count"))
.reversed())
.limit(10)
.limit(limit)
.collect(Collectors.toList());
return results;
}
@GetMapping("/queryGroupByAbility")
@ApiOperation("统计数据按能力归集")
public Result queryGroupByAbility(Long start, Long end) throws InterruptedException, ExecutionException, TimeoutException {
String queryFormat = "topk(10, sum(label_replace(increase(apigateway_http_status[%ds:1s]), \"groupInfo\", \"$2\", \"matched_uri\", \"/juapi/(.*?)/(.*?)/.*\")) by (groupInfo))";
List<Map<String, Object>> results = queryMetricCount(queryFormat, start, end, "groupInfo", 10);
//数据聚合
if (!results.isEmpty()) {
List<Object> ids = results.stream().map(item -> item.get("groupInfo")).collect(Collectors.toList());
LambdaQueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<ResourceEntity>().lambda();
queryWrapper
.select(ResourceEntity::getId,
@ -179,7 +189,7 @@ public class MonitorControllerV2 {
ResourceEntity::getApiMethodType,
ResourceEntity::getType,
ResourceEntity::getApiUrl)
.in(ResourceEntity::getId, filterMap.keySet());
.in(ResourceEntity::getId, ids);
List<ResourceEntity> entities = resourceDao.selectList(queryWrapper);
for (Map map : results) {
Long groupInfo = (Long) map.get("groupInfo");
@ -212,6 +222,7 @@ public class MonitorControllerV2 {
@GetMapping("/queryGroupByDepartment")
@ApiOperation("统计数据按部门归集")
public Result queryGroupByDepartment(String query, String time){
/** ================*/
String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}" + "&time=" + time;
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query);
HashMap body = entity.getBody();