Skip to content

Commit eaaf589

Browse files
tomsun28yuluo-yx
andauthored
[improve] update grafana auth method and add expose url (#2818)
Signed-off-by: tomsun28 <[email protected]> Co-authored-by: shown <[email protected]>
1 parent be84a0a commit eaaf589

File tree

7 files changed

+21
-12
lines changed

7 files changed

+21
-12
lines changed

hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/common/GrafanaConstants.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public interface GrafanaConstants {
4848

4949
String QUERY_DATASOURCE_API = "/api/datasources/name/" + DATASOURCE_NAME;
5050

51-
String GET_SERVICE_ACCOUNTS_API = "%s:%s@%s/api/serviceaccounts/search";
51+
String GET_SERVICE_ACCOUNTS_API = "%s/api/serviceaccounts/search";
5252

5353
String ACCOUNT_NAME = ConfigConstants.SystemConstant.PROJECT_NAME;
5454

5555
String ACCOUNT_ROLE = "Admin";
5656

57-
String CREATE_SERVICE_ACCOUNT_API = "%s:%s@%s/api/serviceaccounts";
57+
String CREATE_SERVICE_ACCOUNT_API = "%s/api/serviceaccounts";
5858

59-
String CREATE_SERVICE_TOKEN_API = "%s:%s@%s/api/serviceaccounts/%d/tokens";
59+
String CREATE_SERVICE_TOKEN_API = "%s/api/serviceaccounts/%d/tokens";
6060

6161
String GRAFANA_CONFIG = "grafanaConfig";
6262
}

hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/config/GrafanaProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
@ConfigurationProperties(prefix = ConfigConstants.FunctionModuleConstants.GRAFANA)
3232
public record GrafanaProperties(@DefaultValue("false") boolean enabled,
3333
@DefaultValue("http://127.0.0.1:3000") String url,
34+
@DefaultValue("http://127.0.0.1:3000") String exposeUrl,
3435
@DefaultValue(GrafanaConstants.ADMIN) String username,
3536
@DefaultValue(GrafanaConstants.ADMIN) String password) {
3637
/**

hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/service/DashboardService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public ResponseEntity<?> createOrUpdateDashboard(String dashboardJson, Long moni
8989
GrafanaDashboard grafanaDashboard = JsonUtil.fromJson(response.getBody(), GrafanaDashboard.class);
9090
if (grafanaDashboard != null) {
9191
grafanaDashboard.setEnabled(true);
92-
grafanaDashboard.setUrl(grafanaProperties.getPrefix() + grafanaProperties.getUrl()
92+
grafanaDashboard.setUrl(grafanaProperties.exposeUrl()
9393
+ grafanaDashboard.getUrl().replace(grafanaProperties.getUrl(), "")
9494
+ KIOSK + REFRESH + INSTANCE + monitorId + USE_DATASOURCE);
9595
grafanaDashboard.setMonitorId(monitorId);

hertzbeat-grafana/src/main/java/org/apache/hertzbeat/grafana/service/ServiceAccountService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.http.HttpMethod;
4343
import org.springframework.http.MediaType;
4444
import org.springframework.http.ResponseEntity;
45+
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
4546
import org.springframework.stereotype.Service;
4647
import org.springframework.web.client.RestTemplate;
4748

@@ -92,10 +93,10 @@ public Long createServiceAccount() {
9293
return account.get("id").asLong();
9394
}
9495
}
95-
String endpoint = String.format(prefix + CREATE_SERVICE_ACCOUNT_API, username, password, url);
96+
String endpoint = String.format(prefix + CREATE_SERVICE_ACCOUNT_API, url);
9697
HttpHeaders headers = createHeaders();
9798
String body = String.format("{\"name\":\"%s\",\"role\":\"%s\",\"isDisabled\":false}", ACCOUNT_NAME, ACCOUNT_ROLE);
98-
99+
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(username, password));
99100
HttpEntity<String> request = new HttpEntity<>(body, headers);
100101
try {
101102
ResponseEntity<String> response = restTemplate.postForEntity(endpoint, request, String.class);
@@ -122,10 +123,10 @@ public String applyForToken() {
122123
log.error("Service account not found");
123124
throw new RuntimeException("Service account not found");
124125
}
125-
String endpoint = String.format(prefix + CREATE_SERVICE_TOKEN_API, username, password, url, accountId);
126+
String endpoint = String.format(prefix + CREATE_SERVICE_TOKEN_API, url, accountId);
126127
HttpHeaders headers = createHeaders();
127128
String body = String.format("{\"name\":\"%s\"}", CommonUtil.generateRandomWord(6));
128-
129+
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(username, password));
129130
HttpEntity<String> request = new HttpEntity<>(body, headers);
130131
try {
131132
ResponseEntity<String> response = restTemplate.postForEntity(endpoint, request, String.class);
@@ -173,9 +174,9 @@ public String getToken() {
173174
* @return ResponseEntity containing the list of service accounts
174175
*/
175176
public ResponseEntity<String> getAccounts() {
176-
String endpoint = String.format(prefix + GET_SERVICE_ACCOUNTS_API, username, password, url);
177+
String endpoint = String.format(prefix + GET_SERVICE_ACCOUNTS_API, url);
177178
HttpHeaders headers = createHeaders();
178-
179+
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(username, password));
179180
HttpEntity<String> request = new HttpEntity<>(headers);
180181
try {
181182
ResponseEntity<String> response = restTemplate.exchange(endpoint, HttpMethod.GET, request, String.class);

hertzbeat-manager/src/main/resources/application.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ scheduler:
208208
grafana:
209209
enabled: false
210210
url: http://127.0.0.1:3000
211+
expose-url: http://127.0.0.1:3000
211212
username: admin
212213
password: admin
213214

hertzbeat-warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsClusterDataStorage.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public void saveData(CollectRep.MetricsData metricsData) {
133133
boolean isPrometheusAuto = false;
134134
if (metricsData.getApp().startsWith(CommonConstants.PROMETHEUS_APP_PREFIX)) {
135135
isPrometheusAuto = true;
136+
defaultLabels.remove(MONITOR_METRICS_KEY);
136137
defaultLabels.put(LABEL_KEY_JOB,
137138
metricsData.getApp().substring(CommonConstants.PROMETHEUS_APP_PREFIX.length()));
138139
} else {
@@ -168,7 +169,9 @@ public void saveData(CollectRep.MetricsData metricsData) {
168169
String labelName = isPrometheusAuto ? metricsData.getMetrics()
169170
: metricsData.getMetrics() + SPILT + entry.getKey();
170171
labels.put(LABEL_KEY_NAME, labelName);
171-
labels.put(MONITOR_METRIC_KEY, entry.getKey());
172+
if (!isPrometheusAuto) {
173+
labels.put(MONITOR_METRIC_KEY, entry.getKey());
174+
}
172175
VictoriaMetricsContent content = VictoriaMetricsContent.builder().metric(labels)
173176
.values(new Double[]{entry.getValue()}).timestamps(timestamp).build();
174177
HttpHeaders headers = new HttpHeaders();

hertzbeat-warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/vm/VictoriaMetricsDataStorage.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public void saveData(CollectRep.MetricsData metricsData) {
139139
boolean isPrometheusAuto = false;
140140
if (metricsData.getApp().startsWith(CommonConstants.PROMETHEUS_APP_PREFIX)) {
141141
isPrometheusAuto = true;
142+
defaultLabels.remove(MONITOR_METRICS_KEY);
142143
defaultLabels.put(LABEL_KEY_JOB, metricsData.getApp()
143144
.substring(CommonConstants.PROMETHEUS_APP_PREFIX.length()));
144145
} else {
@@ -174,7 +175,9 @@ public void saveData(CollectRep.MetricsData metricsData) {
174175
String labelName = isPrometheusAuto ? metricsData.getMetrics()
175176
: metricsData.getMetrics() + SPILT + entry.getKey();
176177
labels.put(LABEL_KEY_NAME, labelName);
177-
labels.put(MONITOR_METRIC_KEY, entry.getKey());
178+
if (!isPrometheusAuto) {
179+
labels.put(MONITOR_METRIC_KEY, entry.getKey());
180+
}
178181
VictoriaMetricsContent content = VictoriaMetricsContent.builder()
179182
.metric(labels)
180183
.values(new Double[]{entry.getValue()})

0 commit comments

Comments
 (0)