java 向 Dropwizard 添加额外的指标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15635692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:20:23  来源:igfitidea点击:

Adding additional metrics to Dropwizard

javadropwizard

提问by johncowie

How do I go about adding custom metrics to the default stuff that Dropwizard provides on the admin port (8081)? I can't find anything in the documentation apart from adding health checks. I'd quite like to incorporate some stats retrieved from MongoDB, and would rather keep it with the admin resources on 8081 than create a custom metrics page on port 8080.

我如何将自定义指标添加到 Dropwizard 在管理端口 (8081) 上提供的默认内容中?除了添加健康检查之外,我在文档中找不到任何内容。我很想合并一些从 MongoDB 检索到的统计信息,并且宁愿将其保留在 8081 上的管理资源中,也不愿在端口 8080 上创建自定义指标页面。

采纳答案by Michael Fairley

Dropwizard's using the Metricslibrary for all of its metrics. Metric's getting started sectionhas everything you need to start adding your own.

Dropwizard 的所有指标都使用Metrics库。Metric 的入门部分包含您开始添加自己所需的一切。

回答by CAB

Here is an example. Every metric in that instance of the jvm is exposed via JMX. You can also register metric reporters that will do thing liks dump all metrics to logs on interval, or ship to graphite on an interval.

这是一个例子。该 jvm 实例中的每个指标都通过 JMX 公开。您还可以注册指标报告器,该报告器将执行诸如将所有指标按时间间隔转储到日志或按时间间隔发送到石墨之类的操作。

    //this creates or returns the metrics, basically every metric is only created once and registered in a registry
    private final Timer timerCanMakeHold = 
                Metrics.newTimer(MyClass.class, "METRICNAME", TimeUnit.MILLISECONDS, TimeUnit.SECONDS);

        final TimerContext timerContex = timerCanMakeHold.time();
        try{    
             doSomeWork()//this is what you are timing
        }finally{
            timerContex.stop();
        }

回答by MikeB

If you want your metrics to show up with the metrics servlet that's included in your Dropwizard project, you must use the same MetricRegistry object that the servlet used and register your metrics into it.

如果您希望您的指标与包含在 Dropwizard 项目中的指标 servlet 一起显示,您必须使用与 servlet 相同的 MetricRegistry 对象并将您的指标注册到其中。

You can get the correct MetricRegistry instance from the Environment in your application; or from the Bootstrap object that's passed to its initilaize method during startup.

您可以从应用程序的环境中获取正确的 MetricRegistry 实例;或从在启动期间传递给其 initilaize 方法的 Bootstrap 对象。

The Dropwizard documentation fails to mention the scope of the MetricRegistry and the objects it contains. It implies that you just create your own MetricRegistry. That'll work fine for the stand alone "getting started" application, but the document is about adding metrics to an existing Dropwizard application, not a new, standalone application.

Dropwizard 文档没有提到 MetricRegistry 的范围及其包含的对象。这意味着您只需创建自己的 MetricRegistry。这适用于独立的“入门”应用程序,但该文档是关于向现有 Dropwizard 应用程序添加指标,而不是新的独立应用程序。

回答by user3280180

I haven't tested it, but perhaps this could help:

我还没有测试过,但也许这会有所帮助:

    final Graphite graphite = new Graphite(new InetSocketAddress("graphite.url.example", 2003));
    MetricRegistry metrics = new MetricRegistry();
    GraphiteReporter reporter = GraphiteReporter.forRegistry(metrics)
                      .convertRatesTo(TimeUnit.SECONDS)
                      .convertDurationsTo(TimeUnit.MILLISECONDS)
                      .build(graphite);
    reporter.start(1, TimeUnit.SECONDS);
    Counter counter = metrics.counter("nameOfCounter");
    counter.inc();

For ivy you have to add this to your ivy.xml:

对于常春藤,您必须将其添加到您的 ivy.xml 中:

        <dependency org="io.dropwizard" name="dropwizard-metrics" rev="0.7.1"/>
        <dependency org="io.dropwizard" name="dropwizard-metrics-graphite" rev="0.7.1"/>

If you put this into your config.yml,

如果你把它放到你的 config.yml 中,

metrics:
  reporters:
    - type: console
      timeZone: UTC
      output: stdout
      durationUnit: milliseconds
      rateUnit: seconds
      frequency: 120 seconds
    - type: graphite
      host: localhost
      port: 9090
      prefix: test.prefix

you can also call the MetricsFactory in your run method:

您还可以在 run 方法中调用 MetricsFactory:

        MetricRegistry metrics = new MetricRegistry();
        MetricsFactory mfac = configuration.getMetricsFactory();
        mfac.configure(environment.lifecycle(), metrics);
        Counter counter = metrics.counter("nameOfCounter");
        counter.inc();
        counter.inc();
        counter.inc();
        counter.inc();
        counter.inc();

If you build you own socket listener, then you can see this line incoming every xxx seconds:

如果您构建自己的套接字侦听器,那么您可以看到此行每 xxx 秒传入一次:

test.prefix.nameOfCounter.count 5 1411562372