Skip to content

Commit af70b8e

Browse files
authored
allow for namespaced environment tags (#1101)
1 parent 2235b79 commit af70b8e

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

core/kamon-core-tests/src/test/scala/kamon/util/EnvironmentTagsSpec.scala

+14-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class EnvironmentTagsSpec extends AnyWordSpec with Matchers {
3333
| tags {
3434
| env = staging
3535
| region = asia-1
36+
|
37+
| k8s.namespace.name = production
38+
|
39+
| some {
40+
| tag {
41+
| inside = example
42+
| }
43+
| }
3644
| }
3745
|}
3846
""".stripMargin
@@ -97,12 +105,17 @@ class EnvironmentTagsSpec extends AnyWordSpec with Matchers {
97105
|include-service = no
98106
|include-host = no
99107
|include-instance = no
100-
|exclude = [ "region", "env" ]
108+
|exclude = [ "region", "env", "k8s.namespace.name", "some.tag.inside" ]
101109
""".stripMargin)
102110

103111
val tags = EnvironmentTags.from(testEnv, config)
104112
tags shouldBe empty
105113
}
114+
115+
"allow for nested tag names" in {
116+
testEnv.tags("k8s.namespace.name") shouldBe "production"
117+
testEnv.tags("some.tag.inside") shouldBe "example"
118+
}
106119
}
107120

108121
implicit def toMap(tags: TagSet): Map[String, String] = {

core/kamon-core/src/main/scala/kamon/status/Environment.scala

+18-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package status
1919

2020
import java.net.InetAddress
2121
import java.util.concurrent.ThreadLocalRandom
22-
2322
import com.typesafe.config.Config
2423
import kamon.tag.TagSet
2524
import kamon.util.HexCodec
@@ -64,14 +63,31 @@ object Environment {
6463
val environmentConfig = config.getConfig("kamon.environment")
6564
val service = environmentConfig.getString("service")
6665
val tagsConfig = environmentConfig.getConfig("tags")
67-
val tags = TagSet.from(tagsConfig.topLevelKeys.map(tag => (tag -> tagsConfig.getString(tag))).toMap)
66+
val tags = flattenedTags(tagsConfig)
6867

6968
val host = readValueOrGenerate(environmentConfig.getString("host"), generateHostname())
7069
val instance = readValueOrGenerate(environmentConfig.getString("instance"), s"$service@$host")
7170

7271
Environment(host, service, instance, _incarnation, tags)
7372
}
7473

74+
/**
75+
* Flattens all the configuration keys in the configuration so that we can have namespaced tag names
76+
* like `k8s.namespace.name` or nested configurations and they will still generate a flat `x.y.z=value`
77+
* set of tags.
78+
*/
79+
private def flattenedTags(tagsConfig: Config): TagSet = {
80+
import scala.collection.JavaConverters._
81+
82+
TagSet.from(
83+
tagsConfig.entrySet()
84+
.iterator()
85+
.asScala
86+
.map { e => e.getKey -> e.getValue.unwrapped().toString }
87+
.toMap
88+
)
89+
}
90+
7591
private def generateHostname(): String = {
7692
try InetAddress.getLocalHost.getHostName() catch { case t: Throwable =>
7793
_logger.warn("Could not automatically resolve a host name for this instance, falling back to 'localhost'", t)

0 commit comments

Comments
 (0)