Codahale Metrics: using @Timed metrics annotation in plain Java

后端 未结 8 973
南方客
南方客 2020-12-29 03:18

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,

8条回答
  •  攒了一身酷
    2020-12-29 03:35

    AOP is overkill and not appropriate for use of @timed, generally speaking.

    The default metrics registry writes @timed metrics to a ConcurrentHashMap and does not attach any meaningful listeners.

    DropWizard Bootstrap constructor:

    /**
     * Creates a new {@link Bootstrap} for the given application.
     * @param application a Dropwizard {@link Application}
     */
    public Bootstrap(Application application) {
        this.application = application;
        this.objectMapper = Jackson.newObjectMapper();
        this.bundles = Lists.newArrayList();
        this.configuredBundles = Lists.newArrayList();
        this.commands = Lists.newArrayList();
        this.validatorFactory = Validators.newValidatorFactory();
    
    
        // returns new ConcurrentHashMap(); 
        this.metricRegistry = new MetricRegistry(); 
    
    
        this.configurationSourceProvider = new FileConfigurationSourceProvider();
        this.classLoader = Thread.currentThread().getContextClassLoader();
        this.configurationFactoryFactory = new DefaultConfigurationFactoryFactory();
    }
    

    So you need to build/start/register the appropriate metric registry in order to see results.

    Here I use JMX:

    @Override
    public void initialize(Bootstrap bootstrap) {
        JmxReporter.forRegistry(bootstrap.getMetricRegistry()).build().start();
    }
    

    That's all you need to do.

    Here's an example of the output (run jconsole against your Java application/server to view JMX results):

提交回复
热议问题