Spring boot 如何在用于任意方法名称的@Timed注释中包含方法参数
在我的应用程序中,我有一个用例,我必须通过提供的参数值来监视一个方法。我必须向普罗米修斯端点公开这些指标。但是,该函数是一个常见函数,由许多不同的类使用。我试图将方法参数中传递的值传递给@Timed,以便根据传递的参数值区分此函数将显示的不同行为 我尝试使用@Timed annotation,但无法获得@Timed annotation将函数参数作为度量向普罗米修斯公开Spring boot 如何在用于任意方法名称的@Timed注释中包含方法参数,spring-boot,prometheus,micrometer,spring-micrometer,prometheus-java,Spring Boot,Prometheus,Micrometer,Spring Micrometer,Prometheus Java,在我的应用程序中,我有一个用例,我必须通过提供的参数值来监视一个方法。我必须向普罗米修斯端点公开这些指标。但是,该函数是一个常见函数,由许多不同的类使用。我试图将方法参数中传递的值传递给@Timed,以便根据传递的参数值区分此函数将显示的不同行为 我尝试使用@Timed annotation,但无法获得@Timed annotation将函数参数作为度量向普罗米修斯公开 @Timed("getFooContent") public void getFooContent(Arg1 arg1, Ar
@Timed("getFooContent")
public void getFooContent(Arg1 arg1, Arg2 arg2) {
//some code....
}
仅使用注释无法将参数包含在计时器的标记中。测微计为简单用例提供注释,并建议在需要更复杂的东西时使用编程方法 您应该在计时器上使用
记录
方法,并将代码包装在该计时器中
registry.timer("myclass.getFooContent", Tags.of("arg1", arg1)).record(() -> {
//some code...
})
在您的配置类中添加下面的bean,然后重试
@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
return new TimedAspect(registry);
}
用@enableAspectProxy注释配置类
请通过此链接阅读我可以通过创建注释
@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;
}
@配置
@抑制警告(“未选中”)
公共类时间配置{
公共静态最终字符串不可用=“N/A”;
函数标记BasedonJoinPoint;
@豆子
公共TimedAspect TimedAspect(计量注册表){
tagsBasedOnJoinPoint=pjp->
Tags.of(“类”,pjp.getStaticPart().getSignature().getDeclaringTypeName(),
“方法”,pjp.getStaticPart().getSignature().getName(),
“参数_1”,getArguments(pjp));
返回新的TimedAspect(注册表,标记BasedOnJoinPoint);
}
私有字符串getArguments(ProceedingJoinPoint pjp){
对象[]args=pjp.getArgs();
字符串className=pjp.getStaticPart().getSignature().getDeclaringTypeName();
if(className.contains(“com.example.foo”){//restricting仅限于以com.example.foo开头的某些包
MethodSignature MethodSignature=(MethodSignature)pjp.getSignature();
Method=methodSignature.getMethod();
Annotation[]annotations=method.getParameterAnnotations();
int指数=-1;
for(int i=0;i=0){
列表参数值=新的ArrayList((列表)参数[index]);
if(CollectionUtils.isNotEmpty(parameterValues)&¶meterValues.get(0)instanceof Byte){
Collections.sort(parameterValues);//根据我的用例对paratemer值进行排序
返回String.valueOf(parameterValues.stream().collect(Collectors.toSet());
}
}
}
返回不可用;
}
谢谢!还有其他库可以实现此功能吗?谢谢分享。我可以通过创建注释“@Foo”来解决这个问题