Skip to content

Commit

Permalink
Merge pull request #3 from bobmcwhirter/metrics
Browse files Browse the repository at this point in the history
First shot at integrating mp-metrics.
  • Loading branch information
stuartwdouglas authored Jul 31, 2018
2 parents 96b0928 + 03c3712 commit 1cee801
Show file tree
Hide file tree
Showing 22 changed files with 599 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class RuntimePriority {

public static final int UNDERTOW_CREATE_DEPLOYMENT = 100;
public static final int UNDERTOW_REGISTER_SERVLET = 200;
public static final int JAXRS_DEPLOYMENT = 250;
public static final int HEALTH_DEPLOYMENT = 260;
public static final int WELD_DEPLOYMENT = 300;
public static final int JAXRS_DEPLOYMENT = 350;
public static final int UNDERTOW_DEPLOY = 400;
public static final int UNDERTOW_START = 500;

Expand Down
7 changes: 6 additions & 1 deletion examples/everything/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<artifactId>shamrock-health-deployment</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-metrics-deployment</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
Expand Down Expand Up @@ -123,4 +128,4 @@
</profile>
</profiles>

</project>
</project>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import javax.ws.rs.Produces;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.microprofile.metrics.annotation.Counted;
import org.eclipse.microprofile.metrics.annotation.Metric;

@Path("/test")
public class TestResource {

@GET
@Counted(monotonic = true)
public String getTest() {
return "TEST";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import org.jboss.shamrock.deployment.ResourceProcessor;
import org.jboss.shamrock.deployment.RuntimePriority;
import org.jboss.shamrock.deployment.codegen.BytecodeRecorder;
import org.jboss.shamrock.jaxrs.runtime.graal.JaxrsTemplate;
import org.jboss.shamrock.jaxrs.runtime.graal.ShamrockInjectorFactory;
import org.jboss.shamrock.runtime.InjectionInstance;
import org.jboss.shamrock.undertow.runtime.UndertowDeploymentTemplate;

Expand All @@ -66,7 +68,9 @@ public class JaxrsScanningProcessor implements ResourceProcessor {
private static final String JAX_RS_SERVLET_NAME = "javax.ws.rs.core.Application";

private static final DotName APPLICATION_PATH = DotName.createSimple("javax.ws.rs.ApplicationPath");

private static final DotName PATH = DotName.createSimple("javax.ws.rs.Path");

private static final DotName XML_ROOT = DotName.createSimple("javax.xml.bind.annotation.XmlRootElement");

private static final DotName[] METHOD_ANNOTATIONS = {
Expand Down Expand Up @@ -125,6 +129,7 @@ public void process(ArchiveContext archiveContext, ProcessorContext processorCon

undertow.addServletContextParameter(null, ResteasyContextParameters.RESTEASY_SCANNED_RESOURCES, sb.toString());
undertow.addServletContextParameter(null, "resteasy.servlet.mapping.prefix", path);
undertow.addServletContextParameter(null, "resteasy.injector.factory", ShamrockInjectorFactory.class.getName());
processorContext.addReflectiveClass(HttpServlet30Dispatcher.class.getName());
for (String i : loadProviders()) {
processorContext.addReflectiveClass(i);
Expand All @@ -144,6 +149,10 @@ public void process(ArchiveContext archiveContext, ProcessorContext processorCon
}
}

try (BytecodeRecorder recorder = processorContext.addStaticInitTask(RuntimePriority.JAXRS_DEPLOYMENT)) {
JaxrsTemplate jaxrs = recorder.getRecordingProxy(JaxrsTemplate.class);
jaxrs.setupIntegration(null);
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion jaxrs/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jboss.shamrock.jaxrs.runtime.graal;

import javax.enterprise.inject.se.SeContainer;

import org.jboss.shamrock.runtime.ContextObject;

/**
* Created by bob on 7/31/18.
*/
public class JaxrsTemplate {

public void setupIntegration(@ContextObject("weld.container")SeContainer container) {
ShamrockInjectorFactory.CONTAINER = container;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.jboss.shamrock.jaxrs.runtime.graal;

import java.lang.reflect.Constructor;

import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.CDI;
import javax.ws.rs.WebApplicationException;

import org.jboss.resteasy.spi.ApplicationException;
import org.jboss.resteasy.spi.ConstructorInjector;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.HttpResponse;
import sun.security.provider.SHA;

/**
* Created by bob on 7/31/18.
*/
public class ShamrockConstructorInjector implements ConstructorInjector {
public ShamrockConstructorInjector(Constructor ctor, ConstructorInjector delegate) {
this.ctor = ctor;
this.delegate = delegate;
}

@Override
public Object construct() {
System.err.println( "construct() " + this.ctor );
return this.delegate.construct();
}

@Override
public Object construct(HttpRequest request, HttpResponse response) throws Failure, WebApplicationException, ApplicationException {
System.err.println( "construct(req,resp) " + this.ctor );
System.err.println( "CAN WE CDI? " + ShamrockInjectorFactory.CONTAINER);
Instance object = ShamrockInjectorFactory.CONTAINER.select(this.ctor.getDeclaringClass());
//return this.delegate.construct(request, response);
return object.get();
}

@Override
public Object[] injectableArguments() {
return this.delegate.injectableArguments();
}

@Override
public Object[] injectableArguments(HttpRequest request, HttpResponse response) throws Failure {
return this.delegate.injectableArguments(request, response);
}

private final ConstructorInjector delegate;

private final Constructor ctor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jboss.shamrock.jaxrs.runtime.graal;

import java.lang.reflect.Constructor;

import javax.enterprise.inject.se.SeContainer;

import org.jboss.resteasy.core.InjectorFactoryImpl;
import org.jboss.resteasy.spi.ConstructorInjector;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.spi.metadata.ResourceConstructor;

/**
* Created by bob on 7/31/18.
*/
public class ShamrockInjectorFactory extends InjectorFactoryImpl {

public static SeContainer CONTAINER = null;

@Override
public ConstructorInjector createConstructor(Constructor constructor, ResteasyProviderFactory providerFactory) {
System.err.println( "create constructor: " + constructor );
return super.createConstructor(constructor, providerFactory);
}

@Override
public ConstructorInjector createConstructor(ResourceConstructor constructor, ResteasyProviderFactory providerFactory) {
System.err.println( "create resource constructor: " + constructor.getConstructor() );
return new ShamrockConstructorInjector(constructor.getConstructor(), super.createConstructor(constructor, providerFactory));
}
}
35 changes: 35 additions & 0 deletions metrics/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>shamrock-metrics</artifactId>
<groupId>org.jboss.shamrock</groupId>
<version>1.0.0.Alpha1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shamrock-metrics-deployment</artifactId>

<dependencies>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-weld-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-undertow-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-metrics-runtime</artifactId>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package org.jboss.shamrock.metrics;

import java.util.List;

import javax.inject.Inject;

import io.smallrye.metrics.MetricProducer;
import io.smallrye.metrics.MetricRegistries;
import io.smallrye.metrics.MetricsRequestHandler;
import io.smallrye.metrics.interceptors.CountedInterceptor;
import io.smallrye.metrics.interceptors.MeteredInterceptor;
import io.smallrye.metrics.interceptors.MetricNameFactory;
import io.smallrye.metrics.interceptors.MetricsBinding;
import io.smallrye.metrics.interceptors.MetricsInterceptor;
import io.smallrye.metrics.interceptors.TimedInterceptor;
import org.eclipse.microprofile.metrics.annotation.Counted;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.MethodInfo;
import org.jboss.shamrock.deployment.ArchiveContext;
import org.jboss.shamrock.deployment.ProcessorContext;
import org.jboss.shamrock.deployment.ResourceProcessor;
import org.jboss.shamrock.deployment.RuntimePriority;
import org.jboss.shamrock.deployment.ShamrockConfig;
import org.jboss.shamrock.deployment.codegen.BytecodeRecorder;
import org.jboss.shamrock.metrics.runtime.MetricsDeploymentTemplate;
import org.jboss.shamrock.metrics.runtime.MetricsServlet;
import org.jboss.shamrock.undertow.ServletData;
import org.jboss.shamrock.undertow.ServletDeployment;
import org.jboss.shamrock.weld.deployment.WeldDeployment;

//import io.smallrye.health.SmallRyeHealthReporter;

public class MetricsProcessor implements ResourceProcessor {


@Inject
private WeldDeployment weldDeployment;

@Inject
private ShamrockConfig config;

@Inject
private ServletDeployment servletDeployment;

@Override
public void process(ArchiveContext archiveContext, ProcessorContext processorContext) throws Exception {
System.err.println("PROCESSING METRICS");
ServletData servletData = new ServletData("metrics", MetricsServlet.class.getName());
servletData.getMapings().add(config.getConfig("metrics.path", "/metrics"));
servletDeployment.addServlet(servletData);

weldDeployment.addAdditionalBean(MetricProducer.class);
weldDeployment.addAdditionalBean(MetricNameFactory.class);
weldDeployment.addAdditionalBean(MetricRegistries.class);

weldDeployment.addAdditionalBean(MetricsInterceptor.class);
weldDeployment.addAdditionalBean(MeteredInterceptor.class);
weldDeployment.addAdditionalBean(CountedInterceptor.class);
weldDeployment.addAdditionalBean(TimedInterceptor.class);

//weldDeployment.addInterceptor(MetricsInterceptor.class);
//weldDeployment.addInterceptor(MeteredInterceptor.class);
//weldDeployment.addInterceptor(CountedInterceptor.class);
//weldDeployment.addInterceptor(TimedInterceptor.class);

processorContext.addReflectiveClass(Counted.class.getName());
processorContext.addReflectiveClass(MetricsBinding.class.getName());

weldDeployment.addAdditionalBean(MetricsRequestHandler.class);
weldDeployment.addAdditionalBean(MetricsServlet.class);

try (BytecodeRecorder recorder = processorContext.addStaticInitTask(RuntimePriority.WELD_DEPLOYMENT + 30)) {
MetricsDeploymentTemplate metrics = recorder.getRecordingProxy(MetricsDeploymentTemplate.class);

metrics.createRegistries();

Index index = archiveContext.getIndex();
List<AnnotationInstance> annos = index.getAnnotations(DotName.createSimple(Counted.class.getName()));

for (AnnotationInstance anno : annos) {
AnnotationTarget target = anno.target();

MethodInfo methodInfo = target.asMethod();
ClassInfo classInfo = methodInfo.declaringClass();

metrics.registerCounted(classInfo.name().toString(),
methodInfo.name().toString());

processorContext.addReflectiveClass(classInfo.name().toString());
}

}
try (BytecodeRecorder recorder = processorContext.addDeploymentTask(RuntimePriority.WELD_DEPLOYMENT + 30)) {
MetricsDeploymentTemplate metrics = recorder.getRecordingProxy(MetricsDeploymentTemplate.class);
metrics.registerBaseMetrics();
metrics.registerVendorMetrics();
}
}

@Override
public int getPriority() {
return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.shamrock.metrics.MetricsProcessor
18 changes: 18 additions & 0 deletions metrics/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>shamrock-parent</artifactId>
<groupId>org.jboss.shamrock</groupId>
<version>1.0.0.Alpha1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shamrock-metrics</artifactId>
<packaging>pom</packaging>
<modules>
<module>deployment</module>
<module>runtime</module>
</modules>
</project>
Loading

0 comments on commit 1cee801

Please sign in to comment.