Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #16 from diversit/feature/register-default-exporters
Browse files Browse the repository at this point in the history
Added setting for automatic loading of default hotspot collectors
  • Loading branch information
stijndehaes authored Oct 6, 2018
2 parents 28e1d3a + 2214abf commit 249e6c3
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 7 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,12 @@ GET /metrics com.github.stijndehaes.playprometheusfilters.contr

You should be able to immediately get the metrics

## Example
## Default Hotspot metrics

The [Prometheus Hotspot library](https://github.com/prometheus/client_java#included-collectors) provides some default collectors
for garbage collection, memory pool, etc.
Default these collectors are _not_ registered. This can be changed by setting the configuration property to `true`.

```
play-prometheus-filters.register-default-hotspot-collectors = true
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ package com.github.stijndehaes.playprometheusfilters
import play.api.inject.{Binding, Module}
import play.api.{Configuration, Environment}
import io.prometheus.client.CollectorRegistry
import io.prometheus.client.hotspot._

object PrometheusModule {
val defaultExportsKey = "play-prometheus-filters.register-default-hotspot-collectors"
}

class PrometheusModule extends Module {
import PrometheusModule._

override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
CollectorRegistry.defaultRegistry.clear()

configuration.getOptional[Boolean](defaultExportsKey).foreach { enabled =>
if (enabled) {
DefaultExports.initialize()
}
}

Seq(
bind[CollectorRegistry].to(CollectorRegistry.defaultRegistry)
)
Expand Down
9 changes: 5 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ lazy val root = (project in file("."))
</developer>
</developers>
)
scalaVersion := "2.12.5"
scalaVersion := "2.12.6"

crossScalaVersions := Seq(scalaVersion.value, "2.11.11")
crossScalaVersions := Seq(scalaVersion.value, "2.11.12")

libraryDependencies ++= Seq(
guice,
"io.prometheus" % "simpleclient" % "0.3.0",
"io.prometheus" % "simpleclient_servlet" % "0.3.0"
"io.prometheus" % "simpleclient" % "0.5.0",
"io.prometheus" % "simpleclient_hotspot" % "0.5.0",
"io.prometheus" % "simpleclient_servlet" % "0.5.0"
)

libraryDependencies ++= Seq(
Expand Down
5 changes: 4 additions & 1 deletion conf/reference.conf
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
play.modules.enabled += com.github.stijndehaes.playprometheusfilters.PrometheusModule
play.modules.enabled += com.github.stijndehaes.playprometheusfilters.PrometheusModule

# Registers some default collectors for jvm metrics. See DefaultExports class.
play-prometheus-filters.register-default-hotspot-collectors = false
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.12")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.18")

addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.3")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.stijndehaes.playprometheusfilters

import io.prometheus.client.CollectorRegistry
import org.scalatest.{BeforeAndAfter, MustMatchers, WordSpec}
import play.api.inject.guice.GuiceApplicationBuilder

class PrometheusModuleSpec extends WordSpec with MustMatchers with BeforeAndAfter {

before {
// clearing registry before each test
CollectorRegistry.defaultRegistry.clear()
}

"PrometheusModule" should {
"register default exporters when enabled" in {
// default enabled
val app = new GuiceApplicationBuilder()
.configure(PrometheusModule.defaultExportsKey -> true)
.build()

val collector = app.injector.instanceOf[CollectorRegistry]
collector.getExporterNames.size must be > 0
}

"not register default exporters when disabled" in {
// disable default exporters
val app = new GuiceApplicationBuilder()
.configure(PrometheusModule.defaultExportsKey -> false)
.build()

val collector = app.injector.instanceOf[CollectorRegistry]
collector.getExporterNames.size must be (0)
}
}

/**
* Utility to expose exporter names for test on [[CollectorRegistry]].
*/
implicit class CollectorRegistryExtention(val registry: CollectorRegistry) {
/**
* @return Registered exporter names.
*/
def getExporterNames: Seq[String] = {
val exportNames = collection.mutable.Buffer.empty[String]
val mfs = registry.metricFamilySamples()
while(mfs.hasMoreElements) {
exportNames += mfs.nextElement().name
}
exportNames
}
}
}

0 comments on commit 249e6c3

Please sign in to comment.