Skip to content

Commit d9ae0f4

Browse files
authored
Merge pull request #515 from ivantopo/issue#510/add-support-for-environment-tags
add support for environment tags, fixes #510
2 parents 26e8684 + d64bbb1 commit d9ae0f4

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

kamon-core-tests/src/test/scala/kamon/EnvironmentSpec.scala

+20-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class EnvironmentSpec extends WordSpec with Matchers {
2727
| instance = auto
2828
|}
2929
""".stripMargin
30-
)
30+
).withFallback(ConfigFactory.defaultReference())
3131

3232
"the Kamon environment" should {
3333
"assign a host and instance name when they are set to 'auto'" in {
@@ -36,6 +36,7 @@ class EnvironmentSpec extends WordSpec with Matchers {
3636
env.host shouldNot be("auto")
3737
env.instance shouldNot be("auto")
3838
env.instance shouldBe s"environment-spec@${env.host}"
39+
env.tags shouldBe empty
3940
}
4041

4142
"use the configured host and instance, if provided" in {
@@ -51,6 +52,24 @@ class EnvironmentSpec extends WordSpec with Matchers {
5152

5253
env.host should be("spec-host")
5354
env.instance should be("spec-instance")
55+
env.tags shouldBe empty
56+
}
57+
58+
"read all environment tags, if provided" in {
59+
val customConfig = ConfigFactory.parseString(
60+
"""
61+
|kamon.environment.tags {
62+
| custom1 = "test1"
63+
| env = staging
64+
|}
65+
""".stripMargin)
66+
67+
val env = Environment.fromConfig(customConfig.withFallback(baseConfig))
68+
69+
env.tags should contain allOf(
70+
("custom1" -> "test1"),
71+
("env" -> "staging")
72+
)
5473
}
5574

5675
"always return the same incarnation name" in {

kamon-core/src/main/resources/reference.conf

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ kamon {
1111

1212
# Identifier for a particular instance of this service. If set to `auto` Kamon will use the pattern service@host.
1313
instance = "auto"
14+
15+
# Arbitraty key-value pairs that further identify the environment where this service instance is running. Typically
16+
# these tags will be used by the reporting modules as additional tags for all metrics or spans. Take a look at each
17+
# reporter module's configuration to ensure these tags are supported and included in the reported data. Example:
18+
#
19+
# kamon.environment.tags {
20+
# env = "staging"
21+
# region = "us-east-1"
22+
# }
23+
tags {
24+
25+
}
1426
}
1527

1628
# FQCN of the reporter instances that should be loaded when calling `Kamon.reporters.loadReportersFromConfig()`. All

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ import java.util.concurrent.ThreadLocalRandom
2121
import com.typesafe.config.Config
2222
import kamon.util.HexCodec
2323

24-
case class Environment(host: String, service: String, instance: String, incarnation: String)
24+
case class Environment(host: String, service: String, instance: String, incarnation: String, tags: Map[String, String])
2525

2626
object Environment {
2727
private val incarnation = HexCodec.toLowerHex(ThreadLocalRandom.current().nextLong())
2828

2929
def fromConfig(config: Config): Environment = {
3030
val environmentConfig = config.getConfig("kamon.environment")
31-
3231
val service = environmentConfig.getString("service")
32+
val tagsConfig = environmentConfig.getConfig("tags")
33+
val tags = tagsConfig.topLevelKeys.map(tag => (tag -> tagsConfig.getString(tag))).toMap
3334

3435
val host = readValueOrGenerate(
3536
environmentConfig.getString("host"),
@@ -41,7 +42,7 @@ object Environment {
4142
s"$service@$host"
4243
)
4344

44-
Environment(host, service, instance, incarnation)
45+
Environment(host, service, instance, incarnation, tags)
4546
}
4647

4748
private def readValueOrGenerate(configuredValue: String, generator: => String): String =

0 commit comments

Comments
 (0)