From c957fcbd71f888658778e40b81c3d92224f784a4 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Tue, 15 Aug 2023 18:12:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E5=B8=A6=E8=AE=A4=E8=AF=81=E7=9A=84prometheus=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20basic=20auth?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1028734 --user=刘瑞斌 【性能测试】使用带认证信息的 Prometheus 地址获取监控信息报错 https://www.tapd.cn/55049933/s/1404029 --- .../metersphere/service/MetricQueryService.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/performance-test/backend/src/main/java/io/metersphere/service/MetricQueryService.java b/performance-test/backend/src/main/java/io/metersphere/service/MetricQueryService.java index e1afe8f923..6a32275d22 100644 --- a/performance-test/backend/src/main/java/io/metersphere/service/MetricQueryService.java +++ b/performance-test/backend/src/main/java/io/metersphere/service/MetricQueryService.java @@ -7,6 +7,7 @@ import io.metersphere.base.mapper.LoadTestReportMapper; import io.metersphere.base.mapper.ext.ExtLoadTestReportMapper; import io.metersphere.commons.constants.ParamConstants; import io.metersphere.commons.exception.MSException; +import io.metersphere.commons.utils.CodingUtil; import io.metersphere.commons.utils.DateUtils; import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.LogUtil; @@ -25,6 +26,7 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; +import java.net.URL; import java.text.DecimalFormat; import java.util.*; import java.util.stream.Collectors; @@ -85,9 +87,19 @@ public class MetricQueryService { String start = df.format(startTime / 1000.0); String end = df.format(endTime / 1000.0); try { - LogUtil.debug(prometheusHost + "/api/v1/query_range?query=" + promQL + "&start=" + start + "&end" + end + "&step=" + step); + // prometheusHost 不再变更 + String host = prometheusHost; HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/x-www-form-urlencoded"); + // 如果prometheus开启了认证,需要在请求头中添加认证信息 + if (host.contains("@")) { + URL url = new URL(host); + // 获取认证信息部分 + String userInfo = url.getUserInfo(); + headers.add("Authorization", "Basic " + CodingUtil.base64Encoding(userInfo)); + host = host.replace(userInfo + "@", ""); + } + LogUtil.debug(host + "/api/v1/query_range?query=" + promQL + "&start=" + start + "&end" + end + "&step=" + step); // 设置请求参数 MultiValueMap postParameters = new LinkedMultiValueMap<>(); postParameters.add("query", promQL); @@ -96,7 +108,7 @@ public class MetricQueryService { postParameters.add("step", step); HttpEntity> httpEntity = new HttpEntity<>(postParameters, headers); - Map response = restTemplate.postForObject(prometheusHost + "/api/v1/query_range", httpEntity, Map.class); + Map response = restTemplate.postForObject(host + "/api/v1/query_range", httpEntity, Map.class); metricData = handleResult(seriesName, response, instance); } catch (Exception e) { LogUtil.error("query prometheus metric fail.");