Add metrics to Arc example

This commit is contained in:
Stuart Douglas
2018-09-10 10:07:47 +10:00
parent e348afd57d
commit 01582d75f4
9 changed files with 95 additions and 15 deletions

View File

@@ -28,6 +28,16 @@
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-shared-library-example</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-metrics-deployment</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-openapi-deployment</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-json-p-provider</artifactId>

View File

@@ -0,0 +1,18 @@
package org.jboss.shamrock.example.metrics;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.eclipse.microprofile.metrics.annotation.Counted;
@Path("/metrics")
public class MetricsResource {
@GET
@Counted(monotonic = true, name = "a_counted_resource")
public String getTest() {
return "TEST";
}
}

View File

@@ -1,7 +1,5 @@
package org.jboss.shamrock.example.rest;
import java.net.URL;
import javax.json.Json;
import javax.json.JsonObject;
import javax.ws.rs.GET;
@@ -10,8 +8,6 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import io.reactivex.Single;
@Path("/test")

View File

@@ -0,0 +1,51 @@
package org.jboss.shamrock.example.test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.jboss.shamrock.junit.ShamrockTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(ShamrockTest.class)
public class MetricsTestCase {
@Test
public void testMetrics() throws Exception {
testCounted("0.0");
invokeResource();
testCounted("1.0");
}
private void testCounted(String val) throws IOException {
URL uri = new URL("http://localhost:8080/metrics");
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[100];
int r;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((r = in.read(buf)) > 0) {
out.write(buf, 0, r);
}
String output = new String(out.toByteArray());
Assert.assertTrue(output, output.contains("application:org_jboss_shamrock_example_metrics_metrics_resource_a_counted_resource " + val));
}
public void invokeResource() throws Exception {
URL uri = new URL("http://localhost:8080/rest/metrics");
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[100];
int r;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((r = in.read(buf)) > 0) {
out.write(buf, 0, r);
}
Assert.assertEquals("TEST", new String(out.toByteArray()));
}
}

View File

@@ -49,6 +49,7 @@ import org.jboss.protean.arc.Subclass;
import org.jboss.protean.arc.processor.ResourceOutput.Resource;
import org.jboss.protean.arc.processor.ResourceOutput.Resource.SpecialType;
import org.jboss.protean.gizmo.BytecodeCreator;
import org.jboss.protean.gizmo.CatchBlockCreator;
import org.jboss.protean.gizmo.ClassCreator;
import org.jboss.protean.gizmo.ClassOutput;
import org.jboss.protean.gizmo.ExceptionTable;
@@ -638,10 +639,10 @@ public class BeanGenerator extends AbstractGenerator {
InvocationContextImpl.class, Constructor.class, List.class, Supplier.class), constructorHandle, aroundConstructsHandle,
func.getInstance());
ExceptionTable tryCatch = create.addTryCatch();
BytecodeCreator exceptionCatch = tryCatch.addCatchClause(Exception.class);
CatchBlockCreator exceptionCatch = tryCatch.addCatchClause(Exception.class);
// throw new RuntimeException(e)
// TODO existing exception param
exceptionCatch.throwException(RuntimeException.class, "Error invoking aroundConstructs", exceptionCatch.getMethodParam(0));
exceptionCatch.throwException(RuntimeException.class, "Error invoking aroundConstructs", exceptionCatch.getCaughtException());
instanceHandle = create.invokeInterfaceMethod(MethodDescriptor.ofMethod(InvocationContext.class, "proceed", Object.class),
invocationContextHandle);
tryCatch.complete();
@@ -691,10 +692,10 @@ public class BeanGenerator extends AbstractGenerator {
instanceHandle, postConstructsHandle);
ExceptionTable tryCatch = create.addTryCatch();
BytecodeCreator exceptionCatch = tryCatch.addCatchClause(Exception.class);
CatchBlockCreator exceptionCatch = tryCatch.addCatchClause(Exception.class);
// throw new RuntimeException(e)
// TODO existing exception param
exceptionCatch.throwException(RuntimeException.class, "Error invoking postConstructs", exceptionCatch.getMethodParam(0));
exceptionCatch.throwException(RuntimeException.class, "Error invoking postConstructs", exceptionCatch.getCaughtException());
create.invokeInterfaceMethod(MethodDescriptor.ofMethod(InvocationContext.class, "proceed", Object.class), invocationContextHandle);
tryCatch.complete();
}

View File

@@ -32,6 +32,7 @@ import org.jboss.protean.arc.Reflections;
import org.jboss.protean.arc.Subclass;
import org.jboss.protean.arc.processor.ResourceOutput.Resource;
import org.jboss.protean.gizmo.BytecodeCreator;
import org.jboss.protean.gizmo.CatchBlockCreator;
import org.jboss.protean.gizmo.ClassCreator;
import org.jboss.protean.gizmo.DescriptorUtils;
import org.jboss.protean.gizmo.ExceptionTable;
@@ -245,9 +246,9 @@ public class SubclassGenerator extends AbstractGenerator {
// (java.lang.String) InvocationContextImpl.aroundInvoke(this, methods.get("m1"), params, interceptorChains.get("m1"), forward).proceed()
ExceptionTable tryCatch = forwardMethod.addTryCatch();
// catch (Exception e)
BytecodeCreator exception = tryCatch.addCatchClause(Exception.class);
CatchBlockCreator exception = tryCatch.addCatchClause(Exception.class);
// throw new RuntimeException(e)
exception.throwException(RuntimeException.class, "Error invoking subclass", exception.getThis());
exception.throwException(RuntimeException.class, "Error invoking subclass", exception.getCaughtException());
// InvocationContextImpl.aroundInvoke(this, methods.get("m1"), params, interceptorChains.get("m1"), forward)
ResultHandle methodIdHandle = forwardMethod.load(methodId);
ResultHandle interceptedMethodHandle = forwardMethod.invokeInterfaceMethod(MethodDescriptors.MAP_GET,
@@ -277,9 +278,9 @@ public class SubclassGenerator extends AbstractGenerator {
// try
ExceptionTable tryCatch = destroyMethod.addTryCatch();
// catch (Exception e)
BytecodeCreator exception = tryCatch.addCatchClause(Exception.class);
CatchBlockCreator exception = tryCatch.addCatchClause(Exception.class);
// throw new RuntimeException(e)
exception.throwException(RuntimeException.class, "Error destroying subclass", exception.getThis());
exception.throwException(RuntimeException.class, "Error destroying subclass", exception.getCaughtException());
// InvocationContextImpl.preDestroy(this,predestroys)
ResultHandle invocationContext = destroyMethod.invokeStaticMethod(
MethodDescriptor.ofMethod(InvocationContextImpl.class, "preDestroy", InvocationContextImpl.class, Object.class, List.class),

View File

@@ -238,7 +238,7 @@ public class BuildMojo extends AbstractMojo {
if (visitor != null) {
visitors.add(visitor);
}
}
}
out.putNextEntry(new ZipEntry(pathName));
if (visitors.isEmpty()) {
try (FileInputStream in = new FileInputStream(path.toFile())) {

View File

@@ -95,10 +95,14 @@ public class MetricsProcessor implements ResourceProcessor {
}
MethodInfo methodInfo = target.asMethod();
String name = methodInfo.name();
if(anno.value("name") != null) {
name = anno.value("name").asString();
}
ClassInfo classInfo = methodInfo.declaringClass();
metrics.registerCounted(classInfo.name().toString(),
methodInfo.name().toString());
name);
processorContext.addReflectiveClass(true, false, classInfo.name().toString());
}

View File

@@ -33,7 +33,6 @@ public class MetricsDeploymentTemplate {
*/
public void registerCounted(String topClassName, String elementName) {
MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
MetricResolver resolver = new MetricResolver();
String name = MetricRegistry.name(topClassName, elementName);
Metadata meta = new Metadata(name, MetricType.COUNTER);