playframeworkplayframework-2.5

How to enable Dropwizard Metrics in Play 2.5 HikariCP?


HikariCP database connection pool used in Play 2.5 supports Dropwizard Metrics. The recommended approach is to use HikariConfig.setMetricRegistry(MetricRegistry), but Play does not seem to be using Dependency Injection for HikariConfig. Is there another option to set MetricRegistry on Play HikariCP?


Solution

  • You can access the HikaryDatasource using

    @Singleton
    class InitMetricRegistry @Inject () (@NamedDatabaseProvider("default") val defaultDB: Database){
      val ds: HikariDataSource = defaultDB.dataSource.asInstanceOf[HikariDataSource]
      ds.setMetricRegistry(???)
    }
    

    To initialize the metrics on startup use an eager binding https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection#Eager-bindings