From 81b90fee5213870b1eb6810d2140d09b4a7fec42 Mon Sep 17 00:00:00 2001 From: Ilia Naryzhny Date: Tue, 10 Mar 2020 20:34:07 -0700 Subject: [PATCH] Initial commit of orienteer-metrics for issue #395 --- modules.xml | 10 + orienteer-metrics/README.md | 1 + orienteer-metrics/pom.xml | 269 ++++++++++++++++++ .../org/orienteer/metrics/Initializer.java | 23 ++ .../org/orienteer/metrics/OMetricsModule.java | 43 +++ .../orienteer/metrics/OMetricsResource.java | 53 ++++ .../org/orienteer/metrics/package-info.java | 4 + .../services/org.apache.wicket.IInitializer | 1 + .../src/main/resources/log4j.properties | 12 + .../org/orienteer/metrics/TestModule.java | 34 +++ pom.xml | 1 + 11 files changed, 451 insertions(+) create mode 100644 orienteer-metrics/README.md create mode 100644 orienteer-metrics/pom.xml create mode 100644 orienteer-metrics/src/main/java/org/orienteer/metrics/Initializer.java create mode 100644 orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricsModule.java create mode 100644 orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricsResource.java create mode 100644 orienteer-metrics/src/main/java/org/orienteer/metrics/package-info.java create mode 100644 orienteer-metrics/src/main/resources/META-INF/services/org.apache.wicket.IInitializer create mode 100644 orienteer-metrics/src/main/resources/log4j.properties create mode 100644 orienteer-metrics/src/test/java/org/orienteer/metrics/TestModule.java diff --git a/modules.xml b/modules.xml index a286d46c9..cbfb486c3 100644 --- a/modules.xml +++ b/modules.xml @@ -76,4 +76,14 @@ orienteer-bot Module to build and run messangers bots + + org.orienteer + orienteer-tours + Module to create guided tours + + + org.orienteer + orienteer-metrics + Module to gather metrics from Orienteer instance + diff --git a/orienteer-metrics/README.md b/orienteer-metrics/README.md new file mode 100644 index 000000000..9a88ba582 --- /dev/null +++ b/orienteer-metrics/README.md @@ -0,0 +1 @@ +Module to Collect Metrics from Orienteer \ No newline at end of file diff --git a/orienteer-metrics/pom.xml b/orienteer-metrics/pom.xml new file mode 100644 index 000000000..553ecdee6 --- /dev/null +++ b/orienteer-metrics/pom.xml @@ -0,0 +1,269 @@ + + + + orienteer-parent + org.orienteer + 1.5-SNAPSHOT + + 4.0.0 + + orienteer-metrics + orienteer-metrics + + + + org.orienteer + orienteer-core + ${project.version} + + + + io.prometheus + simpleclient + 0.8.1 + + + + io.prometheus + simpleclient_common + 0.8.1 + + + + io.prometheus + simpleclient_hotspot + 0.8.1 + + + + + + + + + + + + + + + + + + + javax.servlet + javax.servlet-api + + + + org.orienteer + orienteer-core + ${project.version} + test-jar + test + + + ru.ydn.wicket.wicket-orientdb + wicket-orientdb + ${wicket.orientdb.version} + test-jar + test + + + + junit + junit + 4.11 + test + + + javax.servlet + javax.servlet-api + 3.0.1 + test + + + + + + src/main/resources + + + src/main/java + + ** + + + **/*.java + + + + + + src/test/resources + + + src/test/java + + ** + + + **/*.java + + + + + + true + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + UTF-8 + true + true + + + + org.apache.felix + maven-bundle-plugin + 2.3.6 + true + + + org.apache.maven.plugins + maven-eclipse-plugin + 2.9 + + true + ${wtp.version} + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + true + true + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + + ${settings.localRepository} + + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + + + orienteer.loader.repository.local + ${settings.localRepository} + + + + jar + + + ../orienteer-war/src/main/webapp/WEB-INF/web.xml + src/main/resources + / + ^$ + ^$ + + + org.eclipse.jetty.server.nio.SelectChannelConnector + 8080 + 3600000 + + + + + org.eclipse.jetty.aggregate + jetty-all + uber + ${jetty.version} + + + + + + diff --git a/orienteer-metrics/src/main/java/org/orienteer/metrics/Initializer.java b/orienteer-metrics/src/main/java/org/orienteer/metrics/Initializer.java new file mode 100644 index 000000000..112afe9bb --- /dev/null +++ b/orienteer-metrics/src/main/java/org/orienteer/metrics/Initializer.java @@ -0,0 +1,23 @@ +package org.orienteer.metrics; + +import org.apache.wicket.Application; +import org.apache.wicket.IInitializer; +import org.orienteer.core.OrienteerWebApplication; + +/** + * {@link IInitializer} for 'tours' module + */ +public class Initializer implements IInitializer +{ + @Override + public void init(Application application) { + OrienteerWebApplication app = (OrienteerWebApplication)application; + app.registerModule(OMetricsModule.class); + } + + @Override + public void destroy(Application application) { + OrienteerWebApplication app = (OrienteerWebApplication)application; + } + +} diff --git a/orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricsModule.java b/orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricsModule.java new file mode 100644 index 000000000..2ef98a972 --- /dev/null +++ b/orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricsModule.java @@ -0,0 +1,43 @@ +package org.orienteer.metrics; + +import org.orienteer.core.OrienteerWebApplication; +import org.orienteer.core.module.AbstractOrienteerModule; +import org.orienteer.core.module.IOrienteerModule; + +import com.orientechnologies.orient.core.db.document.ODatabaseDocument; +import com.orientechnologies.orient.core.record.impl.ODocument; + +import io.prometheus.client.CollectorRegistry; +import io.prometheus.client.hotspot.DefaultExports; + +/** + * {@link IOrienteerModule} for 'metrics' module + */ +public class OMetricsModule extends AbstractOrienteerModule{ + + + protected OMetricsModule() { + super("metrics", 1); + } + + @Override + public ODocument onInstall(OrienteerWebApplication app, ODatabaseDocument db) { + super.onInstall(app, db); + return null; + } + + @Override + public void onInitialize(OrienteerWebApplication app, ODatabaseDocument db) { + super.onInitialize(app, db); + DefaultExports.initialize(); + app.mountPackage(OMetricsModule.class.getPackage().getName()); + } + + @Override + public void onDestroy(OrienteerWebApplication app, ODatabaseDocument db) { + app.unmountPackage(OMetricsModule.class.getPackage().getName()); + CollectorRegistry.defaultRegistry.clear(); + super.onDestroy(app, db); + } + +} diff --git a/orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricsResource.java b/orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricsResource.java new file mode 100644 index 000000000..56c9322ce --- /dev/null +++ b/orienteer-metrics/src/main/java/org/orienteer/metrics/OMetricsResource.java @@ -0,0 +1,53 @@ +package org.orienteer.metrics; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.HashSet; +import java.util.Set; + +import org.apache.wicket.request.mapper.parameter.PageParameters; +import org.apache.wicket.request.resource.AbstractResource; +import org.apache.wicket.request.resource.IResource; +import org.apache.wicket.util.time.Time; +import org.orienteer.core.MountPath; + +import io.prometheus.client.CollectorRegistry; +import io.prometheus.client.exporter.common.TextFormat; + +/** + * Resource to share metrics + */ +@MountPath("/metrics") +public class OMetricsResource extends AbstractResource { + + @Override + protected ResourceResponse newResourceResponse(Attributes attributes) { + ResourceResponse response = new ResourceResponse(); + response.setLastModified(Time.now()); +// response.setStatusCode(HttpServlet); + response.setContentType(TextFormat.CONTENT_TYPE_004); + if (response.dataNeedsToBeWritten(attributes)) { + + PageParameters params = attributes.getParameters(); + Set includedMetrics = new HashSet<>(); + params.getValues("name").forEach((sv) -> {if(!sv.isEmpty()) includedMetrics.add(sv.toString()); }); + + response.setWriteCallback(createWriteCallback(CollectorRegistry.defaultRegistry, includedMetrics)); + } + return response; + } + + private WriteCallback createWriteCallback(CollectorRegistry registry, Set metrics) { + return new WriteCallback() { + @Override + public void writeData(IResource.Attributes attributes) throws IOException { + try(OutputStreamWriter writer = new OutputStreamWriter(attributes.getResponse().getOutputStream(), "UTF8")) { + TextFormat.write004(writer, registry.filteredMetricFamilySamples(metrics)); + writer.flush(); + } + } + }; + } + +} diff --git a/orienteer-metrics/src/main/java/org/orienteer/metrics/package-info.java b/orienteer-metrics/src/main/java/org/orienteer/metrics/package-info.java new file mode 100644 index 000000000..f0b6e735c --- /dev/null +++ b/orienteer-metrics/src/main/java/org/orienteer/metrics/package-info.java @@ -0,0 +1,4 @@ +/** + * Main package for 'metrics' module + */ +package org.orienteer.metrics; \ No newline at end of file diff --git a/orienteer-metrics/src/main/resources/META-INF/services/org.apache.wicket.IInitializer b/orienteer-metrics/src/main/resources/META-INF/services/org.apache.wicket.IInitializer new file mode 100644 index 000000000..b0154bea7 --- /dev/null +++ b/orienteer-metrics/src/main/resources/META-INF/services/org.apache.wicket.IInitializer @@ -0,0 +1 @@ +org.orienteer.metrics.Initializer \ No newline at end of file diff --git a/orienteer-metrics/src/main/resources/log4j.properties b/orienteer-metrics/src/main/resources/log4j.properties new file mode 100644 index 000000000..908318d9a --- /dev/null +++ b/orienteer-metrics/src/main/resources/log4j.properties @@ -0,0 +1,12 @@ +log4j.appender.Stdout=org.apache.log4j.ConsoleAppender +log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.Stdout.layout.conversionPattern=[%.4p] (%d) %.26c{1}: %m%n + +log4j.rootLogger=INFO,Stdout + +log4j.logger.org.apache.wicket=INFO +log4j.logger.org.apache.wicket.protocol.http.HttpSessionStore=INFO +log4j.logger.org.apache.wicket.version=INFO +log4j.logger.org.apache.wicket.RequestCycle=INFO + + diff --git a/orienteer-metrics/src/test/java/org/orienteer/metrics/TestModule.java b/orienteer-metrics/src/test/java/org/orienteer/metrics/TestModule.java new file mode 100644 index 000000000..33435d301 --- /dev/null +++ b/orienteer-metrics/src/test/java/org/orienteer/metrics/TestModule.java @@ -0,0 +1,34 @@ +package org.orienteer.metrics; + +import org.orienteer.core.OrienteerWebApplication; +import org.orienteer.core.module.IOrienteerModule; + +import org.orienteer.junit.OrienteerTestRunner; +import org.orienteer.junit.OrienteerTester; +import org.orienteer.metrics.OMetricsModule; + +import static org.junit.Assert.*; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.google.inject.Inject; +import com.google.inject.Singleton; + + +@RunWith(OrienteerTestRunner.class) +@Singleton +public class TestModule +{ + @Inject + private OrienteerTester tester; + + @Test + public void testModuleLoaded() + { + OrienteerWebApplication app = tester.getApplication(); + assertNotNull(app); + IOrienteerModule module = app.getModuleByName("metrics"); + assertNotNull(module); + assertTrue(module instanceof OMetricsModule); + } +} diff --git a/pom.xml b/pom.xml index b847089e7..80fdccc3b 100644 --- a/pom.xml +++ b/pom.xml @@ -444,6 +444,7 @@ orienteer-users orienteer-object orienteer-tours + orienteer-metrics