Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First shot at integrating mp-metrics. #3

Merged
merged 4 commits into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -119,4 +124,4 @@
</profile>
</profiles>

</project>
</project>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +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")
@Counted
public class TestResource {

@Counted
@GET
public String getTest() {
return "TEST";
Expand Down
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,92 @@
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.app.CounterImpl;
import io.smallrye.metrics.interceptors.CountedInterceptor;
import io.smallrye.metrics.interceptors.MeteredInterceptor;
import io.smallrye.metrics.interceptors.MetricNameFactory;
import io.smallrye.metrics.interceptors.MetricsInterceptor;
import io.smallrye.metrics.interceptors.TimedInterceptor;
import org.eclipse.microprofile.metrics.Metric;
import org.eclipse.microprofile.metrics.annotation.Counted;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
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);

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();
}

try (BytecodeRecorder recorder = processorContext.addDeploymentTask(RuntimePriority.WELD_DEPLOYMENT + 30 ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno, could this be moved to static instead of deployment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the answer is "yes"

MetricsDeploymentTemplate metrics = recorder.getRecordingProxy(MetricsDeploymentTemplate.class);

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

for (AnnotationInstance anno : annos) {
metrics.registerCounted(anno.target().toString());
}
}
}

@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>
46 changes: 46 additions & 0 deletions metrics/runtime/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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-runtime</artifactId>

<dependencies>

<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-metrics</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-core-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-undertow-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_4.0_spec</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jboss.shamrock.metrics.runtime;

import javax.enterprise.inject.spi.CDI;

import io.smallrye.metrics.MetricRegistries;
import io.smallrye.metrics.app.CounterImpl;
import org.eclipse.microprofile.metrics.Metric;
import org.eclipse.microprofile.metrics.MetricRegistry;

/**
* Created by bob on 7/30/18.
*/
public class MetricsDeploymentTemplate {

public void registerCounted(String name) {
System.err.println("register: " + name);
MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
registry.register(name, new CounterImpl());
}

public void createRegistries() {
System.err.println("creating registries");
MetricRegistries.get(MetricRegistry.Type.APPLICATION);
MetricRegistries.get(MetricRegistry.Type.BASE);
MetricRegistries.get(MetricRegistry.Type.VENDOR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.jboss.shamrock.metrics.runtime;

import java.io.IOException;
import java.util.Collections;
import java.util.stream.Stream;

import javax.inject.Inject;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.smallrye.metrics.MetricsRequestHandler;

/**
* Created by bob on 7/30/18.
*/
@WebServlet
public class MetricsServlet extends HttpServlet {

@Inject
private MetricsRequestHandler metricsHandler;

@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doGet(req, resp);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String requestPath = request.getRequestURI();
String method = request.getMethod();
Stream<String> acceptHeaders = Collections.list(request.getHeaders("Accept")).stream();

metricsHandler.handleRequest(requestPath, method, acceptHeaders, (status, message, headers) -> {
headers.forEach(response::addHeader);
response.setStatus(status);
response.getWriter().write(message);
});
}
}
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<jboss-servlet-api_4.0_spec.version>1.0.0.Final</jboss-servlet-api_4.0_spec.version>
<smallrye-config.version>1.3.1</smallrye-config.version>
<smallrye-health.version>1.0.1</smallrye-health.version>
<smallrye-metrics.version>1.1.0</smallrye-metrics.version>
<javax.inject.version>1</javax.inject.version>
<jboss-classfilewriter.version>1.2.1.Final</jboss-classfilewriter.version>
<jboss-invocation.version>1.5.1.Final</jboss-invocation.version>
Expand All @@ -43,6 +44,7 @@
<module>test-framework</module>
<module>weld</module>
<module>health</module>
<module>metrics</module>
</modules>
<build>
<pluginManagement>
Expand Down Expand Up @@ -94,6 +96,16 @@
<artifactId>shamrock-health-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-metrics-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-metrics-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-jaxrs-deployment</artifactId>
Expand Down Expand Up @@ -147,6 +159,11 @@
<artifactId>smallrye-health</artifactId>
<version>${smallrye-health.version}</version>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-metrics</artifactId>
<version>${smallrye-metrics.version}</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public void process(ArchiveContext archiveContext, ProcessorContext processorCon
for (Class<?> clazz : weldDeployment.getAdditionalBeans()) {
template.addClass(init, clazz);
}
for ( Class<?> clazz: weldDeployment.getInterceptors()) {
template.addInterceptor(init, clazz);
}
SeContainer weld = template.doBoot(init);
template.setupInjection(weld);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class WeldDeployment {

private final List<Class<?>> additionalBeans = new ArrayList<>();
private final List<Class<?>> interceptors = new ArrayList<>();

public void addAdditionalBean(Class<?> beanClass) {
additionalBeans.add(beanClass);
Expand All @@ -14,4 +15,13 @@ public void addAdditionalBean(Class<?> beanClass) {
List<Class<?>> getAdditionalBeans() {
return additionalBeans;
}

public void addInterceptor(Class<?> interceptorClass) {
this.interceptors.add( interceptorClass);
}

List<Class<?>> getInterceptors() {
return this.interceptors;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public void addClass(SeContainerInitializer initializer, Class<?> clazz) {
initializer.addBeanClasses(clazz);
}

public void addInterceptor(SeContainerInitializer initialize, Class<?> interceptorClass) {
initialize.enableInterceptors(interceptorClass);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SeContainerInitializer.enableInterceptors() only enables the interceptors for the synthetic bean archive. MetricsInterceptor and friends are enabled globally (for the application) using @Priority - so there's no need to enable them locally. In fact, this enablement is just ignored.

}

@ContextObject("weld.container")
public SeContainer doBoot( SeContainerInitializer initializer) throws Exception {
SeContainer container = initializer.initialize();
Expand Down