Codahale指标:在普通Java中使用@Timed指标注释 [英] Codahale Metrics: using @Timed metrics annotation in plain Java

查看:2340
本文介绍了Codahale指标:在普通Java中使用@Timed指标注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用codahale指标将指标添加到普通Java应用程序。我想使用@Timed注释,但我不清楚它使用哪个MetricRegistry,或者如何告诉它使用哪个MetricRegistry。该应用程序是一个简单的Java 8应用程序,使用Maven 3构建,没有Spring,没有Hibernate。

I am trying to add metrics to a plain Java application using codahale metrics. I'd like to use the @Timed annotation, but it is unclear to me which MetricRegistry it uses, or how to tell it which MetricRegistry to use. The application is a plain Java 8 application, built with Maven 3, no Spring, no Hibernate.

我找不到任何关于如何在dropwizard中实现@Timed的文档文档: https://dropwizard.github.io/metrics/3.1.0/manual/

I can not find any documentation on how to implement @Timed in the dropwizard documentation: https://dropwizard.github.io/metrics/3.1.0/manual/

我添加了这些依赖项:

<dependency>
  <groupId>io.dropwizard.metrics</groupId>
  <artifactId>metrics-core</artifactId>
  <version>3.1.0</version>
</dependency>
<dependency>
  <groupId>com.codahale.metrics</groupId>
  <artifactId>metrics-annotation</artifactId>
  <version>3.0.2</version>
</dependency>

当我使用对Timer的编程调用时,我可以获得报告,因为我知道使用了哪些MetricsRegistry:

When I use a programatic call to Timer, I can get reports because I know which MetricsRegistry is used:

static final MetricRegistry metrics = new MetricRegistry();
private void update() throws SQLException {
  Timer.Context time = metrics.timer("domainobject.update").time();
  try {
    [...]
  } finally {
    time.stop();
  }
}

但是当我使用更优雅的@Timed注释时,我不知道使用了哪个注册表,因此我无法创建一个记者,这意味着我无法获得报告的指标(我甚至不确定这实际上是否有任何作用):

But when I use the much more elegant @Timed annotation, I have no idea which registry is used, and therefore I can not create a reporter, which means I can not get the metrics reported (I'm not even sure if this actually does anything):

@Timed(name = "domainobject.update")
private void update() throws SQLException {
    [...]
}

请告知如何使@Timed和其他Metrics注释在常规Java中运行应用程序。

Please advise on how to make the @Timed and other Metrics annotations work in a regular Java application.

其他信息:我发现这个奇怪的原因是我添加了Lombok框架并且@ Slf4j注释确实有效。我在maven pom.xml中添加了Lombok作为依赖项:

Additional info: The reason I am finding this strange is that I have added the Lombok framework and the @Slf4j annotations do work. I added Lombok as a dependency in the maven pom.xml:

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.14.8</version>
</dependency>

我可以使用@ Sl4fj类注释向类添加记录器而不会使成员混乱变量:

And I can use the @Sl4fj class annotation to add a logger to the class without cluttering up the member variables:

@Slf4j
public class App {
  public void logsome(){
    log.info("Hello there");
  }
}

因此,如果仅通过添加依赖项就可以实现,我估计我只是缺少一个依赖项或配置来获得codahale @Timed注释工作,如上所述。

So if that's possible by just adding a dependency, I reckon I am just missing a dependency or configuration to get the codahale @Timed annotation work, as described above.

(顺便说一句,看看Lombok,它会让你的生活更轻松: http://projectlombok.org/

(by the way, check out Lombok, it will make your life easier: http://projectlombok.org/ )

推荐答案

长话短说,你不能使用 @Timed 没有某种AOP(无论是Spring AOP还是AspectJ)。

Long story short, you cannot use @Timed without some kind of AOP (be it Spring AOP or AspectJ).

一两个星期前,我还决定为我们的项目添加指标,并选择AspectJ来完成这项任务(主要是因为我过去使用它来达到类似的目的,因为它允许编译时编织Spring只允许运行代理通过代理。)

A week or two ago, I also decided to add metrics to our project and chose AspectJ for this task (mostly because I used it in the past for similar purpose and because it allows for compile-time weaving while Spring allows only runtime via proxies).

您应该能够在此处找到所有必要的信息和说明: https://github.com/astefanutti/metrics-aspectj

You should be able to find all the necessary information and instructions here: https://github.com/astefanutti/metrics-aspectj.

至于龙目岛,我猜他们使用内置的javac注释处理器:

As for Lombok, I guess they use built-in javac annotations processor:


另一个争论点是实现支持IDE集成的代码以及javac注释处理器。这两个项目Lombok都使用非公共API来完成他们的巫术。这意味着可能会破坏Project Lombok随后的IDE或JDK版本。

Another point of contention is the implementation of both the code supporting IDE integration as well as the javac annotation processor. Both of these pieces of Project Lombok make use of non-public APIs to accomplish their sorcery. This means that there is a risk that Project Lombok will be broken with subsequent IDE or JDK releases.

这篇关于Codahale指标:在普通Java中使用@Timed指标注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆