From 8e05694dfaabbc319d0a928886f519e2507fe09f Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Sun, 17 Nov 2024 10:27:59 +0800 Subject: [PATCH] [improve] update grafana auth method and add expose url Signed-off-by: tomsun28 --- .../hertzbeat/grafana/common/GrafanaConstants.java | 6 +++--- .../hertzbeat/grafana/config/GrafanaProperties.java | 1 + .../hertzbeat/grafana/service/DashboardService.java | 2 +- .../grafana/service/ServiceAccountService.java | 13 +++++++------ .../src/main/resources/application.yml | 1 + .../vm/VictoriaMetricsClusterDataStorage.java | 5 ++++- .../history/vm/VictoriaMetricsDataStorage.java | 5 ++++- 7 files changed, 21 insertions(+), 12 deletions(-) diff --git a/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/common/GrafanaConstants.java b/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/common/GrafanaConstants.java index ce4bf0aff8f..9c26924a134 100644 --- a/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/common/GrafanaConstants.java +++ b/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/common/GrafanaConstants.java @@ -48,15 +48,15 @@ public interface GrafanaConstants { String QUERY_DATASOURCE_API = "/api/datasources/name/" + DATASOURCE_NAME; - String GET_SERVICE_ACCOUNTS_API = "%s:%s@%s/api/serviceaccounts/search"; + String GET_SERVICE_ACCOUNTS_API = "%s/api/serviceaccounts/search"; String ACCOUNT_NAME = ConfigConstants.SystemConstant.PROJECT_NAME; String ACCOUNT_ROLE = "Admin"; - String CREATE_SERVICE_ACCOUNT_API = "%s:%s@%s/api/serviceaccounts"; + String CREATE_SERVICE_ACCOUNT_API = "%s/api/serviceaccounts"; - String CREATE_SERVICE_TOKEN_API = "%s:%s@%s/api/serviceaccounts/%d/tokens"; + String CREATE_SERVICE_TOKEN_API = "%s/api/serviceaccounts/%d/tokens"; String GRAFANA_CONFIG = "grafanaConfig"; } diff --git a/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaProperties.java b/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaProperties.java index 0018c6cab60..74144b76fdd 100644 --- a/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaProperties.java +++ b/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaProperties.java @@ -31,6 +31,7 @@ @ConfigurationProperties(prefix = ConfigConstants.FunctionModuleConstants.GRAFANA) public record GrafanaProperties(@DefaultValue("false") boolean enabled, @DefaultValue("http://127.0.0.1:3000") String url, + @DefaultValue("http://127.0.0.1:3000") String exposeUrl, @DefaultValue(GrafanaConstants.ADMIN) String username, @DefaultValue(GrafanaConstants.ADMIN) String password) { /** diff --git a/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/service/DashboardService.java b/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/service/DashboardService.java index 8b895da8210..679db311286 100644 --- a/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/service/DashboardService.java +++ b/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/service/DashboardService.java @@ -89,7 +89,7 @@ public ResponseEntity createOrUpdateDashboard(String dashboardJson, Long moni GrafanaDashboard grafanaDashboard = JsonUtil.fromJson(response.getBody(), GrafanaDashboard.class); if (grafanaDashboard != null) { grafanaDashboard.setEnabled(true); - grafanaDashboard.setUrl(grafanaProperties.getPrefix() + grafanaProperties.getUrl() + grafanaDashboard.setUrl(grafanaProperties.exposeUrl() + grafanaDashboard.getUrl().replace(grafanaProperties.getUrl(), "") + KIOSK + REFRESH + INSTANCE + monitorId + USE_DATASOURCE); grafanaDashboard.setMonitorId(monitorId); diff --git a/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/service/ServiceAccountService.java b/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/service/ServiceAccountService.java index 35d1578afe0..50427a2b844 100644 --- a/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/service/ServiceAccountService.java +++ b/hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/service/ServiceAccountService.java @@ -42,6 +42,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.http.client.support.BasicAuthenticationInterceptor; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @@ -92,10 +93,10 @@ public Long createServiceAccount() { return account.get("id").asLong(); } } - String endpoint = String.format(prefix + CREATE_SERVICE_ACCOUNT_API, username, password, url); + String endpoint = String.format(prefix + CREATE_SERVICE_ACCOUNT_API, url); HttpHeaders headers = createHeaders(); String body = String.format("{\"name\":\"%s\",\"role\":\"%s\",\"isDisabled\":false}", ACCOUNT_NAME, ACCOUNT_ROLE); - + restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(username, password)); HttpEntity request = new HttpEntity<>(body, headers); try { ResponseEntity response = restTemplate.postForEntity(endpoint, request, String.class); @@ -122,10 +123,10 @@ public String applyForToken() { log.error("Service account not found"); throw new RuntimeException("Service account not found"); } - String endpoint = String.format(prefix + CREATE_SERVICE_TOKEN_API, username, password, url, accountId); + String endpoint = String.format(prefix + CREATE_SERVICE_TOKEN_API, url, accountId); HttpHeaders headers = createHeaders(); String body = String.format("{\"name\":\"%s\"}", CommonUtil.generateRandomWord(6)); - + restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(username, password)); HttpEntity request = new HttpEntity<>(body, headers); try { ResponseEntity response = restTemplate.postForEntity(endpoint, request, String.class); @@ -173,9 +174,9 @@ public String getToken() { * @return ResponseEntity containing the list of service accounts */ public ResponseEntity getAccounts() { - String endpoint = String.format(prefix + GET_SERVICE_ACCOUNTS_API, username, password, url); + String endpoint = String.format(prefix + GET_SERVICE_ACCOUNTS_API, url); HttpHeaders headers = createHeaders(); - + restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(username, password)); HttpEntity request = new HttpEntity<>(headers); try { ResponseEntity response = restTemplate.exchange(endpoint, HttpMethod.GET, request, String.class); diff --git a/hertzbeat-manager/src/main/resources/application.yml b/hertzbeat-manager/src/main/resources/application.yml index 1d0d878d85f..82728fc5fc1 100644 --- a/hertzbeat-manager/src/main/resources/application.yml +++ b/hertzbeat-manager/src/main/resources/application.yml @@ -208,6 +208,7 @@ scheduler: grafana: enabled: false url: http://127.0.0.1:3000 + expose-url: http://127.0.0.1:3000 username: admin password: admin diff --git a/hertzbeat-warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsClusterDataStorage.java b/hertzbeat-warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsClusterDataStorage.java index c4747282ae9..ec521be9736 100644 --- a/hertzbeat-warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsClusterDataStorage.java +++ b/hertzbeat-warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsClusterDataStorage.java @@ -133,6 +133,7 @@ public void saveData(CollectRep.MetricsData metricsData) { boolean isPrometheusAuto = false; if (metricsData.getApp().startsWith(CommonConstants.PROMETHEUS_APP_PREFIX)) { isPrometheusAuto = true; + defaultLabels.remove(MONITOR_METRICS_KEY); defaultLabels.put(LABEL_KEY_JOB, metricsData.getApp().substring(CommonConstants.PROMETHEUS_APP_PREFIX.length())); } else { @@ -168,7 +169,9 @@ public void saveData(CollectRep.MetricsData metricsData) { String labelName = isPrometheusAuto ? metricsData.getMetrics() : metricsData.getMetrics() + SPILT + entry.getKey(); labels.put(LABEL_KEY_NAME, labelName); - labels.put(MONITOR_METRIC_KEY, entry.getKey()); + if (!isPrometheusAuto) { + labels.put(MONITOR_METRIC_KEY, entry.getKey()); + } VictoriaMetricsContent content = VictoriaMetricsContent.builder().metric(labels) .values(new Double[]{entry.getValue()}).timestamps(timestamp).build(); HttpHeaders headers = new HttpHeaders(); diff --git a/hertzbeat-warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsDataStorage.java b/hertzbeat-warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsDataStorage.java index a830ff26d3c..7dd905f9998 100644 --- a/hertzbeat-warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsDataStorage.java +++ b/hertzbeat-warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsDataStorage.java @@ -139,6 +139,7 @@ public void saveData(CollectRep.MetricsData metricsData) { boolean isPrometheusAuto = false; if (metricsData.getApp().startsWith(CommonConstants.PROMETHEUS_APP_PREFIX)) { isPrometheusAuto = true; + defaultLabels.remove(MONITOR_METRICS_KEY); defaultLabels.put(LABEL_KEY_JOB, metricsData.getApp() .substring(CommonConstants.PROMETHEUS_APP_PREFIX.length())); } else { @@ -174,7 +175,9 @@ public void saveData(CollectRep.MetricsData metricsData) { String labelName = isPrometheusAuto ? metricsData.getMetrics() : metricsData.getMetrics() + SPILT + entry.getKey(); labels.put(LABEL_KEY_NAME, labelName); - labels.put(MONITOR_METRIC_KEY, entry.getKey()); + if (!isPrometheusAuto) { + labels.put(MONITOR_METRIC_KEY, entry.getKey()); + } VictoriaMetricsContent content = VictoriaMetricsContent.builder() .metric(labels) .values(new Double[]{entry.getValue()})