使用Dropwizard指标的Grails中的注释不起作用

时间:2016-03-01 08:43:43

标签: grails aop dropwizard metrics graphite

我正在为带有注释的grails应用程序配置dropwizard指标。使用metrics-aspectj插件(https://github.com/astefanutti/metrics-aspectj

我在`BuildConfig.groovy'中有以下内容:

compile 'io.astefanutti.metrics.aspectj:metrics-aspectj:1.1.0'
compile 'io.dropwizard.metrics:metrics-core:3.1.0'
compile 'io.dropwizard.metrics:metrics-graphite:3.1.2'
compile 'io.dropwizard.metrics:metrics-annotation:3.1.2'

我试图将数据发布到Graphite服务器,在本地运行。 我用石墨记者配置了如下控制器。当我运行应用程序时它没有报告任何内容,我试图找出我出错的地方,或者请让我知道,如果有另一种方法(例如使用spring AOP)

import com.codahale.metrics.ConsoleReporter
import com.codahale.metrics.MetricFilter
import com.codahale.metrics.MetricRegistry
import com.codahale.metrics.SharedMetricRegistries
import com.codahale.metrics.annotation.Metered
import com.codahale.metrics.annotation.Timed
import com.codahale.metrics.graphite.Graphite
import com.codahale.metrics.graphite.GraphiteReporter
import io.astefanutti.metrics.aspectj.Metrics

import java.util.concurrent.TimeUnit

@Metrics(registry = "graphiteregistry2")
class GlassdoorController {


    final MetricRegistry registry = new MetricRegistry();

    GlassdoorController() {

        final Graphite graphite = new Graphite(new InetSocketAddress("127.0.0.1", 2003));

        final GraphiteReporter reporter = GraphiteReporter.forRegistry(registry)
                .prefixedWith("grails.example.com")
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .filter(MetricFilter.ALL)
                .build(graphite);
        reporter.start(1, TimeUnit.SECONDS);

        ConsoleReporter reporter1 = ConsoleReporter.forRegistry(registry)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
        reporter1.start(5, TimeUnit.SECONDS)

        SharedMetricRegistries.add("graphiteregistry2",registry);
        log.info(SharedMetricRegistries.getOrCreate("graphiteregistry2"));
    }

    @Metered(name  = "reviewspage")
    @Timed(name = "reviewspagetimed")
    def reviews() {

        //business logic

    }

1 个答案:

答案 0 :(得分:0)

看着你的代码,我发现了一个区别:

reporter.start(1, TimeUnit.SECONDS);
// ...
reporter1.start(5, TimeUnit.SECONDS)

这意味着您的Graphite记者会每秒触发一次,但控制台记者每隔5秒触发一次。可能你的测试程序在5秒结束之前就存在了。因此,您可以向Thread.sleep(6000)方法添加main()内容,或者减少控制台报告器的报告间隔以匹配Graphite报告器。我试过了,它有效。 ;-)

06.03.16 12:22:59 ==============================================================

-- Meters ----------------------------------------------------------------------
de.scrum_master.app.GlassdoorController.reviewspage
             count = 1
         mean rate = 0,20 events/second
     1-minute rate = 0,20 events/second
     5-minute rate = 0,20 events/second
    15-minute rate = 0,20 events/second

-- Timers ----------------------------------------------------------------------
de.scrum_master.app.GlassdoorController.reviewspagetimed
             count = 1
         mean rate = 0,20 calls/second
     1-minute rate = 0,20 calls/second
     5-minute rate = 0,20 calls/second
    15-minute rate = 0,20 calls/second
               min = 0,01 milliseconds
               max = 0,01 milliseconds
              mean = 0,01 milliseconds
            stddev = 0,00 milliseconds
            median = 0,01 milliseconds
              75% <= 0,01 milliseconds
              95% <= 0,01 milliseconds
              98% <= 0,01 milliseconds
              99% <= 0,01 milliseconds
            99.9% <= 0,01 milliseconds