Register MetricRegistry inside the Dropwizard package

I am creating a Dropwizard package for reuse in all of my microservices. One of the things I would like to standardize is MetricRegistry every service uses.

It would be great if I could configure each service to use the same MetricRegistry , just adding my package to init, so something like:

 class Microservice1 extends Application<Microservice1Config> { @Override void initialize(Bootstrap<Microservice1Config> cfg) { // Boom! Standardized metrics/reporters configured and initialized! bootstrap.addBundle(new MyBundle()) } } 

The problem is that the Bundle API does not seem to contribute to this type of behavior:

 class MyBundle implements Bundle { MetricRegistry metricRegistry @Override void initialize(Bootstrap bootstrap) { } @Override void run(Environment environment) { environment.jersey().register(???) } } 

Since the register(...) method does not register MetricRegistry as a JAX-RS resource, I don’t understand how to connect it so that this MetricRegistry used for all indicators throughout the microservice. Ideas?


Update

I am looking for where to put the following:

 MetricRegistry metricRegistry = new MetricRegistry() Slf4jReporter slf4jReporter = Slf4jReporter.forRegistry(metricRegistry) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.SECONDS) .build() slf4jReporter.start(5, TimeUnit.SECONDS) 
+7
java codahale-metrics bundle dropwizard
source share
1 answer

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 
+9
source share

All Articles