-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make query based monitoring enabled and disabled via API
- Loading branch information
1 parent
4e2273e
commit 763154b
Showing
6 changed files
with
311 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
rule-based/src/main/java/nl/jads/sodalite/dto/MetricRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package nl.jads.sodalite.dto; | ||
|
||
import org.json.simple.JSONArray; | ||
|
||
public class MetricRecord { | ||
private String name; | ||
private String valueType; | ||
private JSONArray value; | ||
private String label; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public JSONArray getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(JSONArray value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValueType() { | ||
return valueType; | ||
} | ||
|
||
public void setValueType(String valueType) { | ||
this.valueType = valueType; | ||
} | ||
|
||
public String getLabel() { | ||
return label; | ||
} | ||
|
||
public void setLabel(String label) { | ||
this.label = label; | ||
} | ||
} |
28 changes: 23 additions & 5 deletions
28
rule-based/src/main/java/nl/jads/sodalite/scheduler/MonitoringTimerTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
rule-based/src/main/java/nl/jads/sodalite/utils/PrometheusClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package nl.jads.sodalite.utils; | ||
|
||
import com.sun.jersey.api.json.JSONConfiguration; | ||
import nl.jads.sodalite.dto.MetricRecord; | ||
import org.glassfish.jersey.client.ClientConfig; | ||
import org.glassfish.jersey.client.ClientProperties; | ||
import org.json.simple.JSONArray; | ||
import org.json.simple.JSONObject; | ||
import org.json.simple.parser.JSONParser; | ||
import org.json.simple.parser.ParseException; | ||
|
||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.Invocation; | ||
import javax.ws.rs.client.WebTarget; | ||
import javax.ws.rs.core.MediaType; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
public class PrometheusClient { | ||
private String baseRestUri; | ||
|
||
public PrometheusClient() { | ||
baseRestUri = System.getenv("prometheusrest"); | ||
if (baseRestUri == null || "".equals(baseRestUri.trim())) { | ||
baseRestUri = "http://154.48.185.213:9090/"; | ||
} | ||
} | ||
|
||
public List<MetricRecord> readMetric(String query) throws ParseException { | ||
ClientConfig config = new ClientConfig(); | ||
config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, Boolean.TRUE); | ||
config.property(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); | ||
Client client = ClientBuilder.newClient(config); | ||
WebTarget webTarget = client.target(baseRestUri).path("api/v1/query").queryParam("query", query); | ||
Invocation.Builder invocationBuilder | ||
= webTarget.request(MediaType.APPLICATION_JSON); | ||
String response | ||
= invocationBuilder.get(String.class); | ||
JSONParser jsonParser = new JSONParser(); | ||
|
||
JSONObject result = (JSONObject) jsonParser.parse(response); | ||
List<MetricRecord> metricRecords = new ArrayList<>(); | ||
if ("success".equalsIgnoreCase(String.valueOf(result.get("status")))) { | ||
System.out.println("Successful collected data: " + query); | ||
JSONObject dataObject = ((JSONObject) result.get("data")); | ||
String resultType = String.valueOf(dataObject.get("resultType")); | ||
JSONArray jsonArray = (JSONArray) dataObject.get("result"); | ||
for (Iterator it = jsonArray.iterator(); it.hasNext(); ) { | ||
MetricRecord metricRecord = new MetricRecord(); | ||
JSONObject metric = (JSONObject) it.next(); | ||
String mName = String.valueOf(((JSONObject) metric.get("metric")).get("__name__")); | ||
String label = String.valueOf(((JSONObject) metric.get("metric")).get("instance")); | ||
JSONArray mValue = (JSONArray) metric.get("value"); | ||
metricRecord.setValueType(resultType); | ||
metricRecord.setName(mName); | ||
metricRecord.setValue(mValue); | ||
metricRecord.setLabel(label); | ||
metricRecords.add(metricRecord); | ||
} | ||
} else { | ||
System.out.println("Error collecting data: " + query); | ||
} | ||
return metricRecords; | ||
} | ||
} |
Oops, something went wrong.