Java 如何使用自定义@Timed截获HTTP状态代码
如何在AspectJ中获取HTTP状态代码 UMTimed.javaJava 如何使用自定义@Timed截获HTTP状态代码,java,spring,spring-aop,spring-micrometer,Java,Spring,Spring Aop,Spring Micrometer,如何在AspectJ中获取HTTP状态代码 UMTimed.java @Component @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD}) @Inherited public @interface UMTimed { UMTimedConstants value() default UMTimedConstant
@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));
}
}
@组件
@面貌
@非NullAPI
公共类UMTimedAspect{
私人最终计量登记处;
专用最终功能标记BasedonJoinPoint;
公共UMTimedAspect(计量注册){
this.registry=注册表;
this.tagsBasedOnJoinPoint=pjp->
Tags.of(“类”,pjp.getStaticPart().getSignature().getDeclaringTypeName(),
“方法”,pjp.getStaticPart().getSignature().getName();
}
@大约(“执行(@path.UMTimed**.*(..)”)
公共对象timedMethod(ProceedingJoinPoint pjp)抛出可丢弃的{
方法Method=((MethodSignature)pjp.getSignature()).getMethod();
UMTimed=method.getAnnotation(UMTimed.class);
最后一个字符串metricName=timed.value().getMetricName();
Timer.Sample=Timer.start(注册表);
字符串exceptionClass=“无”;
//我们从func参数中获取标记
Iterable tags=getTags(timed.value()、method.getParameters()、pjp.getArgs());
试一试{
返回pjp.procedure();
}捕获(例外情况除外){
exceptionClass=ex.getClass().getSimpleName();
掷骰子;
}最后{
sample.stop(Timer.builder)(metricName)
.说明(空)
.tags(异常标记,异常类)
.tags(tagsBasedOnJoinPoint.apply(pjp))
.标签(标签)
.publishPercentileHistogram(timed.histogram())
.publishPercentiles(timed.percentiles().length==0?null:timed.percentiles())
.注册(登记处);
}
}
只是好奇你为什么要这样做?有一个过滤器
机制,它已经包装了HTTP请求,似乎更适合添加这种东西。