Correct serialization / deserialization of MetricResult.

This commit is contained in:
Mark Nellemann 2021-05-06 10:57:35 +02:00
parent 94eb0d17f9
commit afdc293b43
8 changed files with 88 additions and 17 deletions

View file

@ -15,6 +15,7 @@ public class MetricProcessor implements Processor {
private static final AtomicLong counter = new AtomicLong(); private static final AtomicLong counter = new AtomicLong();
/*
public void process(Exchange exchange) throws Exception { public void process(Exchange exchange) throws Exception {
MetricResult reading = exchange.getIn().getBody(MetricResult.class); MetricResult reading = exchange.getIn().getBody(MetricResult.class);
@ -26,6 +27,14 @@ public class MetricProcessor implements Processor {
// do something... // do something...
MetricMessageDTO payload = new MetricMessageDTO("event " + reading, counter.getAndIncrement()); MetricMessageDTO payload = new MetricMessageDTO("event " + reading, counter.getAndIncrement());
exchange.getIn().setBody(payload, MetricMessageDTO.class); exchange.getIn().setBody(payload, MetricMessageDTO.class);
}*/
public void process(Exchange exchange) throws Exception {
MetricResult result = exchange.getIn().getBody(MetricResult.class);
result.setHostname("sauron");
exchange.getIn().setBody(result);
exchange.getIn().setHeader("component", result.getName());
} }

View file

@ -2,11 +2,14 @@ package org.sysmon.agent;
import org.apache.camel.Exchange; import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder; import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.pf4j.JarPluginManager; import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager; import org.pf4j.PluginManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.sysmon.shared.MetricExtension; import org.sysmon.shared.MetricExtension;
import org.sysmon.shared.MetricResult;
import java.util.List; import java.util.List;
@ -75,13 +78,13 @@ public class MyRouteBuilder extends RouteBuilder {
// Send to collector when combined // Send to collector when combined
from("seda:metrics") from("seda:metrics")
.process(new MetricProcessor())
.marshal().json()
.setHeader(Exchange.HTTP_METHOD, constant("POST")) .setHeader(Exchange.HTTP_METHOD, constant("POST"))
.doTry() .doTry()
.process(new MetricProcessor())
.marshal().json(JsonLibrary.Jackson, MetricResult.class)
.to("http://127.0.0.1:9925/metrics") .to("http://127.0.0.1:9925/metrics")
.doCatch(Exception.class) .doCatch(Exception.class)
.log("Error sending metric to collector.") .log("Error sending metric to collector: ${exception}")
.end(); .end();

View file

@ -20,7 +20,7 @@
camel.main.name = SysMon-Agent camel.main.name = SysMon-Agent
# enable tracing # enable tracing
### camel.main.tracing = true #camel.main.tracing = true
# bean introspection to log reflection based configuration # bean introspection to log reflection based configuration
camel.main.beanIntrospectionExtendedStatistics=true camel.main.beanIntrospectionExtendedStatistics=true

View file

@ -1,7 +1,9 @@
package org.sysmon.collector; package org.sysmon.collector;
import org.apache.camel.builder.RouteBuilder; import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.apache.camel.model.rest.RestBindingMode; import org.apache.camel.model.rest.RestBindingMode;
import org.sysmon.shared.MetricResult;
import org.sysmon.shared.dto.MetricMessageDTO; import org.sysmon.shared.dto.MetricMessageDTO;
public class CollectorRouteBuilder extends RouteBuilder { public class CollectorRouteBuilder extends RouteBuilder {
@ -25,12 +27,13 @@ public class CollectorRouteBuilder extends RouteBuilder {
.post("/metrics") .post("/metrics")
.consumes("application/json") .consumes("application/json")
.produces("text/html") .produces("text/html")
.type(MetricMessageDTO.class) .type(MetricResult.class)
.route() .route()
.to("bean:incomingMetricProcessor")
.to("seda:inbound") .to("seda:inbound")
.endRest(); .endRest();
from("seda:inbound").log("Got metric from: ${header.component}").to("mock:sink");
/* /*
from("seda:inbound") from("seda:inbound")
.to("influxdb://myInfluxConnection?databaseName=sysmon"); .to("influxdb://myInfluxConnection?databaseName=sysmon");

View file

@ -4,6 +4,7 @@ import org.apache.camel.Exchange;
import org.apache.camel.Processor; import org.apache.camel.Processor;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.sysmon.shared.MetricResult;
import org.sysmon.shared.dto.MetricMessageDTO; import org.sysmon.shared.dto.MetricMessageDTO;
public class IncomingMetricProcessor implements Processor { public class IncomingMetricProcessor implements Processor {
@ -12,9 +13,9 @@ public class IncomingMetricProcessor implements Processor {
public void process(Exchange exchange) throws Exception { public void process(Exchange exchange) throws Exception {
MetricMessageDTO payload = exchange.getIn().getBody(MetricMessageDTO.class); MetricResult payload = exchange.getIn().getBody(MetricResult.class);
log.info("I am going to send this data to InfluxDB."); //log.info("I am going to send this data to InfluxDB.");
log.info(payload.toString()); //log.info(payload.toString());
exchange.getMessage().setBody("OK"); exchange.getMessage().setBody("OK");
} }

View file

@ -17,10 +17,10 @@
# to configure camel main # to configure camel main
# here you can configure options on camel main (see MainConfigurationProperties class) # here you can configure options on camel main (see MainConfigurationProperties class)
camel.main.name = SysMon-Collector camel.main.name = sysmon-collector
# enable tracing # enable tracing
camel.main.tracing = true #camel.main.tracing = true
# bean introspection to log reflection based configuration # bean introspection to log reflection based configuration
camel.main.beanIntrospectionExtendedStatistics=true camel.main.beanIntrospectionExtendedStatistics=true

View file

@ -5,6 +5,8 @@ public class MetricMeasurement {
private String name; private String name;
private Object value; private Object value;
public MetricMeasurement() { }
public MetricMeasurement(String name, Object value) { public MetricMeasurement(String name, Object value) {
this.name = name; this.name = name;
this.value = value; this.value = value;
@ -14,4 +16,20 @@ public class MetricMeasurement {
return String.format("%s: %s", name, value); return String.format("%s: %s", name, value);
} }
public String getName() {
return name;
}
public Object getValue() {
return value;
}
public void setName(String name) {
this.name = name;
}
public void setValue(Object value) {
this.value = value;
}
} }

View file

@ -1,17 +1,26 @@
package org.sysmon.shared; package org.sysmon.shared;
import java.io.Serializable;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MetricResult { public class MetricResult implements Serializable {
private final String name; private static final long serialVersionUID = 1L;
private final Instant timestamp;
private List<MetricMeasurement> measurementList; private String name;
private Long timestamp; // epoch milli
private String hostname;
private List<MetricMeasurement> measurementList = new ArrayList<>();
public MetricResult() {
}
public MetricResult(String name) { public MetricResult(String name) {
this.name = name; this.name = name;
this.timestamp = Instant.now(); this.timestamp = Instant.now().toEpochMilli();
} }
public void setMetricMeasurementList(List<MetricMeasurement> measurementList) { public void setMetricMeasurementList(List<MetricMeasurement> measurementList) {
@ -22,6 +31,34 @@ public class MetricResult {
measurementList.add(measurement); measurementList.add(measurement);
} }
public void setHostname(String hostname) {
this.hostname = hostname;
}
public void setName(String name) {
this.name = name;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public String getName() {
return name;
}
public Long getTimestamp() {
return timestamp;
}
public String getHostname() {
return hostname;
}
public List<MetricMeasurement> getMeasurementList() {
return measurementList;
}
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(String.format("%s - %s\n", timestamp.toString(), name)); StringBuilder sb = new StringBuilder(String.format("%s - %s\n", timestamp.toString(), name));
for(MetricMeasurement mm : measurementList) { for(MetricMeasurement mm : measurementList) {