Skip to content

Commit

Permalink
Make query based monitoring enabled and disabled via API
Browse files Browse the repository at this point in the history
  • Loading branch information
IndikaKuma committed Jul 31, 2020
1 parent 4e2273e commit 763154b
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Singleton;
import javax.servlet.ServletContext;
Expand Down Expand Up @@ -131,13 +130,24 @@ public Response uploadFile(

}

@PostConstruct
public void init() {
try {
monitoringDataCollector.start();
} catch (InterruptedException e) {
e.printStackTrace();
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/monitoring/pull")
public Response enableDisableMonitoring(@DefaultValue("disabled") @QueryParam("state") String state) {
if ("enabled".equalsIgnoreCase(state)) {
try {
monitoringDataCollector.start();
return Response.status(200).entity("Monitoring Enabled").build();
} catch (InterruptedException e) {
e.printStackTrace();
return Response.status(500).entity(e.getMessage()).build();
}
} else {
monitoringDataCollector.shutdown();
return Response.status(200).entity("Monitoring Disabled/Stopped").build();
}

}

@PreDestroy
Expand Down
128 changes: 127 additions & 1 deletion rule-based/src/main/java/nl/jads/sodalite/db/MetricsDatabase.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package nl.jads.sodalite.db;

import nl.jads.sodalite.dto.DataRecord;
import nl.jads.sodalite.dto.MetricRecord;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.sql.Connection;
import java.sql.ResultSet;
Expand All @@ -23,6 +27,7 @@ public class MetricsDatabase {
private MetricsDatabase() {
createDataSource();
createTable();
createRawMetricTable();
}

private static Connection getConnection() throws SQLException {
Expand Down Expand Up @@ -77,6 +82,41 @@ private static void createTable() {
}
}

private static void createRawMetricTable() {
Connection connection = null;
Statement stmt = null;
try {
connection = ds.getConnection();
stmt = connection.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS RAWMETRICS " +
"(ID INT PRIMARY KEY NOT NULL," +
" LABEL varchar(255) NOT NULL," +
" METRIC varchar(255) NOT NULL," +
" VAlUETYPE varchar(255), " +
" VALUE TEXT)";
stmt.executeUpdate(sql);
if (log.isInfoEnabled()) {
log.info("Metrics table was created successfully");
}
} catch (SQLException e) {
e.printStackTrace();
log.error(e.getMessage());
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ignored) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException ignored) {
}
}
}
}

/**
* Adding a monitoring data record
*
Expand All @@ -96,7 +136,42 @@ public void addDataRecord(DataRecord dataRecord) {
System.out.println(sql);
stmt.executeUpdate(sql);
if (log.isInfoEnabled()) {
log.info("Metrics table was created successfully");
log.info("Add data to the metrics table was created successfully");
}
} catch (SQLException e) {
e.printStackTrace();
log.error(e.getMessage());
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ignored) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException ignored) {
}
}
}
}

public void addMetricRecord(MetricRecord metricRecord) {
Connection connection = null;
Statement stmt = null;
try {
connection = ds.getConnection();
stmt = connection.createStatement();
String sql =
String.format("INSERT INTO RAWMETRICS (ID, LABEL, METRIC, VALUETYPE, " +
"VALUE) VALUES ( %d,'%s','%s', '%s', '%s');",
System.currentTimeMillis(), metricRecord.getLabel(), metricRecord.getName(),
metricRecord.getValueType(), metricRecord.getValue().toJSONString());
System.out.println(sql);
stmt.executeUpdate(sql);
if (log.isInfoEnabled()) {
log.info("Add data to the raw metrics table was created successfully");
}
} catch (SQLException e) {
e.printStackTrace();
Expand Down Expand Up @@ -163,4 +238,55 @@ public List<DataRecord> getDataRecord(String label) {
}
return dataRecords;
}

/**
* Get the records of monitoring data per node
*
* @param label the label of a node
* @return a record of monitoring data
*/
public List<MetricRecord> getMetricRecord(String label) {
List<MetricRecord> dataRecords = new ArrayList<>();
Connection connection = null;
Statement stmt = null;
try {
connection = ds.getConnection();
stmt = connection.createStatement();
ResultSet rs =
stmt.executeQuery(String.format("SELECT * FROM RAWMETRICS WHERE LABEL='%s';", label));

while (rs.next()) {
MetricRecord dataRecord = new MetricRecord();
dataRecord.setLabel(label);
dataRecord.setLabel(rs.getString("label"));
dataRecord.setName(rs.getString("metric"));
dataRecord.setValueType(rs.getString("valuetype"));
JSONParser parser = new JSONParser();
try {
dataRecord.setValue((JSONArray) parser.parse(rs.getString("value")));
} catch (ParseException e) {
e.printStackTrace();
}
dataRecords.add(dataRecord);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
log.error(e.getMessage());
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ignored) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException ignored) {
}
}
}
return dataRecords;
}
}
42 changes: 42 additions & 0 deletions rule-based/src/main/java/nl/jads/sodalite/dto/MetricRecord.java
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
package nl.jads.sodalite.scheduler;

import nl.jads.sodalite.db.MetricsDatabase;
import nl.jads.sodalite.dto.MetricRecord;
import nl.jads.sodalite.utils.PrometheusClient;
import org.json.simple.parser.ParseException;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.TimerTask;

public class MonitoringTimerTask extends TimerTask {
public class MonitoringTimerTask extends TimerTask {
MetricsDatabase database = MetricsDatabase.getInstance();
PrometheusClient prometheusClient = new PrometheusClient();

@Override
public void run() {
// System.out.println("Data Collected at: "
// + LocalDateTime.ofInstant(Instant.ofEpochMilli(scheduledExecutionTime()),
// ZoneId.systemDefault()));
try {
Thread.sleep(100 * 3);
List<MetricRecord> metricRecords =
prometheusClient.readMetric("http_requests_total");
for (MetricRecord mr : metricRecords) {
database.addMetricRecord(mr);
}
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Data Collected at: "
+ LocalDateTime.ofInstant(Instant.ofEpochMilli(scheduledExecutionTime()),
ZoneId.systemDefault()));
try {
Thread.sleep(1000 * 60);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down
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;
}
}
Loading

0 comments on commit 763154b

Please sign in to comment.