如何每X秒重置一次指标? [英] How to reset metrics every X seconds?

查看:197
本文介绍了如何每X秒重置一次指标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 DropWizard指标库来测量我的应用程序中的应用程序和jvm级别指标>.

I am trying to measure application and jvm level metrics on my application using DropWizard Metrics library.

下面是我的指标类,我在代码中使用它来增加/减少指标.我正在调用以下类的incrementdecrement方法来增加和减少指标.

Below is my metrics class which I am using across my code to increment/decrement the metrics. I am calling increment and decrement method of below class to increment and decrement metrics.

public class TestMetrics {
  private final MetricRegistry metricRegistry = new MetricRegistry();

  private static class Holder {
    private static final TestMetrics INSTANCE = new TestMetrics();
  }

  public static TestMetrics getInstance() {
    return Holder.INSTANCE;
  }

  private TestMetrics() {}

  public void increment(final Names... metricsName) {
    for (Names metricName : metricsName)
      metricRegistry.counter(name(TestMetrics.class, metricName.value())).inc();
  }

  public void decrement(final Names... metricsName) {
    for (Names metricName : metricsName)
      metricRegistry.counter(name(TestMetrics.class, metricName.value())).dec();
  }

  public MetricRegistry getMetricRegistry() {
    return metricRegistry;
  }

  public enum Names {
    // some more fields here
    INVALID_ID("invalid-id"), MESSAGE_DROPPED("drop-message");

    private final String value;

    private Names(String value) {
      this.value = value;
    }

    public String value() {
      return value;
    }
  };
}

这是我在需要的情况下使用以上TestMetrics类增加指标的方式.下面的方法被多个线程调用.

And here is how I am using above TestMetrics class to increment the metrics basis on the case where I need to. Below method is called by multiple threads.

  public void process(GenericRecord record) {
    // ... some other code here
    try {
      String clientId = String.valueOf(record.get("clientId"));
      String procId = String.valueOf(record.get("procId"));
      if (Strings.isNullOrEmpty(clientId) && Strings.isNullOrEmpty(procId)
          && !NumberUtils.isNumber(clientId)) {
        TestMetrics.getInstance().increment(Names.INVALID_ID,
            Names.MESSAGE_DROPPED);
        return;
      }
      // .. other code here

    } catch (Exception ex) {    
      TestMetrics.getInstance().increment(Names.MESSAGE_DROPPED);
    }
  }

现在我有另一个类,该类仅每30秒运行一次(我正在使用Quartz框架),在这里我要打印出所有指标及其计数.通常,我将每30秒将这些指标发送到其他系统,但现在我将其打印在此处.下面是我的操作方式.

Now I have another class which runs every 30 seconds only (I am using Quartz framework for that) from where I want to print out all the metrics and its count. In general, I will send these metrics every 30 seconds to some other system but for now I am printing it out here. Below is how I am doing it.

public class SendMetrics implements Job {

  @Override
  public void execute(final JobExecutionContext ctx) throws JobExecutionException {
    MetricRegistry metricsRegistry = TestMetrics.getInstance().getMetricRegistry();
    Map<String, Counter> counters = metricsRegistry.getCounters();
    for (Entry<String, Counter> counter : counters.entrySet()) {
      System.out.println(counter.getKey());
      System.out.println(counter.getValue().getCount());
    }
  }
}

现在我的问题是:我想每30秒重置一次所有指标计数.意思是当我的execute方法打印出指标时,它应该只打印30秒(针对所有指标)的指标,而不是在程序运行后的整个时间内打印指标.

Now my question is: I want to reset all my metrics count every 30 seconds. Meaning when my execute method prints out the metrics, it should print out the metrics for that 30 second only (for all the metrics) instead of printing for that whole duration from when the program is running.

有什么办法可以使我的所有指标仅计数30秒.计数最近30秒内发生的一切.

Is there any way that all my metrics should have count for 30 seconds only. Count of whatever has happened in last 30 seconds.

推荐答案

作为回答,因为它太长了:

As an answer because it is too long:

您想重置计数器.没有为此的API.链接的github问题中讨论了原因.本文介绍了一种可能的解决方法.您拥有自己的计数器,并照常使用它们-递增和递减.但是您无法重置它们.因此,添加新的Gauge值后,该值将在您要向您报告的计数器之后重设.当您要报告计数器值时,将调用GaugegetValue()方法.存储当前值后,该方法将随之减小计数器的值.这样可以将计数器有效地重置为0.因此,您可以拥有报告,也可以重置计数器. Step 1中对此进行了描述.

You want to reset the counters. There is no API for this. The reasons are discussed in the linked github issue. The article describes a possible workaround. You have your counters and use them as usual - incrementing and decrementing. But you can't reset them. So add new Gauge which value is following the counter you want to reset after it have reported to you. The getValue() method of the Gauge is called when you want to report the counter value. After storing the current value the method is decreasing the value of the counter with it. This effectively reset the counter to 0. So you have your report and also have the counter reset. This is described in Step 1.

Step 2添加了一个过滤器,该过滤器禁止报告实际计数器,因为您现在正在通过仪表报告.

Step 2 adds a filter that prohibits the actual counter to be reported because you are now reporting through the gauge.

这篇关于如何每X秒重置一次指标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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