我能够通过创建一个注释@Foo
然后将此注释添加到我的函数的参数来解决这个问题:
@Timed("getFooContent")
public void getFooContent(@Foo Arg1 arg1, Arg2 arg2) {
//some code....
}
以下是我的定时配置类:
@Configuration
@SuppressWarnings("unchecked")
public class TimedConfiguration {
public static final String NOT_AVAILABLE = "N/A";
Function<ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint;
@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
tagsBasedOnJoinPoint = pjp ->
Tags.of("class", pjp.getStaticPart().getSignature().getDeclaringTypeName(),
"method", pjp.getStaticPart().getSignature().getName(),
"parameter_1", getArguments(pjp));
return new TimedAspect(registry, tagsBasedOnJoinPoint);
}
private String getArguments(ProceedingJoinPoint pjp) {
Object[] args = pjp.getArgs();
String className = pjp.getStaticPart().getSignature().getDeclaringTypeName();
if(className.contains("com.example.foo")) { //Resstricting to only certain packages starting with com.example.foo
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
Method method = methodSignature.getMethod();
Annotation[][] annotations = method.getParameterAnnotations();
int index = -1;
for(int i = 0; i < annotations.length; i++) {
Annotation[] annotationsArr = annotations[i];
for(Annotation annotation: annotationsArr) {
if(annotation.annotationType().getName().equals(Foo.class.getName())) {
index = i;
break;
}
}
}
if(index >= 0) {
List parameterValues = new ArrayList((List)args[index]);
if(CollectionUtils.isNotEmpty(parameterValues) && parameterValues.get(0) instanceof Byte) {
Collections.sort(parameterValues); //Sorting the paratemer values as per my use case
return String.valueOf(parameterValues.stream().collect(Collectors.toSet()));
}
}
}
return NOT_AVAILABLE;
}