【问题标题】:Use dropwizards annotation with Spring Boot在 Spring Boot 中使用 dropwizards 注释
【发布时间】:2015-12-28 09:58:31
【问题描述】:

我打算使用提供的@Timed 注释,而不是通过将代码插入每个感兴趣的方法来进行测量。但指标没有显示任何对应的值:

这是我的代码,想法是将包含的 SQL 的执行时间放入指标中。

@Component
public class Foo {
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public Metadata(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Timed(name = "myapp.get.foo")
    public boolean getFoo(final String foo ) {
        String foo = jdbcTemplate.query( ...
    }
}

@Timed没有出现的问题可能是因为Spring Boot only supports Counter and Gauge

但是@Gauge@Metered@Counted 也不起作用。

为了至少使 Spring Boot 支持的那些指标注释工作,我缺少什么? (在我的测试中是 1.3.1)

【问题讨论】:

    标签: java spring-boot metrics codahale-metrics spring-boot-actuator


    【解决方案1】:

    你可能想看看 http://www.ryantenney.com/metrics-spring/

    该项目简化了将 dropwizard 指标集成到 Spring Boot 项目中。 它将自动创建指标和代理 bean 以使 @Timed 注释工作。

    【讨论】:

      【解决方案2】:

      我最终没有使用注释,而只是在我想要检测的 bean 上实现 MetricSet。特别是对于计时器,使用一个并不难。

      @Service
      public class MyBean implements MetricSet {
          Timer putTimer = new Timer();
      
          public void someService() {
             try(Context context = putTimer.time()) {
                ....
             }
          }
      
          @Override
          public Map<String, Metric> getMetrics() {
              Map<String,Metric> metrics=new HashMap<>();
              metrics.put("mymetricname",putTimer);
              return metrics;
          }
      }
      
      @Slf4j
      @Service
      public class MetricsContextListener implements ApplicationListener<ContextRefreshedEvent> {
      
          private final MetricRegistry metricRegistry;
      
          public MetricsContextListener(MetricRegistry metricRegistry) {
              this.metricRegistry = metricRegistry;
          }
      
          @Override
          public void onApplicationEvent(ContextRefreshedEvent event) {
              Map<String, MetricSet> beans = event.getApplicationContext().getBeansOfType(MetricSet.class);
              for(Map.Entry<String, MetricSet> e: beans.entrySet()) {
                  log.info("registering " + e.getKey() + " " + e.getValue().getClass().getName());
                  metricRegistry.register(e.getKey(), e.getValue());
              }
          }
      }
      

      您可能还想创建自己的 MetricRegistry。我们在创建一个 spring boot 时遇到了一些问题,但是我们的 spring 测试失败了,因为它丢失了。如果你自己创建一个,这个问题就会消失。只需将其添加到您的 @Configuration 类之一

      @Bean
      public MetricRegistry metricRegistry() {
          // explicitly register a metricRegistry so we can run our spring tests without relying on spring boot creating
          // one for us. Spring boot seems to do the right thing either way.
          // Used to register codahale metrics from MetricSet implementing beans.
          return new MetricRegistry();
      }
      

      所有这些都到位后,Spring boot 做正确的事并将指标添加到 /metrics。

      【讨论】:

        猜你喜欢
        • 2020-10-02
        • 2018-09-01
        • 2019-09-18
        • 2019-12-18
        • 1970-01-01
        • 2020-06-04
        • 1970-01-01
        • 2015-09-01
        • 1970-01-01
        相关资源
        最近更新 更多