Well, there is a registry of metrics available from environment.metrics() . There are many ways to get this when you need to. I am using dropwizard-guice to add Guice support.
private GuiceBundle<MyConfiguration> guiceBundle; @Override public void initialize(Bootstrap<MyConfiguration> bootstrap) { guiceBundle = GuiceBundle.<MyConfiguration>newBuilder() .addModule(new MyModule()) .setConfigClass(MyConfiguration.class) .build(Stage.DEVELOPMENT); bootstrap.addBundle(guiceBundle); }
Then I create a provider rule in the application module for metrics registries.
public class MyModule extends AbstractModule { ... @Provides @Singleton public MetricRegistry provideRegistry( Environment environment ) { return environment.metrics(); } }
And in my resource I mark the registry as entered.
@Path("/resource") public class MyResource { @Inject MetricRegistry registry; }
Finally, I request my static resources from the Guice injector, and then pass the instance to Jersey instead of the class name.
@Override public void run(Environment environment) { Injector injector = guiceBundle.getInjector(); environment.jersey().register(injector.getInstance(MyResource.class)); }
You could just register the class name and rely on the Guice / HK2 bridge to provide the injection, but historically Guice / HK2 integration has been problematic.
After you have connected the metrics registry, you will need to configure the metrics to transfer data somewhere. You can do this using the metrics configuration block .
For example, if you want to send metrics to graphite, you can set up a graphical metric reporter . You will need to add a graphite reporter dependency.
<dependency> <artifactId>dropwizard-metrics-graphite</artifactId> <groupId>io.dropwizard</groupId> <version>${dropwizard.version}</version> </dependency>
and add the configuration for the reporter.
metrics: reporters: - type: graphite prefix: <PREFIX_FOR_METRICS> host: <GRAPHITE_HOST> port: <GRAPHITE_PORT> frequency: 10s
Christian trimble
source share