如何解决如何使用自定义@Timed拦截HTTP状态代码
UMTimed.java
@Component
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE,ElementType.TYPE,ElementType.METHOD})
@Inherited
public @interface UMTimed {
UMTimedConstants value() default UMTimedConstants.METRIC_TMR_DEFAULT;
double[] percentiles() default {};
boolean histogram() default false;
}
UMTimedAspect.java
@Component
@Aspect
@NonNullApi
public class UMTimedAspect {
private final MeterRegistry registry;
private final Function<ProceedingJoinPoint,Iterable<Tag>> tagsBasedOnJoinPoint;
public UMTimedAspect(MeterRegistry registry) {
this.registry = registry;
this.tagsBasedOnJoinPoint = pjp ->
Tags.of("class",pjp.getStaticPart().getSignature().getDeclaringTypeName(),"method",pjp.getStaticPart().getSignature().getName());
}
@Around("execution (@path.UMTimed * *.*(..))")
public Object timedMethod(ProceedingJoinPoint pjp) throws Throwable {
Method method = ((MethodSignature) pjp.getSignature()).getmethod();
UMTimed timed = method.getAnnotation(UMTimed.class);
final String metricName = timed.value().getMetricName();
Timer.Sample sample = Timer.start(registry);
String exceptionClass = "none";
// we fetch tags from the func args
Iterable<Tag> tags = getTags(timed.value(),method.getParameters(),pjp.getArgs());
try {
return pjp.proceed();
} catch (Exception ex) {
exceptionClass = ex.getClass().getSimpleName();
throw ex;
} finally {
sample.stop(Timer.builder(metricName)
.description(null)
.tags(EXCEPTION_TAG,exceptionClass)
.tags(tagsBasedOnJoinPoint.apply(pjp))
.tags(tags)
.publishpercentileHistogram(timed.histogram())
.publishpercentiles(timed.percentiles().length == 0 ? null : timed.percentiles())
.register(registry));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。