Skip to content
This repository has been archived by the owner on Mar 29, 2020. It is now read-only.

Commit

Permalink
cleanup the API reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantopo committed Apr 24, 2018
1 parent 6204cec commit 49bfb48
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/scala/kamon/datadog/DatadogAPIReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ class DatadogAPIReporter extends MetricReporter {
override def reportPeriodSnapshot(snapshot: PeriodSnapshot): Unit = {
val body = RequestBody.create(jsonType, buildRequestBody(snapshot))
val request = new Request.Builder().url(apiUrl + configuration.apiKey).post(body).build
httpClient.newCall(request).execute
val response = httpClient.newCall(request).execute()

if (!response.isSuccessful()) {
logger.error(s"Failed to POST metrics to Datadog with status code [${response.code()}], Body: [${response.body().string()}]")
}

response.close()
}

private[datadog] def buildRequestBody(snapshot: PeriodSnapshot): String = {
Expand All @@ -77,15 +83,16 @@ class DatadogAPIReporter extends MetricReporter {
def addDistribution(metric: MetricDistribution): Unit = {
import metric._

addMetric(name + ".min", valueFormat.format(scale(distribution.min, unit)), gauge, metric.tags)
val average = if (distribution.count > 0L) (distribution.sum / distribution.count) else 0L
addMetric(name + ".avg", valueFormat.format(scale(average, unit)), gauge, metric.tags)
addMetric(name + ".count", valueFormat.format(distribution.count), count, metric.tags)
addMetric(name + ".median", valueFormat.format(distribution.percentile(50D).value), gauge, metric.tags)
addMetric(name + ".95percentile", valueFormat.format(scale(distribution.percentile(95D).value, unit)), gauge, metric.tags)
addMetric(name + ".max", valueFormat.format(scale(distribution.max, unit)), gauge, metric.tags)
addMetric(name + ".count", valueFormat.format(scale(distribution.count, unit)), count, metric.tags)
addMetric(name + ".sum", valueFormat.format(scale(distribution.sum, unit)), gauge, metric.tags)
addMetric(name + ".p95", valueFormat.format(scale(distribution.percentile(95D).value, unit)), gauge, metric.tags)
addMetric(name + ".min", valueFormat.format(scale(distribution.min, unit)), gauge, metric.tags)
}

def addMetric(metricName: String, value: String, metricType: String, tags: Map[String, String]): Unit = {
println(tags)
val customTags = (configuration.extraTags ++ tags.filterKeys(configuration.tagFilter.accept)).map { case (k, v) quote"$k:$v" }.toSeq
val allTagsString = customTags.mkString("[", ",", "]")

Expand Down Expand Up @@ -126,7 +133,6 @@ class DatadogAPIReporter extends MetricReporter {
private def readConfiguration(config: Config): Configuration = {
val datadogConfig = config.getConfig("kamon.datadog")

println(datadogConfig.getString("filter-config-key"))
Configuration(
apiKey = datadogConfig.getString("http.api-key"),
connectTimeout = datadogConfig.getDuration("http.connect-timeout"),
Expand Down

0 comments on commit 49bfb48

Please sign in to comment.