fix(性能测试): 修复自定义监控查询时,输入promQL中包括特殊字符不能正常使用的问题

--bug=1028478 --user=刘瑞斌 【性能测试】github#26031,【性能测试】监控磁盘IO使用率,在prometheus页面使用promQL语句(读+写之和)可以正常查询到,将表达式放到MS监控里,没有监控数据出现 https://www.tapd.cn/55049933/s/1402006
This commit is contained in:
CaptainB 2023-08-09 11:39:05 +08:00 committed by 刘瑞斌
parent 345c519f12
commit b52c642414
1 changed files with 16 additions and 3 deletions

View File

@ -17,11 +17,14 @@ import io.metersphere.request.MetricRequest;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;
@ -82,8 +85,18 @@ public class MetricQueryService {
String start = df.format(startTime / 1000.0);
String end = df.format(endTime / 1000.0);
try {
Map response = restTemplate.getForObject(prometheusHost + "/api/v1/query_range?query={promQL}&start={start}&end={end}&step={step}", Map.class, promQL, start, end, step);
LogUtil.info(prometheusHost + "/api/v1/query_range?query={" + promQL + "}&start={" + start + "}&end{" + end + "}&step={" + step + "}");
LogUtil.debug(prometheusHost + "/api/v1/query_range?query=" + promQL + "&start=" + start + "&end" + end + "&step=" + step);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
// 设置请求参数
MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
postParameters.add("query", promQL);
postParameters.add("start", start);
postParameters.add("end", end);
postParameters.add("step", step);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(postParameters, headers);
Map response = restTemplate.postForObject(prometheusHost + "/api/v1/query_range", httpEntity, Map.class);
metricData = handleResult(seriesName, response, instance);
} catch (Exception e) {
LogUtil.error("query prometheus metric fail.");