Skip to content

Commit

Permalink
Fixed @timed for methods raising Error
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinaMoiseenko committed Sep 16, 2024
1 parent b410174 commit 82919c1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,19 @@ private Object processWithTimer(ProceedingJoinPoint pjp, Timed timed, String met
return ((CompletionStage<?>) pjp.proceed()).whenComplete(
(result, throwable) -> record(pjp, timed, metricName, sample, getExceptionTag(throwable)));
}
catch (Exception ex) {
record(pjp, timed, metricName, sample, ex.getClass().getSimpleName());
throw ex;
catch (Throwable e) {
record(pjp, timed, metricName, sample, e.getClass().getSimpleName());
throw e;
}
}

String exceptionClass = DEFAULT_EXCEPTION_TAG_VALUE;
try {
return pjp.proceed();
}
catch (Exception ex) {
exceptionClass = ex.getClass().getSimpleName();
throw ex;
catch (Throwable e) {
exceptionClass = e.getClass().getSimpleName();
throw e;
}
finally {
record(pjp, timed, metricName, sample, exceptionClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,36 @@ void timeMethodWhenCompletedExceptionally() {
.count()).isEqualTo(1);
}

@Test
void timeMethodWhenCompletedErroneously() {
MeterRegistry registry = new SimpleMeterRegistry();

AspectJProxyFactory pf = new AspectJProxyFactory(new AsyncTimedService());
pf.addAspect(new TimedAspect(registry));

AsyncTimedService service = pf.getProxy();

assertThat(registry.find("callRaisingError")
.tag("class", getClass().getName() + "$AsyncTimedService")
.tag("method", "callRaisingError")
.tag("extra", "tag")
.tag("exception", "Error")
.timer()).isNull();

CompletableFuture<?> completableFuture = service.callRaisingError();

assertThatThrownBy(completableFuture::join).isInstanceOf(CompletionException.class) ;

assertThat(registry.get("callRaisingError")
.tag("class", getClass().getName() + "$AsyncTimedService")
.tag("method", "callRaisingError")
.tag("extra", "tag")
.tag("exception", "Error")
.timer()
.count()).isEqualTo(1);
}


@Test
void timeMethodWithLongTaskTimerWhenCompleted() {
MeterRegistry registry = new SimpleMeterRegistry();
Expand Down Expand Up @@ -774,6 +804,12 @@ CompletableFuture<?> longCall(GuardedResult guardedResult) {
return supplyAsync(guardedResult::get);
}

@Timed(value = "callRaisingError", extraTags = { "extra", "tag" })
CompletableFuture<?> callRaisingError() {

return CompletableFuture.failedFuture(new Error());
}

}

static class GuardedResult {
Expand Down

0 comments on commit 82919c1

Please sign in to comment.