I'm really struggling to understand and implement metrics in my DropWizard application. This is what I want to do:
- In my config.yml, there's a boolean to either turn metrics reporting on or off.
- If metrics is turned on, then in my SomeResource class, I would like to:
- Record how many times a method in SomeResource is called.
- Record how many times a method in SomeResource returns "false."
- Record the runtime of a method in SomeResource.
- If metrics is turned off, then I do not want to record anything (obviously)!
- 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!