Rework @Timer annotation support

Rework existing `@Timer` annotation support to remove duplicate code
and offer general purpose utilities that can be used in future metrics
support.

See gh-23112
See gh-22217
This commit is contained in:
Phillip Webb
2021-04-09 17:41:48 -07:00
parent 3d7e5e3abd
commit 9f16491535
6 changed files with 87 additions and 49 deletions

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.actuate.metrics.web.method;
package org.springframework.boot.actuate.metrics.annotation;
import java.lang.reflect.Method;
import java.util.Set;
@@ -23,22 +23,21 @@ import io.micrometer.core.annotation.Timed;
import org.junit.jupiter.api.Test;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.method.HandlerMethod;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link HandlerMethodTimedAnnotations}.
* Tests for {@link TimedAnnotations}.
*
* @author Phillip Webb
*/
class HandlerMethodTimedAnnotationsTests {
class TimedAnnotationsTests {
@Test
void getWhenNoneReturnsEmptySet() {
Object bean = new None();
Method method = ReflectionUtils.findMethod(bean.getClass(), "handle");
Set<Timed> annotations = HandlerMethodTimedAnnotations.get(new HandlerMethod(bean, method));
Set<Timed> annotations = TimedAnnotations.get(method, bean.getClass());
assertThat(annotations).isEmpty();
}
@@ -46,7 +45,7 @@ class HandlerMethodTimedAnnotationsTests {
void getWhenOnMethodReturnsMethodAnnotations() {
Object bean = new OnMethod();
Method method = ReflectionUtils.findMethod(bean.getClass(), "handle");
Set<Timed> annotations = HandlerMethodTimedAnnotations.get(new HandlerMethod(bean, method));
Set<Timed> annotations = TimedAnnotations.get(method, bean.getClass());
assertThat(annotations).extracting(Timed::value).containsOnly("y", "z");
}
@@ -54,7 +53,7 @@ class HandlerMethodTimedAnnotationsTests {
void getWhenNonOnMethodReturnsBeanAnnotations() {
Object bean = new OnBean();
Method method = ReflectionUtils.findMethod(bean.getClass(), "handle");
Set<Timed> annotations = HandlerMethodTimedAnnotations.get(new HandlerMethod(bean, method));
Set<Timed> annotations = TimedAnnotations.get(method, bean.getClass());
assertThat(annotations).extracting(Timed::value).containsOnly("y", "z");
}