[improve] update grafana auth method and add expose url (#2818)

Signed-off-by: tomsun28 <tomsun28@outlook.com>
Co-authored-by: shown <yuluo08290126@gmail.com>
This commit is contained in:
tomsun28
2024-11-18 23:19:57 +08:00
committed by GitHub
co-authored by shown
parent be84a0abae
commit eaaf589083
7 changed files with 21 additions and 12 deletions
@@ -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";
}
@@ -31,6 +31,7 @@ import org.springframework.boot.context.properties.bind.DefaultValue;
@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) {
/**
@@ -89,7 +89,7 @@ public class DashboardService {
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);
@@ -42,6 +42,7 @@ import org.springframework.http.HttpHeaders;
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 class ServiceAccountService {
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<String> request = new HttpEntity<>(body, headers);
try {
ResponseEntity<String> response = restTemplate.postForEntity(endpoint, request, String.class);
@@ -122,10 +123,10 @@ public class ServiceAccountService {
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<String> request = new HttpEntity<>(body, headers);
try {
ResponseEntity<String> response = restTemplate.postForEntity(endpoint, request, String.class);
@@ -173,9 +174,9 @@ public class ServiceAccountService {
* @return ResponseEntity containing the list of service accounts
*/
public ResponseEntity<String> 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<String> request = new HttpEntity<>(headers);
try {
ResponseEntity<String> response = restTemplate.exchange(endpoint, HttpMethod.GET, request, String.class);
@@ -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
@@ -133,6 +133,7 @@ public class VictoriaMetricsClusterDataStorage extends AbstractHistoryDataStorag
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 class VictoriaMetricsClusterDataStorage extends AbstractHistoryDataStorag
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();
@@ -139,6 +139,7 @@ public class VictoriaMetricsDataStorage extends AbstractHistoryDataStorage {
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 class VictoriaMetricsDataStorage extends AbstractHistoryDataStorage {
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()})