in ozhera-monitor/ozhera-monitor-service/src/main/java/org/apache/ozhera/monitor/service/api/impl/GrafanaServiceExtensionImpl.java [174:243]
public String innerRequestGrafanaStr(String area, String title, String containerName, String group, GrafanaTemplate template, String application) {
String folderId = grafanaFolderData.get("id");
String folderUid = grafanaFolderData.get("uid");
String grafanaUrl = grafanaAddress;
String grafanaApiKey = this.grafanaApiKey;
if (grafanaUrl == null || grafanaApiKey == null) {
log.error("Incoming environment exception, server is {} url is {} ", title, grafanaUrl);
}
Map<String, Object> map = getTemplateVariables(folderId, group, title, folderUid, grafanaUrl, containerName, area, application);
try {
String temp = template.getTemplate();
String data = FreeMarkerUtil.freemarkerProcess(map, template.getTemplate());
URL url = new URL(grafanaUrl + getGrafanaCreateDashboardUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
PrintWriter out = null;
conn.setRequestProperty("Expect", "");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.setRequestProperty("Authorization", "Bearer " + grafanaApiKey);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.connect();
//POST request
BufferedWriter out1 = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
out1.write(data);
out1.flush();
out1.close();
//Determine whether it has already been generated, and if it is generated, the panel will replace it and request it again
String finalGrafanaStr = "";
if (conn.getResponseCode() == 412) {
log.info("requestGrafana panel already created,second request begin appName:{}", title);
conn.disconnect();
String checkUrl = "";
String uid = title;
int len = title.length();
if (len > 40) {
uid = title.substring(0, 40);
}
checkUrl = grafanaUrl + grafanaCheckUrl + uid;
String finalData = this.getFinalData(data, checkUrl, grafanaApiKey, "GET", title, template.getPanelIdList(), false, null);
finalGrafanaStr = innerRequestGrafana(finalData, grafanaUrl + getGrafanaCreateDashboardUrl, grafanaApiKey, "POST");
} else {
//The chart has not been generated before, and the default template is directly generated
String finalStr = "";
try (InputStream is = conn.getInputStream()) {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = br.readLine()) != null) {
finalStr = str;
}
}
conn.disconnect();
finalGrafanaStr = finalStr;
}
//First judge whether the return is json in a specific format, if not, the interface request fails, just return it directly
//Add the version management function to request the grafana version interface
String dashboardId = isGrafanaDataJson(finalGrafanaStr);
if (StringUtils.isEmpty(dashboardId)) {
return finalGrafanaStr;
}
//If the interface returns a result in the correct format, request the grafana version api to determine whether the update/creation is successful
String version = getDashboardLastVersion(dashboardId);
JsonObject jsonObject = gson.fromJson(finalGrafanaStr, JsonObject.class);
jsonObject.addProperty("mimonitor_version", version);
return jsonObject.toString();
} catch (Exception e) {
return e.getMessage();
}
}