From a8bef44040c20047de700bc09f608bbdfdea033b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cardoso?= Date: Fri, 27 Oct 2023 05:28:33 +0100 Subject: [PATCH] Use ConfigUtil.splitPath to build environment tag keys from a config path (#1303) * = kamon-core: add tests that fail using tags with special chars * = kamon-core: Use ConfigUtil.splitPath to build a tag key from a config path * = kamon-core: Improve spec examples and formatting --------- Co-authored-by: hughsimpson --- .../kamon/util/EnvironmentTagsSpec.scala | 31 ++++++++++++++++++- .../main/scala/kamon/status/Environment.scala | 4 +-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/core/kamon-core-tests/src/test/scala/kamon/util/EnvironmentTagsSpec.scala b/core/kamon-core-tests/src/test/scala/kamon/util/EnvironmentTagsSpec.scala index c0dac90cc..c2c405f87 100644 --- a/core/kamon-core-tests/src/test/scala/kamon/util/EnvironmentTagsSpec.scala +++ b/core/kamon-core-tests/src/test/scala/kamon/util/EnvironmentTagsSpec.scala @@ -39,8 +39,15 @@ class EnvironmentTagsSpec extends AnyWordSpec with Matchers { | some { | tag { | inside = example + | "@inside" = value | } | } + | + | "defined-using-quotes" = value + | + | "\"tag-with-quotes\"" = value + | + | "@tag-with-special-chars" = value | } |} """.stripMargin @@ -80,6 +87,19 @@ class EnvironmentTagsSpec extends AnyWordSpec with Matchers { tags("env") shouldBe "staging" tags("region") shouldBe "asia-1" + tags.toMap shouldBe Map( + "@tag-with-special-chars" -> "value", + "env" -> "staging", + "host" -> "my-hostname", + "instance" -> "my-instance-name", + "k8s.namespace.name" -> "production", + "region" -> "asia-1", + "service" -> "environment-spec", + "some.tag.@inside" -> "value", + "some.tag.inside" -> "example", + "defined-using-quotes" -> "value", + "\"tag-with-quotes\"" -> "value" + ) } "remove excluded tags" in { @@ -105,7 +125,16 @@ class EnvironmentTagsSpec extends AnyWordSpec with Matchers { |include-service = no |include-host = no |include-instance = no - |exclude = [ "region", "env", "k8s.namespace.name", "some.tag.inside" ] + |exclude = [ + | "region", + | "env", + | "k8s.namespace.name", + | "some.tag.inside", + | "some.tag.@inside", + | "defined-using-quotes", + | "@tag-with-special-chars", + | "\"tag-with-quotes\"" + |] """.stripMargin) val tags = EnvironmentTags.from(testEnv, config) diff --git a/core/kamon-core/src/main/scala/kamon/status/Environment.scala b/core/kamon-core/src/main/scala/kamon/status/Environment.scala index 48e616baa..4292bcd8a 100644 --- a/core/kamon-core/src/main/scala/kamon/status/Environment.scala +++ b/core/kamon-core/src/main/scala/kamon/status/Environment.scala @@ -19,7 +19,7 @@ package status import java.net.InetAddress import java.util.concurrent.ThreadLocalRandom -import com.typesafe.config.Config +import com.typesafe.config.{Config, ConfigUtil} import kamon.tag.TagSet import kamon.util.HexCodec import org.slf4j.LoggerFactory @@ -83,7 +83,7 @@ object Environment { tagsConfig.entrySet() .iterator() .asScala - .map { e => e.getKey -> e.getValue.unwrapped().toString } + .map { e => ConfigUtil.splitPath(e.getKey).asScala.mkString(".") -> e.getValue.unwrapped().toString } .toMap ) }