Created
March 1, 2018 20:23
-
-
Save ivantopo/78a7ca2964d765c52ce531f31deb1ab9 to your computer and use it in GitHub Desktop.
Automatically starting the Kamon system metrics module on Play Applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
play.modules.enabled += "modules.SystemMetricsModule" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Goes into app/modules/SystemMetricsModule.scala | |
package modules | |
import javax.inject.{Inject, Singleton} | |
import kamon.system.SystemMetrics | |
import play.api.{Configuration, Environment, Logger} | |
import play.api.inject.{ApplicationLifecycle, Binding, Module} | |
import scala.concurrent.Future | |
class SystemMetricsModule extends Module { | |
def bindings(environment: Environment, configuration: Configuration): Seq[Binding[SystemMetricsModule.KamonSystemMetricsLoader]] = { | |
Seq(bind[SystemMetricsModule.KamonSystemMetricsLoader].toSelf.eagerly()) | |
} | |
} | |
object SystemMetricsModule { | |
@Singleton | |
class KamonSystemMetricsLoader @Inject() (lifecycle: ApplicationLifecycle, environment: Environment, configuration: Configuration) { | |
Logger(classOf[KamonSystemMetricsLoader]).info("Starting the Kamon System Metrics Module") | |
SystemMetrics.startCollecting() | |
lifecycle.addStopHook { () ⇒ | |
Future.successful(SystemMetrics.stopCollecting()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment