Need full example on how to gather metrics and send to Graphite server

147 views
Skip to first unread message

lbecke...@gmail.com

unread,
Nov 20, 2015, 4:39:01 PM11/20/15
to dropwizard-user
I'm really struggling to understand and implement metrics in my DropWizard application. This is what I want to do:
  1. In my config.yml, there's a boolean to either turn metrics reporting on or off.
  2. If metrics is turned on, then in my SomeResource class, I would like to:
    1. Record how many times a method in SomeResource is called.
    2. Record how many times a method in SomeResource returns "false."
    3. Record the runtime of a method in SomeResource.
  3. If metrics is turned off, then I do not want to record anything (obviously)!
  4. I want these metrics to be sent to a Graphite server.
I do not know how to do this. I have been unsuccessful in specifying a graphite metrics reporter in my config.yml. I have documented my problems in this post and this post. My workaround has been to build a class (MyMetrics) that initializes a GraphiteReporter with the MetricRegistry. I tried using the MetricRegistry from env.metrics(), but I received errors stating that data was unable to be sent to my graphite server. When I initialized my own MetricRegistry, I successfully sent metrics to my Graphite server.

Currently, I have the following setup: The MetricRegistry in MyMetrics is given as an instance variable to SomeResource. SomeResource then initializes some meters and timers. In SomeResource's methods, I am programmatically calling Meter.mark() and timer.stop(). I am not using any annotations, because I don't understand the @Timed and @Metered annotations. How are these linked to a Meter/Timer object? Where are these objects if you don't explicitly create them? From the documentation, it seemed like metrics recorded via those annotations could only be accessed from a MetricServlet, not via Graphite. Is that correct? 

Also, I need 2 meters per method (one to count when the method is called, another to count only if the return value is false), and it seems you can't have two @Metered annotations above a method. Whether I use annotations or programmatically call Meter.mark() and timer.stop(), the problem remains: how do I only record metrics when the config.yml has specified metrics reporting should be off?

I'd really like a full example, from yml file to any additions in my classes that extend Configuration and Application to my SomeResource class so I can get a thorough understanding of how everything works together. Thank you in advance!

Tim Bart

unread,
Nov 21, 2015, 3:23:42 PM11/21/15
to dropwizard-user
I've created an example for you at https://github.com/pims/dropwizard-metrics-example

Let me know if you have any questions.

lbecke...@gmail.com

unread,
Nov 23, 2015, 9:11:12 AM11/23/15
to dropwizard-user
Thank you - this was very helpful!

lbecke...@gmail.com

unread,
Nov 23, 2015, 9:59:13 AM11/23/15
to dropwizard-user
Is there any way to remove the package from being appended to the Graphite path? For example, I've added the prefix "this.is.my.prefix" and then added @Timed to my method in MyResource which is in package com.example.resources. When I look at my Graphite dashboard, the path is enormously long:

this.is.my.prefix.com.example.resources.MyResource.myMethod.

I don't need the package path.


lbecke...@gmail.com

unread,
Nov 23, 2015, 11:45:17 AM11/23/15
to dropwizard-user
Figured it out. @Timed(absolute=true, name="whatever").
Reply all
Reply to author
Forward
0 new messages