Java 将dropwizards注释与Spring Boot一起使用
我打算使用提供的Java 将dropwizards注释与Spring Boot一起使用,java,spring-boot,metrics,codahale-metrics,spring-boot-actuator,Java,Spring Boot,Metrics,Codahale Metrics,Spring Boot Actuator,我打算使用提供的@Timed注释,而不是通过在每个感兴趣的方法中插入代码来进行测量。但这些指标没有显示任何相应的值: 这是我的代码,想法是将包含的SQL的执行时间放入度量中 @Component public class Foo { private JdbcTemplate jdbcTemplate; @Autowired public Metadata(JdbcTemplate jdbcTemplate) { this.jdbcTemplate =
@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支持的度量标注至少能够正常工作,我缺少了什么?(1.3.1在我的测试中)您可能想看看 此项目简化了dropwizard指标与Spring Boot项目的集成。
它将自动创建度量和代理bean,以使@Timed注释正常工作。我最终没有使用注释,只是在我想要插入的bean上实现了度量集。特别是对于计时器,使用计时器并不难
@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());
}
}
}
所有这些就绪后,SpringBoot做了正确的事情,并将度量添加到/metrics中
@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();
}