Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading