Skip to content

Commit

Permalink
= kamon-datadog: Allow failure log level configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
thyandrecardoso committed Aug 30, 2024
1 parent a96a353 commit 0fa69a7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
2 changes: 2 additions & 0 deletions reporters/kamon-datadog/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ kamon {
compression = false
}

# The log level in which to log failures to submit metrics.
failure-log-level = "error"

# All time values are collected in nanoseconds,
# to scale before sending to datadog set "time-units" to "s" or "ms" or "µs".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import kamon.{ module, Kamon }
import kamon.datadog.DatadogAPIReporter.Configuration
import kamon.module.{ MetricReporter, ModuleFactory }
import org.slf4j.LoggerFactory
import org.slf4j.event.Level

import scala.util.{ Failure, Success }

Expand Down Expand Up @@ -65,7 +66,7 @@ class DatadogAPIReporter(@volatile private var configuration: Configuration, @vo
override def reportPeriodSnapshot(snapshot: PeriodSnapshot): Unit = {
httpClient.doPost("application/json; charset=utf-8", buildRequestBody(snapshot)) match {
case Failure(e) =>
logger.error(e.getMessage)
logger.logAtLevel(configuration.failureLogLevel, e.getMessage)
case Success(response) =>
logger.trace(response)
}
Expand Down Expand Up @@ -149,21 +150,35 @@ private object DatadogAPIReporter {
val count = "count"
val gauge = "gauge"

case class Configuration(httpConfig: Config, timeUnit: MeasurementUnit, informationUnit: MeasurementUnit, extraTags: Seq[(String, String)], tagFilter: Filter)
case class Configuration(
httpConfig: Config,
timeUnit: MeasurementUnit,
informationUnit: MeasurementUnit,
extraTags: Seq[(String, String)],
tagFilter: Filter,
failureLogLevel: Level)

implicit class QuoteInterp(val sc: StringContext) extends AnyVal {
def quote(args: Any*): String = "\"" + sc.s(args: _*) + "\""
}

def readConfiguration(config: Config): Configuration = {
val datadogConfig = config.getConfig("kamon.datadog")

// Remove the "host" tag since it gets added to the datadog payload separately
val extraTags = EnvironmentTags
.from(Kamon.environment, datadogConfig.getConfig("environment-tags"))
.without("host")
.all()
.map(p => p.key -> Tag.unwrapValue(p).toString)

Configuration(
datadogConfig.getConfig("api"),
timeUnit = readTimeUnit(datadogConfig.getString("time-unit")),
informationUnit = readInformationUnit(datadogConfig.getString("information-unit")),
// Remove the "host" tag since it gets added to the datadog payload separately
EnvironmentTags.from(Kamon.environment, datadogConfig.getConfig("environment-tags")).without("host").all().map(p => p.key -> Tag.unwrapValue(p).toString),
Kamon.filter("kamon.datadog.environment-tags.filter")
extraTags = extraTags,
tagFilter = Kamon.filter("kamon.datadog.environment-tags.filter"),
failureLogLevel = readLogLevel(datadogConfig.getString("failure-log-level"))
)
}
}
28 changes: 28 additions & 0 deletions reporters/kamon-datadog/src/main/scala/kamon/datadog/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.typesafe.config.Config
import kamon.metric.MeasurementUnit
import kamon.metric.MeasurementUnit.{ information, time }
import okhttp3._
import org.slf4j.Logger
import org.slf4j.event.Level

import scala.util.{ Failure, Success, Try }

Expand All @@ -36,6 +38,23 @@ package object datadog {
}
}

implicit class LoggerExtras(val logger: Logger) extends AnyVal {
def logAtLevel(level: Level, msg: String): Unit = {
level match {
case Level.TRACE =>
logger.trace(msg)
case Level.DEBUG =>
logger.debug(msg)
case Level.INFO =>
logger.info(msg)
case Level.WARN =>
logger.warn(msg)
case Level.ERROR =>
logger.error(msg)
}
}
}

private[datadog] case class HttpClient(apiUrl: String, apiKey: Option[String], usingCompression: Boolean, usingAgent: Boolean, connectTimeout: Duration,
readTimeout: Duration, writeTimeout: Duration) {

Expand Down Expand Up @@ -124,4 +143,13 @@ package object datadog {
case "gb" => information.gigabytes
case other => sys.error(s"Invalid time unit setting [$other], the possible values are [b, kb, mb, gb]")
}

def readLogLevel(level: String): Level = level match {
case "trace" => Level.TRACE
case "debug" => Level.DEBUG
case "info" => Level.INFO
case "warn" => Level.WARN
case "error" => Level.ERROR
case other => sys.error(s"Invalid log level setting [$other], the possible values are [trace, debug, info, warn, error]")
}
}

0 comments on commit 0fa69a7

Please sign in to comment.