sysmon/client/src/main/java/sysmon/client/ClientRouteBuilder.java

98 lines
3.8 KiB
Java
Raw Normal View History

2021-05-21 09:08:43 +00:00
package sysmon.client;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;
2021-05-07 15:53:47 +00:00
import org.apache.camel.spi.Registry;
import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-05-21 09:08:43 +00:00
import sysmon.shared.MetricExtension;
import sysmon.shared.MetricResult;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
2021-05-07 15:53:47 +00:00
public class ClientRouteBuilder extends RouteBuilder {
2021-05-07 15:53:47 +00:00
private static final Logger log = LoggerFactory.getLogger(ClientRouteBuilder.class);
@Override
public void configure() {
2021-05-07 15:53:47 +00:00
Registry registry = getContext().getRegistry();
2021-09-10 10:15:33 +00:00
Configuration configuration = (Configuration) registry.lookupByName("configuration");
2021-05-07 15:53:47 +00:00
Path[] pluginpaths = { Paths.get(registry.lookupByNameAndType("pluginPath", String.class)) };
PluginManager pluginManager = new JarPluginManager(pluginpaths);
pluginManager.loadPlugins();
pluginManager.startPlugins();
List<String> providers = new ArrayList<>();
List<MetricExtension> metricExtensions = pluginManager.getExtensions(MetricExtension.class);
for (MetricExtension ext : metricExtensions) {
2021-09-10 10:15:33 +00:00
final String name = ext.getName();
final String provides = ext.getProvides();
2021-09-10 10:15:33 +00:00
// Load configuration if available
if(configuration.isForExtension(name)) {
log.info(">>> Loading configuring for extension: " + ext.getDescription());
ext.setConfiguration(configuration.getForExtension(name));
}
if(ext.isSupported() && ext.isEnabled()) {
// Check that another extension has not already been loaded - TODO: Is this required ?
if(providers.contains(provides)) {
log.warn("Skipping extension (already provided): " + ext.getName());
continue;
}
log.info(">>> Enabling extension: " + ext.getDescription());
providers.add(provides);
2021-09-10 10:15:33 +00:00
// TODO: Make timer thread configurable ?
// Setup Camel route for this extension
2021-05-13 19:53:28 +00:00
// a unique timer name gives the timer it's own thread, otherwise it's a shared thread for other timers with same name.
//from("timer:"+provides+"?fixedRate=true&period=30s")
from("timer:extensions?fixedRate=true&period=30s")
.bean(ext, "getMetrics")
2021-05-06 14:31:02 +00:00
//.doTry()
.outputType(MetricResult.class)
.process(new MetricEnrichProcessor(registry))
2021-05-06 14:31:02 +00:00
.choice().when(exchangeProperty("skip").isEqualTo(true))
2021-05-31 12:44:48 +00:00
.log("Skipping empty measurement.")
2021-05-06 14:31:02 +00:00
.stop()
.otherwise()
.to("seda:metrics?discardWhenFull=true");
} else {
2021-09-10 10:15:33 +00:00
log.info(">>> Skipping extension (not supported or disabled): " + ext.getDescription());
}
}
// TODO: Make 'concurrentConsumers' configurable
from("seda:metrics?concurrentConsumers=1")
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
2021-05-06 14:31:02 +00:00
//.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.doTry()
//.process(new MetricProcessor())
.marshal().json(JsonLibrary.Jackson, MetricResult.class)
2021-05-07 15:53:47 +00:00
.to((String)registry.lookupByName("myServerUrl"))
.doCatch(Exception.class)
2021-05-06 14:31:02 +00:00
.log("Error: ${exception.message}")
//.log("Error sending metric to collector: ${body}")
.end();
}
2021-05-11 07:05:00 +00:00
}