Skip to content

Commit

Permalink
Adding metrics support.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwcarman committed May 18, 2017
1 parent 90fd4b5 commit 78b1830
Show file tree
Hide file tree
Showing 53 changed files with 1,331 additions and 128 deletions.
14 changes: 14 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
<version>0.3.0-SNAPSHOT</version>
</dependency>

<!--
Metrics Service
-->
<dependency>
<groupId>org.microbule</groupId>
<artifactId>microbule-metrics-core</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>

<!--
Decorators
-->
Expand Down Expand Up @@ -113,6 +122,11 @@
<artifactId>microbule-logging-decorator</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.microbule</groupId>
<artifactId>microbule-metrics-decorator</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.microbule</groupId>
<artifactId>microbule-requestlog-decorator</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions cache/decorator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>org.microbule</groupId>
<artifactId>microbule-util</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.microbule</groupId>
<artifactId>microbule-cache-annotation</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,38 @@

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.GET;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;

import org.microbule.cache.annotation.Cacheable;
import org.microbule.util.jaxrs.AnnotationDrivenDynamicFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Provider
public class CacheDynamicFeature implements DynamicFeature {
//----------------------------------------------------------------------------------------------------------------------
// Fields
//----------------------------------------------------------------------------------------------------------------------
private static final Logger LOGGER = LoggerFactory.getLogger(CacheDynamicFeature.class);

private final Class<?> serviceInterface;
private final Set<Method> methods;

public class CacheDynamicFeature extends AnnotationDrivenDynamicFeature<Cacheable> {
//----------------------------------------------------------------------------------------------------------------------
// Constructors
// Fields
//----------------------------------------------------------------------------------------------------------------------

public CacheDynamicFeature(Class<?> serviceInterface, Set<Method> methods) {
this.serviceInterface = serviceInterface;
this.methods = methods;
}
private static final Logger LOGGER = LoggerFactory.getLogger(CacheDynamicFeature.class);

//----------------------------------------------------------------------------------------------------------------------
// DynamicFeature Implementation
// Other Methods
//----------------------------------------------------------------------------------------------------------------------

@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
try {
final Method resourceMethod = serviceInterface.getMethod(resourceInfo.getResourceMethod().getName(), resourceInfo.getResourceMethod().getParameterTypes());
if (methods.contains(resourceMethod)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Adding cache filter to method {}({})...", resourceMethod.getName(), Arrays.stream(resourceMethod.getParameterTypes()).map(Class::getSimpleName).collect(Collectors.joining(", ")));
}
context.register(new ContainerCacheFilter(resourceMethod.getAnnotation(Cacheable.class)));
protected void configure(Cacheable annotation, ResourceInfo resourceInfo, FeatureContext featureContext) {
final Method method = resourceInfo.getResourceMethod();
getAnnotation(method, GET.class).ifPresent(get -> {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Adding cache filter to method {}({})...", resourceInfo.getResourceMethod().getName(), Arrays.stream(method.getParameterTypes()).map(Class::getSimpleName).collect(Collectors.joining(", ")));
}
} catch (NoSuchMethodException e) {
LOGGER.warn("Method {}() is not found on service interface {}.", resourceInfo.getResourceMethod().getName(), serviceInterface.getCanonicalName());
}
featureContext.register(new ContainerCacheFilter(annotation));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@

package org.microbule.cache.decorator;

import java.lang.reflect.Method;
import java.util.Set;
import java.util.stream.Collectors;

import javax.inject.Named;
import javax.inject.Singleton;
import javax.ws.rs.GET;

import org.apache.commons.lang3.reflect.MethodUtils;
import org.microbule.cache.annotation.Cacheable;
import org.microbule.config.api.Config;
import org.microbule.spi.JaxrsServerDecorator;
import org.microbule.spi.JaxrsServiceDescriptor;
Expand All @@ -39,12 +32,9 @@ public class CacheServerDecorator implements JaxrsServerDecorator {
//----------------------------------------------------------------------------------------------------------------------

@Override
public void decorate(JaxrsServiceDescriptor object, Config config) {
final Set<Method> methods = MethodUtils.getMethodsListWithAnnotation(object.serviceInterface(), Cacheable.class).stream().filter(method -> method.isAnnotationPresent(GET.class)).collect(Collectors.toSet());
if (!methods.isEmpty()) {
object.addProvider(new CacheDynamicFeature(object.serviceInterface(), methods));
}
object.addProvider(new CacheInfoProvider());
public void decorate(JaxrsServiceDescriptor descriptor, Config config) {
descriptor.addProvider(new CacheDynamicFeature());
descriptor.addProvider(new CacheInfoProvider());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

<bean id="decorator" class="org.microbule.cache.decorator.CacheServerDecorator"/>

<service ref="decorator" interface="org.microbule.spi.JaxrsServerDecorator">
<service-properties>
<entry key="name" value="cache"/>
</service-properties>
</service>
<service ref="decorator" interface="org.microbule.spi.JaxrsServerDecorator"/>

</blueprint>
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class CacheResourceImpl implements CacheResource {
// CacheResource Implementation
//----------------------------------------------------------------------------------------------------------------------


@Override
public String getValueWithEtag() {
resourceState.setEntityTag(ENTITY_TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

<bean id="decorator" class="org.microbule.circuitbreaker.decorator.CircuitBreakerDecorator"/>

<service ref="decorator" interface="org.microbule.spi.JaxrsProxyDecorator">
<service-properties>
<entry key="name" value="circuitbreaker" />
</service-properties>
</service>
<service ref="decorator" interface="org.microbule.spi.JaxrsProxyDecorator" />

</blueprint>
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

<bean id="decorator" class="org.microbule.cors.decorator.CorsDecorator"/>

<service ref="decorator" interface="org.microbule.spi.JaxrsServerDecorator">
<service-properties>
<entry key="name" value="cors" />
</service-properties>
</service>
<service ref="decorator" interface="org.microbule.spi.JaxrsServerDecorator"/>

</blueprint>
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,18 @@

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

<reference id="errorMapperService" interface="org.microbule.errormap.api.ErrorMapperService" />
<reference id="errorMapperService" interface="org.microbule.errormap.api.ErrorMapperService"/>

<bean id="serverDecorator" class="org.microbule.errormap.decorator.ErrorMapperServerDecorator">
<argument ref="errorMapperService" />
<argument ref="errorMapperService"/>
</bean>

<service ref="serverDecorator" interface="org.microbule.spi.JaxrsServerDecorator">
<service-properties>
<entry key="name" value="errormap" />
</service-properties>
</service>
<service ref="serverDecorator" interface="org.microbule.spi.JaxrsServerDecorator"/>

<bean id="proxyDecorator" class="org.microbule.errormap.decorator.ErrorMapperProxyDecorator">
<argument ref="errorMapperService" />
<argument ref="errorMapperService"/>
</bean>

<service ref="proxyDecorator" interface="org.microbule.spi.JaxrsProxyDecorator">
<service-properties>
<entry key="name" value="errormap" />
</service-properties>
</service>
<service ref="proxyDecorator" interface="org.microbule.spi.JaxrsProxyDecorator"/>

</blueprint>
5 changes: 5 additions & 0 deletions example/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<artifactId>microbule-cache-annotation</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.microbule</groupId>
<artifactId>microbule-metrics-annotation</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.swagger.annotations.ApiParam;
import org.microbule.annotation.JaxrsService;
import org.microbule.cache.annotation.Cacheable;
import org.microbule.metrics.annotation.Timed;

@Path("/")
@Api(value = "/", produces = "application/json")
Expand All @@ -41,6 +42,7 @@ public interface HelloResource {
@Path("/{name}")
@Produces(MediaType.APPLICATION_JSON)
@GET
@Timed
@Cacheable
@ApiOperation(value = "Say Hello", notes = "Returns a greeting", response = HelloResponse.class)
HelloResponse sayHello(@ApiParam(value="name", required = true) @PathParam("name") @Size(min = 5, message = "Name must be at least 5 characters long.") String name);
Expand Down
11 changes: 10 additions & 1 deletion features/src/main/resources/features.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<feature version="${project.version}">microbule-tracer</feature>
<feature version="${project.version}">microbule-circuitbreaker</feature>
<feature version="${project.version}">microbule-gson</feature>
<feature version="${project.version}">microbule-metrics</feature>
</feature>

<!--
Expand Down Expand Up @@ -96,14 +97,22 @@
</feature>

<feature name="microbule-gson" version="${project.version}">

<bundle start-level="60">mvn:org.microbule/microbule-gson-api/${project.version}</bundle>
<bundle start-level="60">mvn:org.microbule/microbule-gson-spi/${project.version}</bundle>
<bundle start-level="60">mvn:org.microbule/microbule-gson-provider/${project.version}</bundle>
<bundle start-level="60">mvn:org.microbule/microbule-gson-core/${project.version}</bundle>
<bundle start-level="60">mvn:org.microbule/microbule-gson-decorator/${project.version}</bundle>
</feature>

<feature name="microbule-metrics" version="${project.version}">
<bundle dependency="true" start-level="60">mvn:io.dropwizard.metrics/metrics-core/${dropwizard.metrics.version}</bundle>
<bundle start-level="60">mvn:org.microbule/microbule-metrics-annotation/${project.version}</bundle>
<bundle start-level="60">mvn:org.microbule/microbule-metrics-api/${project.version}</bundle>
<bundle start-level="60">mvn:org.microbule/microbule-metrics-spi/${project.version}</bundle>
<bundle start-level="60">mvn:org.microbule/microbule-metrics-core/${project.version}</bundle>
<bundle start-level="60">mvn:org.microbule/microbule-metrics-decorator/${project.version}</bundle>
</feature>

<feature name="microbule-gzip" version="${project.version}">
<bundle start-level="60">mvn:org.microbule/microbule-gzip-decorator/${project.version}</bundle>
</feature>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,8 @@
<argument ref="gsonService" />
</bean>

<service ref="decorator" interface="org.microbule.spi.JaxrsServerDecorator">
<service-properties>
<entry key="name" value="gson" />
</service-properties>
</service>
<service ref="decorator" interface="org.microbule.spi.JaxrsServerDecorator"/>

<service ref="decorator" interface="org.microbule.spi.JaxrsProxyDecorator">
<service-properties>
<entry key="name" value="gson" />
</service-properties>
</service>
<service ref="decorator" interface="org.microbule.spi.JaxrsProxyDecorator"/>

</blueprint>
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,8 @@

<bean id="decorator" class="org.microbule.gzip.decorator.GzipDecorator"/>

<service ref="decorator" interface="org.microbule.spi.JaxrsServerDecorator">
<service-properties>
<entry key="name" value="gzip"/>
</service-properties>
</service>
<service ref="decorator" interface="org.microbule.spi.JaxrsServerDecorator"/>

<service ref="decorator" interface="org.microbule.spi.JaxrsProxyDecorator">
<service-properties>
<entry key="name" value="gzip"/>
</service-properties>
</service>
<service ref="decorator" interface="org.microbule.spi.JaxrsProxyDecorator"/>

</blueprint>
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,8 @@

<bean id="decorator" class="org.microbule.logging.decorator.LoggingDecorator"/>

<service ref="decorator" interface="org.microbule.spi.JaxrsServerDecorator">
<service-properties>
<entry key="name" value="logging"/>
</service-properties>
</service>
<service ref="decorator" interface="org.microbule.spi.JaxrsServerDecorator"/>

<service ref="decorator" interface="org.microbule.spi.JaxrsProxyDecorator">
<service-properties>
<entry key="name" value="logging"/>
</service-properties>
</service>
<service ref="decorator" interface="org.microbule.spi.JaxrsProxyDecorator"/>

</blueprint>
34 changes: 34 additions & 0 deletions metrics/annotation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ Copyright (c) 2017 The Microbule Authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->

<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.microbule</groupId>
<artifactId>microbule-metrics-parent</artifactId>
<version>0.3.0-SNAPSHOT</version>
</parent>

<artifactId>microbule-metrics-annotation</artifactId>
<name>Microbule :: Metrics :: Annotation</name>
<packaging>bundle</packaging>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.microbule.metrics.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static org.apache.commons.lang3.StringUtils.EMPTY;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Timed {
//----------------------------------------------------------------------------------------------------------------------
// Other Methods
//----------------------------------------------------------------------------------------------------------------------

String name() default EMPTY;

String strategy() default EMPTY;
}
Loading

0 comments on commit 78b1830

Please sign in to comment.