spring-boot - 如何在用于任意方法名称的@Timed 注释中包含方法参数

标签 spring-boot prometheus micrometer spring-micrometer prometheus-java

在我的应用程序中,我有一个用例,我必须通过提供的参数值来监视方法。我必须将指标公开给 Prometheus 端点。但是,该函数是一个通用函数,被许多不同的类使用。我正在尝试获取方法参数中传递给@Timed 的值,以便根据传递的参数值区分此函数将表现出的不同行为。

我尝试使用@Timed 注释,但无法让@Timed 注释将函数参数作为度量标准公开给Prometheus。

@Timed("getFooContent")
public void getFooContent(Arg1 arg1, Arg2 arg2) {
    //some code.... 
}

最佳答案

我能够通过创建注释 @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;
  }

关于spring-boot - 如何在用于任意方法名称的@Timed 注释中包含方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57244570/

相关文章:

java - 在 Spring Boot 中模拟多个 bean

kubernetes - 如何编辑 Helm chart 的配置?

kubernetes - 有没有办法使用 prometheus 监控 kube cron 作业

java - LoggingMeterRegistry 计数器未显示预期计数

java - 通量发生器 - 当没有可用数据时如何处理?

javascript - 如何使用JavaScript将文件作为多部分文件上传到后端?

java.lang.IllegalArgumentException : Jetty ALPN/NPN has not been properly configured 异常

grafana - 将 PM2 集群统计信息导出到 Prometheus

spring-boot - 如何使用 Spring Cloud Stream 4.x 生产者和消费者检测 Spring Boot 3.x 以关联记录器中的跟踪信息

metrics - Spring Boot 执行器/千分尺指标禁用某些功能