Skip to content

Commit

Permalink
[improve] update grafana auth method and add expose url (#2818)
Browse files Browse the repository at this point in the history
Signed-off-by: tomsun28 <[email protected]>
Co-authored-by: shown <[email protected]>
  • Loading branch information
tomsun28 and yuluo-yx authored Nov 18, 2024
1 parent be84a0a commit eaaf589
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> request = new HttpEntity<>(body, headers);
try {
ResponseEntity<String> response = restTemplate.postForEntity(endpoint, request, String.class);
Expand All @@ -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<String> request = new HttpEntity<>(body, headers);
try {
ResponseEntity<String> response = restTemplate.postForEntity(endpoint, request, String.class);
Expand Down Expand Up @@ -173,9 +174,9 @@ public String getToken() {
* @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);
Expand Down
1 change: 1 addition & 0 deletions hertzbeat-manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()})
Expand Down

0 comments on commit eaaf589

Please sign in to comment.