Skip to content

Commit 066d746

Browse files
CyantyAias00
andauthored
[improve] Adjust Grafana configuration priority hierarchy and optimize API request exception handling (apache#3241)
Co-authored-by: aias00 <[email protected]>
1 parent 9619638 commit 066d746

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.http.ResponseEntity;
4141
import org.springframework.stereotype.Service;
4242
import org.springframework.transaction.annotation.Transactional;
43+
import org.springframework.web.client.HttpClientErrorException;
4344
import org.springframework.web.client.RestTemplate;
4445
import lombok.extern.slf4j.Slf4j;
4546

@@ -71,6 +72,11 @@ public class DashboardService {
7172
*/
7273
@Transactional(rollbackFor = Exception.class)
7374
public ResponseEntity<?> createOrUpdateDashboard(String dashboardJson, Long monitorId) {
75+
if (!grafanaProperties.enabled()) {
76+
log.info("HertzBeat Grafana config not enabled");
77+
throw new RuntimeException("HertzBeat Grafana config not enabled");
78+
}
79+
7480
String token = serviceAccountService.getToken();
7581
String url = grafanaProperties.getPrefix() + grafanaProperties.getUrl() + CREATE_DASHBOARD_API;
7682

@@ -103,6 +109,12 @@ public ResponseEntity<?> createOrUpdateDashboard(String dashboardJson, Long moni
103109
log.error("create dashboard error: {}", response.getStatusCode());
104110
throw new RuntimeException("create dashboard error");
105111
}
112+
} catch (HttpClientErrorException.Forbidden ex) {
113+
log.error("Grafana Access denied to save dashboard", ex);
114+
throw new RuntimeException("Grafana Access denied to save dashboard", ex);
115+
} catch (HttpClientErrorException.NotFound ex){
116+
log.error("Grafana Dashboard not found", ex);
117+
throw new RuntimeException("Grafana Dashboard not found", ex);
106118
} catch (Exception ex) {
107119
log.error("create dashboard error", ex);
108120
throw new RuntimeException("create dashboard error", ex);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public DatasourceService(
6161
* Create a new datasource in Grafana.
6262
*/
6363
public void existOrCreateDatasource(String token) {
64+
if (!warehouseProperties.enabled()) {
65+
log.info("HertzBeat VictoriaMetrics config not enabled");
66+
return;
67+
}
68+
6469
HttpHeaders headers = new HttpHeaders();
6570
headers.setContentType(MediaType.APPLICATION_JSON);
6671
headers.setBearerAuth(token);

0 commit comments

Comments
 (0)