Skip to content

Commit

Permalink
Get Timed annotation from proxy target when failed (micrometer-metric…
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye authored and shakuzen committed Mar 6, 2019
1 parent 2825522 commit 672466d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@
import java.lang.reflect.Method;
import java.util.function.Function;


/**
* AspectJ aspect for intercepting types or method annotated with @Timed.
* AspectJ aspect for intercepting types or methods annotated with {@link Timed @Timed}.
*
* @author David J. M. Karlsen
* @author Jon Schneider
* @author Johnny Lim
* @since 1.0.0
*/
@Aspect
@NonNullApi
@Incubating(since = "1.0.0")
public class TimedAspect {
private final MeterRegistry registry;
private final Function<ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinpoint;
private final Function<ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint;

public TimedAspect(MeterRegistry registry) {
this(registry, pjp ->
Expand All @@ -51,15 +52,19 @@ public TimedAspect(MeterRegistry registry) {
);
}

public TimedAspect(MeterRegistry registry, Function<ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinpoint) {
public TimedAspect(MeterRegistry registry, Function<ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint) {
this.registry = registry;
this.tagsBasedOnJoinpoint = tagsBasedOnJoinpoint;
this.tagsBasedOnJoinPoint = tagsBasedOnJoinPoint;
}

@Around("execution (@io.micrometer.core.annotation.Timed * *.*(..))")
public Object timedMethod(ProceedingJoinPoint pjp) throws Throwable {
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
Timed timed = method.getAnnotation(Timed.class);
if (timed == null) {
method = pjp.getTarget().getClass().getMethod(method.getName(), method.getParameterTypes());
timed = method.getAnnotation(Timed.class);
}

if (timed.value().isEmpty()) {
return pjp.proceed();
Expand All @@ -73,7 +78,7 @@ public Object timedMethod(ProceedingJoinPoint pjp) throws Throwable {
sample.stop(Timer.builder(timed.value())
.description(timed.description().isEmpty() ? null : timed.description())
.tags(timed.extraTags())
.tags(tagsBasedOnJoinpoint.apply(pjp))
.tags(tagsBasedOnJoinPoint.apply(pjp))
.publishPercentileHistogram(timed.histogram())
.publishPercentiles(timed.percentiles().length == 0 ? null : timed.percentiles())
.register(registry));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link TimedAspect}.
*
* @author Jon Schneider
* @author Johnny Lim
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TimedAspectTest.TestAspectConfig.class)
public class TimedAspectTest {
Expand All @@ -41,12 +47,21 @@ public class TimedAspectTest {
@Autowired
private MeterRegistry registry;

@Autowired
private SomeService someService;

@Test
public void serviceIsTimed() {
service.timeMe();
assertThat(registry.get("something").timer().count()).isEqualTo(1);
}

@Test
public void timedWhenImplementingInterfaceShouldWork() {
assertThat(someService.doService("Hello, world!")).isEqualTo("Done: Hello, world!");
assertThat(registry.get("some").timer().count()).isEqualTo(1);
}

@Configuration
@EnableAspectJAutoProxy
@Import(TimedService.class)
Expand All @@ -60,6 +75,11 @@ public SimpleMeterRegistry simpleMeterRegistry() {
public TimedAspect micrometerAspect(MeterRegistry meterRegistry) {
return new TimedAspect(meterRegistry);
}

@Bean
public DefaultSomeService someService() {
return new DefaultSomeService();
}
}

@Service
Expand All @@ -69,4 +89,20 @@ public String timeMe() {
return "hello world";
}
}

interface SomeService {

String doService(String data);

}

static class DefaultSomeService implements SomeService {

@Timed("some")
@Override
public String doService(String data) {
return "Done: " + data;
}

}
}

0 comments on commit 672466d

Please sign in to comment.