yammer @Timed 将值保留为零 [英] yammer @Timed leaving values at zero

查看:23
本文介绍了yammer @Timed 将值保留为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在此处中描述的使用 yammer 计时注释的后续努力.

This is a follow-up to my struggle using yammer timing annotations as described here.

我的 spring 上下文文件只有:

My spring context file has simply:

<metrics:annotation-driven />

我有以下课程:

import com.yammer.metrics.annotation.ExceptionMetered;
import com.yammer.metrics.annotation.Metered;
import com.yammer.metrics.annotation.Timed;

...

@Component
public class GetSessionServlet extends HttpServlet {

  private final static Logger log = LoggerFactory.getLogger(GetSessionServlet.class);


  @Override
  public void init(final ServletConfig config) throws ServletException {
    super.init(config);
    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(
            this, config.getServletContext());
  }

  @Override
  @Timed(name = "get-session", rateUnit = TimeUnit.MILLISECONDS)
  @Metered
  @ExceptionMetered(name = "get-session-failures", rateUnit = TimeUnit.MILLISECONDS)
  public void doGet(final HttpServletRequest req,
          final HttpServletResponse resp) throws ServletException, IOException {
    final String sessionId = req.getParameter("sessionId");
    final String fields = req.getParameter("fields");
    final String format = req.getParameter("format");
    if (StringUtils.isEmpty(sessionId)) {
      resp.getWriter().write("sessionId parameter missing!\n");
      return;
    }
    ...
  }

我使用 mvn clean tomcat7:run 在本地运行我的应用程序,然后连接 jconsole.

I run my app locally with mvn clean tomcat7:run and then connect jconsole.

在 MBeans 选项卡中,我可以看到一个包含我的 GetSessionServlet 类的包名称的条目,其中包含三个子文件夹 doGet(用于 @Metered 数字)、get-session, 和 get-session-failures.

In the MBeans tab, I can see an entry with the package name of my GetSessionServlet class with three subfolders doGet (for the @Metered numbers), get-session, and get-session-failures.

然而,无论我调用我的 servlet 多少次,上面子文件夹中的所有值都保持为零.我错过了什么?此外,关于这些计量指标的任何文档比官方文档更详细文档将不胜感激.

However, no matter how many times I call my servlet, all the values in the sub-folders above remain at zero. What am I missing? Also, any documentation on these metered metrics that goes into more details than the official documentation will be greatly appreciated.

推荐答案

我想我已经成功了.

我注意到的第一件事是示例 spring 上下文文件的 schemaLocation 属性中的 URI:http://www.yammer.com/schema/metrics/metrics.xsd返回 404 错误.

The first thing I noticed was that the URI in the schemaLocation attribute of the sample spring context file: http://www.yammer.com/schema/metrics/metrics.xsd returns a 404 error.

我用谷歌搜索了这个问题,看看是否有人抱怨架构无法访问,并找到了这个线程,github.com/codahale/metrics/issues/322,其中用户评论说,metrics-spring 模块已从核心指标存储库并移至:github.com/ryantenney/metrics-spring.

I googled the issue to see if anyone had complained about the schema being unreachable and found this thread, github.com/codahale/metrics/issues/322, wherein a user comments that the metrics-spring module has been removed from the core metrics repository and moved to: github.com/ryantenney/metrics-spring.

按照该网站上的示例,我从我的 pom 中删除了旧的 metrics-spring 依赖项并添加了以下内容:

Following the example on that website, I stripped out the old metrics-spring dependency from my pom and added the following:

  <dependency>
      <groupId>com.ryantenney.metrics</groupId>
      <artifactId>metrics-spring</artifactId>
      <version>2.1.4</version>
  </dependency>

然后我更新了 spring-context.xml 文件中的 xmlns 声明:

I then updated the xmlns declaration in my spring-context.xml file:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:metrics="http://www.ryantenney.com/schema/metrics"
       xsi:schemaLocation="
           http://www.ryantenney.com/schema/metrics 
           http://www.ryantenney.com/schema/metrics/metrics.xsd 
           ...other schemas...">

添加了注解驱动的元素:

Added the annotation-driven element:

<metrics:annotation-driven proxy-target-class="true" />

我启用了 proxy-target-class 以使基于类的自动装配工作.

I enabled proxy-target-class to get class based autowiring working.

在我想要 Time 的 bean 中添加了 @Timed 注释和 @PostConstruct 方法,以使 ConsoleReporter 能够每 10 秒转储一次统计信息:

And in the bean I wanted to Time added the @Timed annotation and a @PostConstruct method to enable a ConsoleReporter to dump stats every 10 secs:

@PostConstruct
public void init() {
    ConsoleReporter.enable(10, TimeUnit.SECONDS);    
} 

@Timed
public void doService(...) {
    .... do stuff ....
}

然后我启动了我的 web 应用程序......并从 sax 解析器解析 spring context.xml 文件中得到一个令人讨厌的错误.

I then started up my webapp .... and got a nasty error from the sax parser parsing the spring context.xml file.

似乎 www.ryantenney.com/schema/metrics/metrics.xsd 404 也是如此!

It seems that www.ryantenney.com/schema/metrics/metrics.xsd 404s as well!

解决方法很简单,但是,我从 github 存储库下载了 xsd:https://github.com/ryantenney/metrics-spring/blob/master/src/main/resources/com/ryantenney/metrics/spring/config/metrics-3.0.xsd

The workaround for this was simple, however, I downloaded the xsd from the github repository: https://github.com/ryantenney/metrics-spring/blob/master/src/main/resources/com/ryantenney/metrics/spring/config/metrics-3.0.xsd

然后在我的 Maven 项目的 src/main/resources/META-INF 目录中添加以下文件:

and then in the src/main/resources/META-INF directory of my maven project added the following files:

META-INF/
    - spring.schemas
    - xsd/
         - metrics.xsd

其中metrics.xsd是从github下载的xsd文件,spring.schemas是一个文本文件,包含:

where metrics.xsd is the xsd file downloaded from github, and spring.schemas is a text file containing:

http\://www.ryantenney.com/schema/metrics/metrics.xsd=META-INF/xsd/metrics.xsd

spring.schemas 文件在启动时被 spring 读取,并告诉它从本地文件中拉下 xsd.

The spring.schemas file is read by spring at startup and tells it to pull down the xsd from the local file.

有了上述内容,我就能够成功启动我的应用程序并点击使用服务方法的端点.

With the above in place I was able to successfully fire up my app and hit the endpoint that used the service method.

看着控制台,我确实看到以下每 10 秒转储一次:

Watching the console I did indeed see the following dumped every 10 seconds:

com.sample.service.MyServiceClass:
doService:
    count = 8
    mean rate = 0.27 calls/s
    1-minute rate = 0.23 calls/s
    5-minute rate = 0.21 calls/s
    15-minute rate = 0.20 calls/s
    min = 0.13ms
    max = 19.59ms
    mean = 2.59ms
    stddev = 6.87ms
    median = 0.18ms

这篇关于yammer @Timed 将值保留为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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