From 14fb59a9e07dd1bc50b7fe2794143e105fff6029 Mon Sep 17 00:00:00 2001 From: Juan Carrey Labarta Date: Fri, 10 May 2024 11:02:44 +0200 Subject: [PATCH 1/2] ISSUE-1337 Add EntityID to DatadogAgentReporter --- .../kamon/datadog/DatadogAgentReporter.scala | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/reporters/kamon-datadog/src/main/scala/kamon/datadog/DatadogAgentReporter.scala b/reporters/kamon-datadog/src/main/scala/kamon/datadog/DatadogAgentReporter.scala index b6a804ec3..8453b8c09 100644 --- a/reporters/kamon-datadog/src/main/scala/kamon/datadog/DatadogAgentReporter.scala +++ b/reporters/kamon-datadog/src/main/scala/kamon/datadog/DatadogAgentReporter.scala @@ -50,6 +50,9 @@ class DatadogAgentReporter private[datadog] (@volatile private var config: Datad new DecimalFormat("#.################################################################", symbols) private val valueFormat = new DecimalFormat("#0.#########", symbols) + private val DD_ENTITY_ID_ENV_VAR = "DD_ENTITY_ID" + private val ENTITY_ID_TAG_NAME = "dd.internal.entity_id" + logger.info("Started the Kamon Datadog reporter") override def stop(): Unit = {} @@ -68,7 +71,7 @@ class DatadogAgentReporter private[datadog] (@volatile private var config: Datad counter.name, config.measurementFormatter.formatMeasurement( encodeDatadogCounter(instrument.value, counter.settings.unit), - instrument.tags + updateTagsWithEntityID(instrument.tags) ) ) } @@ -81,7 +84,7 @@ class DatadogAgentReporter private[datadog] (@volatile private var config: Datad gauge.name, config.measurementFormatter.formatMeasurement( encodeDatadogGauge(instrument.value, gauge.settings.unit), - instrument.tags + updateTagsWithEntityID(instrument.tags) ) ) } @@ -94,7 +97,7 @@ class DatadogAgentReporter private[datadog] (@volatile private var config: Datad val bucketData = config.measurementFormatter.formatMeasurement( encodeDatadogHistogramBucket(bucket.value, bucket.frequency, metric.settings.unit), - instruments.tags + updateTagsWithEntityID(instruments.tags) ) config.packetBuffer.appendMeasurement(metric.name, bucketData) } @@ -103,6 +106,17 @@ class DatadogAgentReporter private[datadog] (@volatile private var config: Datad } + private def updateTagsWithEntityID(tags: TagSet, entityID: String): TagSet = { + if (entityID == null || entityID.trim().isEmpty()) { + entityId = System.getenv("DD_ENTITY_ID") + } + if (entityId != null && !entityId.trim().isEmpty()) { + return tags.withTag(ENTITY_ID_TAG_NAME, entityId); + } + + return tags; + } + private def encodeDatadogHistogramBucket(value: Long, frequency: Long, unit: MeasurementUnit): String = { val metricType = if (unit.dimension == Dimension.Time) "ms" else "h" val samplingRate: Double = 1d / frequency.toDouble From fcf8a3e050a6da394d763352784dcf638b6a5f69 Mon Sep 17 00:00:00 2001 From: Juan Carrey Labarta Date: Fri, 10 May 2024 11:08:33 +0200 Subject: [PATCH 2/2] fix: EntityID could be provided from config, but for now only env supported --- .../src/main/scala/kamon/datadog/DatadogAgentReporter.scala | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/reporters/kamon-datadog/src/main/scala/kamon/datadog/DatadogAgentReporter.scala b/reporters/kamon-datadog/src/main/scala/kamon/datadog/DatadogAgentReporter.scala index 8453b8c09..f7cb23e51 100644 --- a/reporters/kamon-datadog/src/main/scala/kamon/datadog/DatadogAgentReporter.scala +++ b/reporters/kamon-datadog/src/main/scala/kamon/datadog/DatadogAgentReporter.scala @@ -106,10 +106,8 @@ class DatadogAgentReporter private[datadog] (@volatile private var config: Datad } - private def updateTagsWithEntityID(tags: TagSet, entityID: String): TagSet = { - if (entityID == null || entityID.trim().isEmpty()) { - entityId = System.getenv("DD_ENTITY_ID") - } + private def updateTagsWithEntityID(tags: TagSet): TagSet = { + val entityId = System.getenv("DD_ENTITY_ID") if (entityId != null && !entityId.trim().isEmpty()) { return tags.withTag(ENTITY_ID_TAG_NAME, entityId); }