Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A StatsDClient helper function for doing custom DataDog metrics in scala
package com.kukaatx.utilities
import javax.inject.Singleton
import com.google.inject.Provides
import com.timgroup.statsd._
import play.api.{Configuration, Environment}
trait StatsDBinding {
val configuration: Configuration
val environment: Environment
@Provides
@Singleton
def getStatsDClient: StatsDClient = {
try {
val stub = configuration.getBoolean(s"statsd.stub.${environment.mode.toString.toLowerCase}").contains(true)
if (stub) {
new NoOpStatsDClient()
} else {
new NonBlockingStatsDClient(
configuration.getString("statsd.prefix").getOrElse("yourname"),
configuration.getString("statsd.host").getOrElse("your-dd-agent-daemonset-service"),
configuration.getInt("statsd.port").getOrElse(1234)
)
}
} catch {
case e: Throwable =>
println(s"Exception when creating statsd client. Disabling statsd logging. e=$e")
new NoOpStatsDClient()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment