我正在尝试将AspectJ用于匕首应用程序中的带注释的度量标准,没有构建问题,但是没有注入方面。
我尝试在基本模块中使用@Provides,但这没有帮助。
方面:
@Singleton
@Aspect
public class TimedAspect {
/*private final XYZ xyz;
@Inject
public TimedAspect(@Named(XYZ_CLIENT) XYZ xyz) {
this.xyz = xyz;
}*/
@Setter
protected XYZ xyz;
@Around(value = "execution(* com.myproject.myapp..*(..)) && @annotation(timed)", argNames = "metricName")
public Object profile(final ProceedingJoinPoint joinPoint, final Timed time) throws Throwable {
final Instant startTime = Instant.now();
PutMetricDataRequest metricRequest = new PutMetricDataRequest();
try {
return joinPoint.proceed();
} finally {
String metricName = timed.metricName();
metricRequest.setMetricData(Collections.singletonList(
new MetricDatum()
.withMetricName(metricName)
.withTimestamp(Date.from(startTime))
.withUnit(StandardUnit.Milliseconds)
.withValue(Double.valueOf(Duration.between(startTime, Instant.now()).toMillis()))
));
metricRequest.setNamespace("ABC");
xyz.putMetricData(metricRequest);
}
}
}
Annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR })
public @interface Timed {
String metricName() default "";
}
模块:
@Module
public class BasicModule {
@Singleton
@Provides
@Named(TIMED_ASPECT)
public static TimedAspect providesTimedAspect(@Named(XYZ_CLIENT) XYZ xyz) {
final TimedAspect timedAspect = Aspects.aspectOf(TimedAspect.class);
timedAspect.setXyz(xyz);
return timedAspect;
}
}
处理程序:
public class BoomEventHandler {
@Timed(metricName = "CheckLatency")
public Response handleRequest(A a, C c) {
//..doesSomething
}
}
该指标未记录。