如何使用自定义@Timed拦截HTTP状态代码

问题描述

如何在AspectJ中获取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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...