2525import io .micrometer .core .instrument .Timer ;
2626import io .micrometer .core .instrument .distribution .DistributionStatisticConfig ;
2727import io .micrometer .core .instrument .distribution .pause .PauseDetector ;
28- import io .micrometer .core .instrument .search .MeterNotFoundException ;
2928import io .micrometer .core .instrument .simple .SimpleMeterRegistry ;
3029import org .aspectj .lang .ProceedingJoinPoint ;
30+ import org .junit .jupiter .api .Nested ;
3131import org .junit .jupiter .api .Test ;
3232import org .junit .jupiter .params .ParameterizedTest ;
3333import org .junit .jupiter .params .provider .EnumSource ;
@@ -73,7 +73,7 @@ void timeMethodWithSkipPredicate() {
7373
7474 service .call ();
7575
76- assertThat (registry .find ( "call" ). timer ()). isNull ();
76+ assertThat (registry .getMeters ()). isEmpty ();
7777 }
7878
7979 @ Test
@@ -91,8 +91,7 @@ void timeMethodWithLongTaskTimer() {
9191 .tag ("class" , getClass ().getName () + "$TimedService" )
9292 .tag ("method" , "longCall" )
9393 .tag ("extra" , "tag" )
94- .longTaskTimers ()
95- .size ()).isEqualTo (1 );
94+ .longTaskTimers ()).hasSize (1 );
9695 }
9796
9897 @ Test
@@ -106,13 +105,7 @@ void timeMethodFailure() {
106105
107106 service .call ();
108107
109- assertThatExceptionOfType (MeterNotFoundException .class ).isThrownBy (() -> {
110- failingRegistry .get ("call" )
111- .tag ("class" , getClass ().getName () + "$TimedService" )
112- .tag ("method" , "call" )
113- .tag ("extra" , "tag" )
114- .timer ();
115- });
108+ assertThat (failingRegistry .getMeters ()).isEmpty ();
116109 }
117110
118111 @ Test
@@ -126,13 +119,50 @@ void timeMethodFailureWithLongTaskTimer() {
126119
127120 service .longCall ();
128121
129- assertThatExceptionOfType (MeterNotFoundException .class ).isThrownBy (() -> {
130- failingRegistry .get ("longCall" )
131- .tag ("class" , getClass ().getName () + "$TimedService" )
132- .tag ("method" , "longCall" )
133- .tag ("extra" , "tag" )
134- .longTaskTimer ();
135- });
122+ assertThat (failingRegistry .getMeters ()).isEmpty ();
123+ }
124+
125+ @ Test
126+ void timeMethodWithError () {
127+ MeterRegistry registry = new SimpleMeterRegistry ();
128+
129+ AspectJProxyFactory pf = new AspectJProxyFactory (new TimedService ());
130+ pf .addAspect (new TimedAspect (registry ));
131+
132+ TimedService service = pf .getProxy ();
133+
134+ assertThat (registry .getMeters ()).isEmpty ();
135+
136+ assertThatThrownBy (service ::callRaisingError ).isInstanceOf (TestError .class );
137+
138+ assertThat (registry .get ("callRaisingError" )
139+ .tag ("class" , getClass ().getName () + "$TimedService" )
140+ .tag ("method" , "callRaisingError" )
141+ .tag ("extra" , "tag" )
142+ .tag ("exception" , "TestError" )
143+ .timer ()
144+ .count ()).isEqualTo (1 );
145+ }
146+
147+ @ Test
148+ void timeMethodWithErrorAndLongTaskTimer () {
149+ MeterRegistry registry = new SimpleMeterRegistry ();
150+
151+ AspectJProxyFactory pf = new AspectJProxyFactory (new TimedService ());
152+ pf .addAspect (new TimedAspect (registry ));
153+
154+ TimedService service = pf .getProxy ();
155+
156+ assertThat (registry .getMeters ()).isEmpty ();
157+
158+ assertThatThrownBy (service ::longCallRaisingError ).isInstanceOf (TestError .class );
159+
160+ assertThat (registry .get ("longCallRaisingError" )
161+ .tag ("class" , getClass ().getName () + "$TimedService" )
162+ .tag ("method" , "longCallRaisingError" )
163+ .tag ("extra" , "tag" )
164+ .longTaskTimer ()
165+ .activeTasks ()).isEqualTo (0 );
136166 }
137167
138168 @ Test
@@ -147,12 +177,7 @@ void timeMethodWhenCompleted() {
147177 GuardedResult guardedResult = new GuardedResult ();
148178 CompletableFuture <?> completableFuture = service .call (guardedResult );
149179
150- assertThat (registry .find ("call" )
151- .tag ("class" , getClass ().getName () + "$AsyncTimedService" )
152- .tag ("method" , "call" )
153- .tag ("extra" , "tag" )
154- .tag ("exception" , "none" )
155- .timer ()).isNull ();
180+ assertThat (registry .getMeters ()).isEmpty ();
156181
157182 guardedResult .complete ();
158183 completableFuture .join ();
@@ -178,21 +203,16 @@ void timeMethodWhenCompletedExceptionally() {
178203 GuardedResult guardedResult = new GuardedResult ();
179204 CompletableFuture <?> completableFuture = service .call (guardedResult );
180205
181- assertThat (registry .find ("call" )
182- .tag ("class" , getClass ().getName () + "$AsyncTimedService" )
183- .tag ("method" , "call" )
184- .tag ("extra" , "tag" )
185- .tag ("exception" , "NullPointerException" )
186- .timer ()).isNull ();
206+ assertThat (registry .getMeters ()).isEmpty ();
187207
188- guardedResult .complete (new NullPointerException ( ));
208+ guardedResult .complete (new IllegalStateException ( "simulated" ));
189209 catchThrowableOfType (completableFuture ::join , CompletionException .class );
190210
191211 assertThat (registry .get ("call" )
192212 .tag ("class" , getClass ().getName () + "$AsyncTimedService" )
193213 .tag ("method" , "call" )
194214 .tag ("extra" , "tag" )
195- .tag ("exception" , "NullPointerException " )
215+ .tag ("exception" , "IllegalStateException " )
196216 .timer ()
197217 .count ()).isEqualTo (1 );
198218 }
@@ -271,12 +291,7 @@ void timeMethodFailureWhenCompletedExceptionally() {
271291 guardedResult .complete ();
272292 completableFuture .join ();
273293
274- assertThatExceptionOfType (MeterNotFoundException .class ).isThrownBy (() -> failingRegistry .get ("call" )
275- .tag ("class" , getClass ().getName () + "$AsyncTimedService" )
276- .tag ("method" , "call" )
277- .tag ("extra" , "tag" )
278- .tag ("exception" , "none" )
279- .timer ());
294+ assertThat (failingRegistry .getMeters ()).isEmpty ();
280295 }
281296
282297 @ Test
@@ -293,13 +308,7 @@ void timeMethodFailureWithLongTaskTimerWhenCompleted() {
293308 guardedResult .complete ();
294309 completableFuture .join ();
295310
296- assertThatExceptionOfType (MeterNotFoundException .class ).isThrownBy (() -> {
297- failingRegistry .get ("longCall" )
298- .tag ("class" , getClass ().getName () + "$AsyncTimedService" )
299- .tag ("method" , "longCall" )
300- .tag ("extra" , "tag" )
301- .longTaskTimer ();
302- });
311+ assertThat (failingRegistry .getMeters ()).isEmpty ();
303312 }
304313
305314 @ Test
@@ -365,16 +374,11 @@ void timeClassFailure() {
365374
366375 service .call ();
367376
368- assertThatExceptionOfType (MeterNotFoundException .class ).isThrownBy (() -> {
369- failingRegistry .get ("call" )
370- .tag ("class" , "io.micrometer.core.aop.TimedAspectTest$TimedClass" )
371- .tag ("method" , "call" )
372- .tag ("extra" , "tag" )
373- .timer ();
374- });
377+ assertThat (failingRegistry .getMeters ()).isEmpty ();
375378 }
376379
377- static class MeterTagsTests {
380+ @ Nested
381+ class MeterTagsTests {
378382
379383 ValueResolver valueResolver = parameter -> "Value from myCustomTagValueResolver [" + parameter + "]" ;
380384
@@ -569,7 +573,7 @@ public void subMethod(@MeterTag("subTag") String foo) {
569573
570574 }
571575
572- private final class FailingMeterRegistry extends SimpleMeterRegistry {
576+ private static final class FailingMeterRegistry extends SimpleMeterRegistry {
573577
574578 private FailingMeterRegistry () {
575579 super ();
@@ -579,14 +583,14 @@ private FailingMeterRegistry() {
579583 @ Override
580584 protected Timer newTimer (@ NonNull Id id , @ NonNull DistributionStatisticConfig distributionStatisticConfig ,
581585 @ NonNull PauseDetector pauseDetector ) {
582- throw new RuntimeException ();
586+ throw new RuntimeException ("FailingMeterRegistry" );
583587 }
584588
585589 @ NonNull
586590 @ Override
587591 protected LongTaskTimer newLongTaskTimer (@ Nonnull Id id ,
588592 @ Nonnull DistributionStatisticConfig distributionStatisticConfig ) {
589- throw new RuntimeException ();
593+ throw new RuntimeException ("FailingMeterRegistry" );
590594 }
591595
592596 }
@@ -601,6 +605,16 @@ void call() {
601605 void longCall () {
602606 }
603607
608+ @ Timed (value = "callRaisingError" , extraTags = { "extra" , "tag" })
609+ void callRaisingError () {
610+ throw new TestError ();
611+ }
612+
613+ @ Timed (value = "longCallRaisingError" , extraTags = { "extra" , "tag" }, longTask = true )
614+ void longCallRaisingError () {
615+ throw new TestError ();
616+ }
617+
604618 }
605619
606620 static class AsyncTimedService {
@@ -675,4 +689,8 @@ public void call() {
675689
676690 }
677691
692+ static class TestError extends Error {
693+
694+ }
695+
678696}
0 commit comments