From 4b81ae4abf1f80c9319f3b3c8dbcc4a82d3db9f8 Mon Sep 17 00:00:00 2001 From: shown Date: Wed, 11 Sep 2024 07:32:06 +0800 Subject: [PATCH] [improve]: extract common constant str (#2698) --- .../collect/http/HttpCollectImpl.java | 4 +-- .../common/constants/ConfigConstants.java | 9 ++++++ .../common/constants/NetworkConstants.java | 4 +++ .../grafana/common/GrafanaConstants.java | 14 ++++----- .../config/GrafanaAutoConfiguration.java | 7 ++++- .../grafana/config/GrafanaProperties.java | 29 ++++++++++--------- .../controller/DashboardController.java | 4 +-- .../grafana/service/DashboardService.java | 1 - .../grafana/service/DatasourceService.java | 1 - .../service/ServiceAccountService.java | 10 ++++--- .../vm/VictoriaMetricsClusterDataStorage.java | 15 ++++++---- .../vm/VictoriaMetricsDataStorage.java | 19 ++++++------ 12 files changed, 69 insertions(+), 48 deletions(-) diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java index 99dfe3f9b94..0a43f73166e 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java @@ -544,9 +544,9 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) { } else if (DispatchConstants.BASIC_AUTH.equals(authorization.getType())) { if (StringUtils.hasText(authorization.getBasicAuthUsername()) && StringUtils.hasText(authorization.getBasicAuthPassword())) { - String authStr = authorization.getBasicAuthUsername() + ":" + authorization.getBasicAuthPassword(); + String authStr = authorization.getBasicAuthUsername() + SignConstants.DOUBLE_MARK + authorization.getBasicAuthPassword(); String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); - requestBuilder.addHeader(HttpHeaders.AUTHORIZATION, DispatchConstants.BASIC + " " + encodedAuth); + requestBuilder.addHeader(HttpHeaders.AUTHORIZATION, DispatchConstants.BASIC + SignConstants.BLANK + encodedAuth); } } } diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/ConfigConstants.java b/common/src/main/java/org/apache/hertzbeat/common/constants/ConfigConstants.java index 5703a75d07d..727d9d7d122 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/constants/ConfigConstants.java +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/ConfigConstants.java @@ -23,6 +23,13 @@ public interface ConfigConstants { + /** + * System Constant: hertzbeat + */ + interface SystemConstant { + String PROJECT_NAME = "hertzbeat"; + } + /** * Package name constant. */ @@ -56,6 +63,8 @@ interface FunctionModuleConstants { String DISPATCH = "dispatch"; String INFO = "info"; + + String GRAFANA = "grafana"; } } diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java b/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java index de58a2e1fe7..f95c1fe13b9 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java @@ -47,6 +47,10 @@ public interface NetworkConstants { String LOCATION = "Location"; + String BASIC = "Basic"; + + String AUTHORIZATION = "Authorization"; + /** * HttpClient Configuration Constants. */ diff --git a/grafana/src/main/java/org/apache/hertzbeat/grafana/common/GrafanaConstants.java b/grafana/src/main/java/org/apache/hertzbeat/grafana/common/GrafanaConstants.java index 36712c1a4ee..ce4bf0aff8f 100644 --- a/grafana/src/main/java/org/apache/hertzbeat/grafana/common/GrafanaConstants.java +++ b/grafana/src/main/java/org/apache/hertzbeat/grafana/common/GrafanaConstants.java @@ -17,15 +17,15 @@ package org.apache.hertzbeat.grafana.common; +import org.apache.hertzbeat.common.constants.ConfigConstants; + /** * Grafana Common Constants */ public interface GrafanaConstants { - - String HTTP = "http://"; - String HTTPS = "https://"; - + String ADMIN = "admin"; + String KIOSK = "?kiosk=tv"; String REFRESH = "&refresh=15s"; @@ -50,7 +50,7 @@ public interface GrafanaConstants { String GET_SERVICE_ACCOUNTS_API = "%s:%s@%s/api/serviceaccounts/search"; - String ACCOUNT_NAME = "hertzbeat"; + String ACCOUNT_NAME = ConfigConstants.SystemConstant.PROJECT_NAME; String ACCOUNT_ROLE = "Admin"; @@ -58,9 +58,5 @@ public interface GrafanaConstants { String CREATE_SERVICE_TOKEN_API = "%s:%s@%s/api/serviceaccounts/%d/tokens"; - String APPLICATION_JSON = "application/json"; - - String URL = "url"; - String GRAFANA_CONFIG = "grafanaConfig"; } diff --git a/grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaAutoConfiguration.java b/grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaAutoConfiguration.java index 8cf6fd09481..73605368e7e 100644 --- a/grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaAutoConfiguration.java +++ b/grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaAutoConfiguration.java @@ -17,13 +17,18 @@ package org.apache.hertzbeat.grafana.config; +import org.apache.hertzbeat.common.constants.ConfigConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.ComponentScan; /** * Grafana auto configuration. */ -@ComponentScan(basePackages = "org.apache.hertzbeat.grafana") +@ComponentScan(basePackages = ConfigConstants.PkgConstant.PKG + + SignConstants.DOT + + ConfigConstants.FunctionModuleConstants.GRAFANA +) @EnableConfigurationProperties(GrafanaProperties.class) public class GrafanaAutoConfiguration { } diff --git a/grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaProperties.java b/grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaProperties.java index 49969d85329..0018c6cab60 100644 --- a/grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaProperties.java +++ b/grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaProperties.java @@ -17,9 +17,10 @@ package org.apache.hertzbeat.grafana.config; -import static org.apache.hertzbeat.grafana.common.GrafanaConstants.HTTP; -import static org.apache.hertzbeat.grafana.common.GrafanaConstants.HTTPS; import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.common.constants.ConfigConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; +import org.apache.hertzbeat.grafana.common.GrafanaConstants; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.bind.DefaultValue; @@ -27,31 +28,31 @@ * grafana configuration */ @Slf4j -@ConfigurationProperties(prefix = "grafana") +@ConfigurationProperties(prefix = ConfigConstants.FunctionModuleConstants.GRAFANA) public record GrafanaProperties(@DefaultValue("false") boolean enabled, @DefaultValue("http://127.0.0.1:3000") String url, - @DefaultValue("admin") String username, - @DefaultValue("admin") String password) { + @DefaultValue(GrafanaConstants.ADMIN) String username, + @DefaultValue(GrafanaConstants.ADMIN) String password) { /** * get the prefix of the grafana url, such as http or https */ public String getPrefix() { - if (url.startsWith(HTTP)) { - return HTTP; - } else if (url.startsWith(HTTPS)) { - return HTTPS; + if (url.startsWith(NetworkConstants.HTTP_HEADER)) { + return NetworkConstants.HTTP_HEADER; + } else if (url.startsWith(NetworkConstants.HTTPS_HEADER)) { + return NetworkConstants.HTTPS_HEADER; } - return HTTP; + return NetworkConstants.HTTP_HEADER; } /** * get the grafana url without the prefix, such as localhost:3000 */ public String getUrl() { - if (getPrefix().equals(HTTP)) { - return url.replace(HTTP, ""); - } else if (getPrefix().equals(HTTPS)) { - return url.replace(HTTPS, ""); + if (getPrefix().equals(NetworkConstants.HTTP_HEADER)) { + return url.replace(NetworkConstants.HTTP_HEADER, ""); + } else if (getPrefix().equals(NetworkConstants.HTTPS_HEADER)) { + return url.replace(NetworkConstants.HTTPS_HEADER, ""); } return url; } diff --git a/grafana/src/main/java/org/apache/hertzbeat/grafana/controller/DashboardController.java b/grafana/src/main/java/org/apache/hertzbeat/grafana/controller/DashboardController.java index 0201566f3dc..df72a30870d 100644 --- a/grafana/src/main/java/org/apache/hertzbeat/grafana/controller/DashboardController.java +++ b/grafana/src/main/java/org/apache/hertzbeat/grafana/controller/DashboardController.java @@ -19,7 +19,6 @@ import static org.apache.hertzbeat.common.constants.CommonConstants.FAIL_CODE; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; - import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; @@ -41,7 +40,8 @@ @Slf4j @Tag(name = "Dashboard API") @RestController -@RequestMapping(path = "/api/grafana/dashboard", produces = {APPLICATION_JSON_VALUE}) +@RequestMapping(path = "/api/grafana/dashboard", + produces = {APPLICATION_JSON_VALUE}) public class DashboardController { @Autowired diff --git a/grafana/src/main/java/org/apache/hertzbeat/grafana/service/DashboardService.java b/grafana/src/main/java/org/apache/hertzbeat/grafana/service/DashboardService.java index e5168c299c1..8b895da8210 100644 --- a/grafana/src/main/java/org/apache/hertzbeat/grafana/service/DashboardService.java +++ b/grafana/src/main/java/org/apache/hertzbeat/grafana/service/DashboardService.java @@ -41,7 +41,6 @@ import org.springframework.web.client.RestTemplate; import lombok.extern.slf4j.Slf4j; - /** * Service for managing Grafana dashboards. */ diff --git a/grafana/src/main/java/org/apache/hertzbeat/grafana/service/DatasourceService.java b/grafana/src/main/java/org/apache/hertzbeat/grafana/service/DatasourceService.java index 656529dcc3f..40b781402b9 100644 --- a/grafana/src/main/java/org/apache/hertzbeat/grafana/service/DatasourceService.java +++ b/grafana/src/main/java/org/apache/hertzbeat/grafana/service/DatasourceService.java @@ -17,7 +17,6 @@ package org.apache.hertzbeat.grafana.service; - import static org.apache.hertzbeat.grafana.common.GrafanaConstants.CREATE_DATASOURCE_API; import static org.apache.hertzbeat.grafana.common.GrafanaConstants.DATASOURCE_ACCESS; import static org.apache.hertzbeat.grafana.common.GrafanaConstants.DATASOURCE_NAME; diff --git a/grafana/src/main/java/org/apache/hertzbeat/grafana/service/ServiceAccountService.java b/grafana/src/main/java/org/apache/hertzbeat/grafana/service/ServiceAccountService.java index 4d40809523c..35d1578afe0 100644 --- a/grafana/src/main/java/org/apache/hertzbeat/grafana/service/ServiceAccountService.java +++ b/grafana/src/main/java/org/apache/hertzbeat/grafana/service/ServiceAccountService.java @@ -28,6 +28,8 @@ import java.util.Base64; import java.util.Objects; import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.common.constants.NetworkConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.entity.manager.GeneralConfig; import org.apache.hertzbeat.common.util.CommonUtil; import org.apache.hertzbeat.common.util.JsonUtil; @@ -43,7 +45,6 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; - /** * Service for managing Grafana service accounts and tokens. */ @@ -189,13 +190,14 @@ public ResponseEntity getAccounts() { } private HttpHeaders createHeaders() { - String auth = username + ":" + password; + String auth = username + SignConstants.DOUBLE_MARK + password; byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes()); - String authHeader = "Basic " + new String(encodedAuth); + String authHeader = NetworkConstants.BASIC + + SignConstants.BLANK + new String(encodedAuth); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); - headers.set("Authorization", authHeader); + headers.set(NetworkConstants.AUTHORIZATION, authHeader); return headers; } } diff --git a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsClusterDataStorage.java b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsClusterDataStorage.java index 18b1c57d409..c4747282ae9 100644 --- a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsClusterDataStorage.java +++ b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsClusterDataStorage.java @@ -40,6 +40,8 @@ import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.math.NumberUtils; import org.apache.hertzbeat.common.constants.CommonConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.entity.dto.Value; import org.apache.hertzbeat.common.entity.message.CollectRep; import org.apache.hertzbeat.common.util.CommonUtil; @@ -58,6 +60,8 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; +import static org.apache.hertzbeat.common.constants.ConfigConstants.FunctionModuleConstants.STATUS; + /** * tdengine data storage */ @@ -70,14 +74,12 @@ public class VictoriaMetricsClusterDataStorage extends AbstractHistoryDataStorag private static final String IMPORT_PATH = "/api/v1/import"; private static final String EXPORT_PATH = "/api/v1/export"; private static final String STATUS_PATH = "/api/v1/status/tsdb"; - private static final String STATUS = "status"; private static final String STATUS_SUCCESS = "success"; private static final String QUERY_RANGE_PATH = "/api/v1/query_range"; private static final String LABEL_KEY_NAME = "__name__"; private static final String LABEL_KEY_JOB = "job"; private static final String LABEL_KEY_INSTANCE = "instance"; private static final String SPILT = "_"; - private static final String BASIC = "Basic"; private static final String MONITOR_METRICS_KEY = "__metrics__"; private static final String MONITOR_METRIC_KEY = "__metric__"; @@ -177,7 +179,8 @@ public void saveData(CollectRep.MetricsData metricsData) { String encodedAuth = new String( Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); - headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth); + headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC + + SignConstants.BLANK + encodedAuth); } HttpEntity httpEntity = new HttpEntity<>(content, headers); ResponseEntity responseEntity = restTemplate.postForEntity( @@ -219,7 +222,8 @@ public Map> getHistoryMetricData(Long monitorId, String app, String authStr = vmSelectProps.username() + ":" + vmSelectProps.password(); String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); - headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth); + headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC + + SignConstants.BLANK + encodedAuth); } HttpEntity httpEntity = new HttpEntity<>(headers); URI uri = UriComponentsBuilder.fromHttpUrl(vmSelectProps.url() + EXPORT_PATH) @@ -314,7 +318,8 @@ public Map> getHistoryIntervalMetricData(Long monitorId, Str String authStr = vmSelectProps.username() + ":" + vmSelectProps.password(); String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); - headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth); + headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC + + SignConstants.BLANK + encodedAuth); } HttpEntity httpEntity = new HttpEntity<>(headers); URI uri = UriComponentsBuilder.fromHttpUrl(vmSelectProps.url() + QUERY_RANGE_PATH) diff --git a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsDataStorage.java b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsDataStorage.java index 01757f266d2..a830ff26d3c 100644 --- a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsDataStorage.java +++ b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsDataStorage.java @@ -39,6 +39,8 @@ import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.math.NumberUtils; import org.apache.hertzbeat.common.constants.CommonConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.entity.dto.Value; import org.apache.hertzbeat.common.entity.message.CollectRep; import org.apache.hertzbeat.common.util.CommonUtil; @@ -75,13 +77,10 @@ public class VictoriaMetricsDataStorage extends AbstractHistoryDataStorage { private static final String EXPORT_PATH = "/api/v1/export"; private static final String QUERY_RANGE_PATH = "/api/v1/query_range"; private static final String STATUS_PATH = "/api/v1/status/tsdb"; - private static final String STATUS = "status"; - private static final String STATUS_SUCCESS = "success"; private static final String LABEL_KEY_NAME = "__name__"; private static final String LABEL_KEY_JOB = "job"; private static final String LABEL_KEY_INSTANCE = "instance"; private static final String SPILT = "_"; - private static final String BASIC = "Basic"; private static final String MONITOR_METRICS_KEY = "__metrics__"; private static final String MONITOR_METRIC_KEY = "__metric__"; @@ -105,9 +104,9 @@ private boolean checkVictoriaMetricsDatasourceAvailable() { HttpHeaders headers = new HttpHeaders(); if (StringUtils.hasText(victoriaMetricsProp.username()) && StringUtils.hasText(victoriaMetricsProp.password())) { - String authStr = victoriaMetricsProp.username() + ":" + victoriaMetricsProp.password(); + String authStr = victoriaMetricsProp.username() + SignConstants.DOUBLE_MARK + victoriaMetricsProp.password(); String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); - headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth); + headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC + " " + encodedAuth); } HttpEntity httpEntity = new HttpEntity<>(headers); ResponseEntity responseEntity = restTemplate.exchange(victoriaMetricsProp.url() + STATUS_PATH, @@ -185,9 +184,10 @@ public void saveData(CollectRep.MetricsData metricsData) { headers.setContentType(MediaType.APPLICATION_JSON); if (StringUtils.hasText(victoriaMetricsProp.username()) && StringUtils.hasText(victoriaMetricsProp.password())) { - String authStr = victoriaMetricsProp.username() + ":" + victoriaMetricsProp.password(); + String authStr = victoriaMetricsProp.username() + SignConstants.DOUBLE_MARK + victoriaMetricsProp.password(); String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); - headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth); + headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC + + SignConstants.BLANK + encodedAuth); } HttpEntity httpEntity = new HttpEntity<>(content, headers); ResponseEntity responseEntity = restTemplate.postForEntity(victoriaMetricsProp.url() + IMPORT_PATH, @@ -227,7 +227,7 @@ public Map> getHistoryMetricData(Long monitorId, String app, && StringUtils.hasText(victoriaMetricsProp.password())) { String authStr = victoriaMetricsProp.username() + ":" + victoriaMetricsProp.password(); String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); - headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth); + headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC + SignConstants.BLANK + encodedAuth); } HttpEntity httpEntity = new HttpEntity<>(headers); URI uri = UriComponentsBuilder.fromHttpUrl(victoriaMetricsProp.url() + EXPORT_PATH) @@ -321,7 +321,8 @@ public Map> getHistoryIntervalMetricData(Long monitorId, Str && StringUtils.hasText(victoriaMetricsProp.password())) { String authStr = victoriaMetricsProp.username() + ":" + victoriaMetricsProp.password(); String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); - headers.add(HttpHeaders.AUTHORIZATION, BASIC + " " + encodedAuth); + headers.add(HttpHeaders.AUTHORIZATION, NetworkConstants.BASIC + + SignConstants.BLANK + encodedAuth); } HttpEntity httpEntity = new HttpEntity<>(headers); URI uri = UriComponentsBuilder.fromHttpUrl(victoriaMetricsProp.url() + QUERY_RANGE_PATH)