main.misk.metrics.backends.prometheus.PrometheusMetricsServiceModule.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of misk-prometheus Show documentation
Show all versions of misk-prometheus Show documentation
Open source application container in Kotlin
package misk.metrics.backends.prometheus
import io.prometheus.client.Collector
import io.prometheus.client.CollectorRegistry
import io.prometheus.client.hotspot.BufferPoolsExports
import io.prometheus.client.hotspot.ClassLoadingExports
import io.prometheus.client.hotspot.GarbageCollectorExports
import io.prometheus.client.hotspot.MemoryPoolsExports
import io.prometheus.client.hotspot.StandardExports
import io.prometheus.client.hotspot.ThreadExports
import io.prometheus.client.hotspot.VersionInfoExports
import misk.ReadyService
import misk.ServiceModule
import misk.inject.KAbstractModule
import jakarta.inject.Inject
import misk.metrics.MetricsModule
/**
* Exposes prometheus metrics over a dedicated port. Allows internal metrics to be exposed via a k8s
* ClusterIP address, where they can be scraped by a cluster local Prometheus server without also
* exposing them to the outside world via the port bound to the service load balancer. If using
* the prometheus operator, one would generally create a k8s ClusterIP service exporting the
* metrics port, then a prometheus ServiceMonitor selecting that service via a label.
*/
class PrometheusMetricsServiceModule(private val config: PrometheusConfig) : KAbstractModule() {
override fun configure() {
install(MetricsModule())
bind().toInstance(config)
install(
ServiceModule()
.enhancedBy()
)
// For every Collector registered with a multibinding, configure it in the registry when the
// injector is created.
requestInjection(object : Any() {
@Inject fun registerCollectors(
registry: CollectorRegistry,
collectors: @JvmSuppressWildcards Set
) {
collectors.forEach { registry.register(it) }
}
})
// Bind collectors that are packaged with Prometheus.
multibind().toInstance(StandardExports())
multibind().toInstance(MemoryPoolsExports())
multibind().toInstance(BufferPoolsExports())
multibind().toInstance(ThreadExports())
multibind().toInstance(GarbageCollectorExports())
multibind().toInstance(ClassLoadingExports())
multibind().toInstance(VersionInfoExports())
}
}