-
Notifications
You must be signed in to change notification settings - Fork 997
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement @scheduled timing, changed record -> recordThrowable in Tim…
…er, Gradle 4.0
- Loading branch information
1 parent
c39fdfa
commit 2b88f69
Showing
11 changed files
with
153 additions
and
49 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Mon Apr 10 14:49:12 CDT 2017 | ||
#Thu May 18 12:01:02 CDT 2017 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-milestone-2-bin.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
src/main/java/org/springframework/metrics/instrument/MetricException.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/main/java/org/springframework/metrics/instrument/ThrowableCallable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.springframework.metrics.instrument; | ||
|
||
@FunctionalInterface | ||
public interface ThrowableCallable<V> { | ||
/** | ||
* Computes a result, or throws an exception if unable to do so. | ||
* | ||
* @return computed result | ||
* @throws Throwable if unable to compute a result | ||
*/ | ||
V call() throws Throwable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ public interface Timer extends Meter { | |
* @param f Function to execute and measure the execution time. | ||
* @return The return value of `f`. | ||
*/ | ||
<T> T record(Callable<T> f) throws MetricException; | ||
<T> T recordThrowable(ThrowableCallable<T> f) throws Throwable; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jkschneider
Author
Contributor
|
||
|
||
/** | ||
* Executes the runnable `f` and records the time taken. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/org/springframework/metrics/instrument/scheduling/MetricsSchedulingAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.springframework.metrics.instrument.scheduling; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.metrics.instrument.MeterRegistry; | ||
import org.springframework.metrics.instrument.annotation.Timed; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
@Aspect | ||
public class MetricsSchedulingAspect { | ||
protected final Log logger = LogFactory.getLog(MetricsSchedulingAspect.class); | ||
|
||
private final MeterRegistry registry; | ||
|
||
public MetricsSchedulingAspect(MeterRegistry registry) { | ||
this.registry = registry; | ||
} | ||
|
||
@Around("execution (@org.springframework.scheduling.annotation.Scheduled * *.*(..))") | ||
public Object timeScheduledOperation(ProceedingJoinPoint pjp) throws Throwable { | ||
Method method = ((MethodSignature) pjp.getSignature()).getMethod(); | ||
|
||
String signature = pjp.getSignature().toShortString(); | ||
|
||
if (method.getDeclaringClass().isInterface()) { | ||
try { | ||
method = pjp.getTarget().getClass().getDeclaredMethod(pjp.getSignature().getName(), | ||
method.getParameterTypes()); | ||
} catch (final SecurityException | NoSuchMethodException e) { | ||
logger.warn("Unable to perform metrics timing on " + signature, e); | ||
return pjp.proceed(); | ||
} | ||
} | ||
|
||
Timed timed = method.getAnnotation(Timed.class); | ||
|
||
if (timed == null) { | ||
logger.debug("Skipping metrics timing on " + signature + ": no @Timed annotation is present on the method"); | ||
return pjp.proceed(); | ||
} | ||
|
||
if (timed.value().isEmpty()) { | ||
logger.warn("Unable to perform metrics timing on " + signature + ": @Timed annotation must have a value used to name the metric"); | ||
return pjp.proceed(); | ||
} | ||
|
||
return registry.timer(timed.value()).recordThrowable(pjp::proceed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...t/java/org/springframework/metrics/instrument/scheduling/MetricsSchedulingAspectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.springframework.metrics.instrument.scheduling; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.metrics.boot.EnableMetrics; | ||
import org.springframework.metrics.instrument.MeterRegistry; | ||
import org.springframework.metrics.instrument.Timer; | ||
import org.springframework.metrics.instrument.annotation.Timed; | ||
import org.springframework.metrics.instrument.simple.SimpleMeterRegistry; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest | ||
class MetricsSchedulingAspectTest { | ||
|
||
@Autowired | ||
MeterRegistry registry; | ||
|
||
@Test | ||
void scheduledIsInstrumented() { | ||
assertThat(registry.findMeter(Timer.class, "beeper")) | ||
.containsInstanceOf(Timer.class) | ||
.hasValueSatisfying(t -> assertThat(t.count()).isEqualTo(1)); | ||
} | ||
|
||
@SpringBootApplication | ||
@EnableMetrics | ||
@EnableScheduling | ||
static class MetricsApp { | ||
@Bean | ||
MeterRegistry registry() { | ||
return new SimpleMeterRegistry(); | ||
} | ||
|
||
@Timed("beeper") | ||
@Scheduled(fixedRate = 1000) | ||
void beep1() { | ||
System.out.println("beep"); | ||
} | ||
|
||
@Timed // not instrumented because @Timed lacks a metric name | ||
@Scheduled(fixedRate = 1000) | ||
void beep2() { | ||
System.out.println("beep"); | ||
} | ||
|
||
@Scheduled(fixedRate = 1000) // not instrumented because it isn't @Timed | ||
void beep3() { | ||
System.out.println("beep"); | ||
} | ||
} | ||
} |
Why the change? Aren't unchecked exceptions preferable? Also the method took me a moment to find since I was expecting
record()
orrecordCallable
This causes me to catch a throwable or rethrow it when my code being measured has no actual exceptions.