@Timed metrics for non-jersey resources

605 views
Skip to first unread message

groov...@gmail.com

unread,
Jul 23, 2014, 1:50:05 PM7/23/14
to dropwiz...@googlegroups.com
Hi,
I have been banging my head against the wall for the last few hours trying to add timer metrics for services, dao, etc within my app. The documentation on this is really slim and searches on the interwebs haven't exactly got me where I want to be.

So this is the scenario...

MyResource -> MyService -> MyDao 

class MyResource {
  @Timed
  @GET
  public String meth() {
      myservice.doWork();
      ...
      return "hello";
  }
}

class MyService {
  @Timed
  public void doWork() { ... }
}


I have put up a mini sample with what I have so far. With this, the metrics are registered in the registry, but no metrics is gathered no matter what I try. I must be missing something, but can't figure out what.

My end goal is to have metrics gathered not only on the MyResource (which comes out of the box with DW), but also MyService and MyDao methods.

Please can anyone help? I'll really appreciate it.

Thanks

groov...@gmail.com

unread,
Jul 23, 2014, 2:17:12 PM7/23/14
to dropwiz...@googlegroups.com
Just realized I forgot to paste the link to the sample.

https://github.com/berinle/custom-metrics-dw

You can run it with gradlew run

Carlo Barbara

unread,
Jul 24, 2014, 3:19:59 PM7/24/14
to dropwiz...@googlegroups.com
Take a look at http://metrics.codahale.com/getting-started/#timers

Note the steps:

1. Get the timer
2. call time()
3. execute code to measure
4. call stop()


If you want that functionality in other places, you need to get a handle to the timer and create an aspect to run your timed code in.




--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

groov...@gmail.com

unread,
Jul 24, 2014, 7:29:43 PM7/24/14
to dropwiz...@googlegroups.com
Thanks Carlo. I was finally able to resolve this with your help. I pushed up the changes in case anyone else is stuck like I was. Hopefully, it will help someone else in the near future. 

I find it strange the documentation is quite light around this topic. Seems like a feature most people will desire ;)

Jochen Schalanda

unread,
Jul 28, 2014, 5:37:27 PM7/28/14
to dropwiz...@googlegroups.com
Hi,

On 25.07.2014 01:29, groov...@gmail.com wrote:
> I find it strange the documentation is quite light around this topic.
> Seems like a feature most people will desire ;)

What you try to achieve (using the annotations from metrics-annotation
with arbitrary methods) is more complicated or more heavy-weight than
you might think.

Jersey just happened to offer the necessary integrations points, but
actually this would be a job for AspectJ [1], Guice [2], or another
AOP-library/-framework. I don't think it's a job for Dropwizard itself
but a extension for the Metrics library might work.

You could try to integrate Google Guice and metrics-guice [3] into your
project to actually get what you want.


[1]: http://www.eclipse.org/aspectj/
[2]: https://github.com/google/guice/wiki/AOP
[3]: https://github.com/palominolabs/metrics-guice


Cheers,
Jochen

> On Thursday, July 24, 2014 3:19:59 PM UTC-4, Carlo Barbara wrote:
>
> Take a look at http://metrics.codahale.com/getting-started/#timers
> <http://metrics.codahale.com/getting-started/#timers>
>
> Note the steps:
>
> 1. Get the timer
> 2. call time()
> 3. execute code to measure
> 4. call stop()
>
> The code that makes that work for resource methods in DW
> is https://github.com/dropwizard/metrics/blob/master/metrics-jetty8/src/main/java/com/codahale/metrics/jetty8/InstrumentedHandler.java
> <https://github.com/dropwizard/metrics/blob/master/metrics-jetty8/src/main/java/com/codahale/metrics/jetty8/InstrumentedHandler.java>
>
> If you want that functionality in other places, you need to get a
> handle to the timer and create an aspect to run your timed code in.
>
>
>
> On Wed, Jul 23, 2014 at 2:17 PM, <groov...@gmail.com <javascript:>>

Bayo Erinle

unread,
Jul 28, 2014, 6:20:54 PM7/28/14
to dropwiz...@googlegroups.com
Thanks Jochen. I already have it working. I went the AOP route and
posted back to this thread with the solution.

As per the docs, I still think it can do a better job of nudging you
in the right direction.

Sent from my iPad
> --
> You received this message because you are subscribed to a topic in the Google Groups "dropwizard-user" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/dropwizard-user/_XWGIypnmb4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to dropwizard-us...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages