Custom metrics using play-prometheus-filters

Hi all,

I have a dummy Play application in Scala which is a clone of https://github.com/Jyllands-Posten/play-prometheus-filters-example-app.

I have tried to add my own custom metrics, which seems to work, but the problem is that I cannot get the metrics defined by the play-prometheus-filters package and my own metrics exported on the /metrics endpoint at the same time.

I am declaring the HelloWorldController as follows, which exports my metrics, but ONLY my metrics:

class HelloWorldController @Inject() (val customizedPrometheusMetrics: CustomizedPrometheusMetrics,
                                      val cc: ControllerComponents)(implicit ec: ExecutionContext) extends AbstractController(cc) { ...  } 

Declaring the HelloWorldController as follows exports the metrics provided by the package, but NOT my own custom metrics:

class HelloWorldController @Inject()(val cc: ControllerComponents)  (implicit ec: ExecutionContext)   extends AbstractController(cc) { ... }

For reference, the “CustomizedPrometheusMetrics” looks as follows:

package models

import io.prometheus.client._
import javax.inject.Singleton

trait CustomizedPrometheusMetrics {
  def myHistogram(): Histogram
}

@Singleton
class CustomizedPrometheusMetricsImpl extends CustomizedPrometheusMetrics {
  CollectorRegistry.defaultRegistry.clear

  private val _myHistogram = Histogram
    .build("myHistogram", "latency on api calls")
    .register()

  override def myHistogram(): Histogram =
    _myHistogram
}

and I also have a “modlues/Module.scala”-file that looks as follows:

import javax.inject.Inject
import play.api.Logger
import play.api.Environment
import play.api.Configuration
import com.google.inject.AbstractModule

import models.CustomizedPrometheusMetrics
import models.CustomizedPrometheusMetricsImpl

import io.prometheus.client.CollectorRegistry

class Module extends play.api.inject.Module {
  import Module._

  override def bindings(env: Environment, conf: Configuration) = {

    Seq(
      bind[CustomizedPrometheusMetrics].to(classOf[CustomizedPrometheusMetricsImpl]),
    )
  }
}

The code above is everything that I have changed. The rest looks exactly like the code in the example app on the git repo: https://github.com/Jyllands-Posten/play-prometheus-filters-example-app.

My guess, for whatever it is worth, is that something is going wrong when injecting my own customizedPrometheusMetrics and somehow this one overwrites the default metrics rather than extending them, but I do not know enough to figure out how to fix that. The default metrics are request counters, latence counters and some jvm stats, so they are quite nice to get “out of the box” without having to figure out how to code all of those as well by myself.

I am relatively new to the Play framework and also don’t have a huge amount of Scala experience. I have just started to play around (no pun intended…) with the Play framework and am trying to get a dummy app with some logging and metrics up and running for my own personal amusement.

Any help is highly appreciated. Or, failing that, a few pointers on what I should read/study to one day be able to figure this out by myself.

Thank you in advance