diff options
author | Rafael Chacon <rchacon@myfitnesspal.com> | 2014-08-20 07:47:48 -0700 |
---|---|---|
committer | Rafael Chacon <rchacon@myfitnesspal.com> | 2014-08-20 07:47:48 -0700 |
commit | be65d54a8c120343f87f07f617b296bfc5442625 (patch) | |
tree | da551faace02fb0e4cb86881bfb4af37053efdbf | |
parent | bd038442903e9070626ab21b64a4016d2ad5ae6a (diff) | |
download | Kamon-be65d54a8c120343f87f07f617b296bfc5442625.tar.gz Kamon-be65d54a8c120343f87f07f617b296bfc5442625.tar.bz2 Kamon-be65d54a8c120343f87f07f617b296bfc5442625.zip |
+ statsd: add the include-hostname setting to the default key generator
* This commit adds 'include-hostname' the default key generator setting
in statsd. When set to false, the generated keys wont include host
information. They will have the following scheme:
application.entity.entity-name.metric-name
3 files changed, 56 insertions, 10 deletions
diff --git a/kamon-statsd/src/main/resources/reference.conf b/kamon-statsd/src/main/resources/reference.conf index eac5eade..f86052c1 100644 --- a/kamon-statsd/src/main/resources/reference.conf +++ b/kamon-statsd/src/main/resources/reference.conf @@ -33,6 +33,10 @@ kamon { # this pattern: # application.host.entity.entity-name.metric-name application = "kamon" + # Includes the name of the hostname in the generated metric. When set to false, the scheme for the metrics + # will look as follows: + # application.entity.entity-name.metric-name + include-hostname = true } } }
\ No newline at end of file diff --git a/kamon-statsd/src/main/scala/kamon/statsd/StatsD.scala b/kamon-statsd/src/main/scala/kamon/statsd/StatsD.scala index ff273d9f..76ef8d5f 100644 --- a/kamon-statsd/src/main/scala/kamon/statsd/StatsD.scala +++ b/kamon-statsd/src/main/scala/kamon/statsd/StatsD.scala @@ -105,6 +105,8 @@ class StatsDExtension(system: ExtendedActorSystem) extends Kamon.Extension { class SimpleMetricKeyGenerator(config: Config) extends StatsD.MetricKeyGenerator { val application = config.getString("kamon.statsd.simple-metric-key-generator.application") + val includeHostnameInMetrics = + config.getBoolean("kamon.statsd.simple-metric-key-generator.include-hostname") val _localhostName = ManagementFactory.getRuntimeMXBean.getName.split('@')(1) val _normalizedLocalhostName = _localhostName.replace('.', '_') @@ -112,13 +114,16 @@ class SimpleMetricKeyGenerator(config: Config) extends StatsD.MetricKeyGenerator def normalizedLocalhostName: String = _normalizedLocalhostName + val baseName: String = + if (includeHostnameInMetrics) s"${application}.${normalizedLocalhostName}" + else application + def generateKey(groupIdentity: MetricGroupIdentity, metricIdentity: MetricIdentity): String = { val normalizedGroupName = groupIdentity.name.replace(": ", "-").replace(" ", "_").replace("/", "_") + val key = s"${baseName}.${groupIdentity.category.name}.${normalizedGroupName}" - if (isUserMetric(groupIdentity)) - s"${application}.${normalizedLocalhostName}.${groupIdentity.category.name}.${normalizedGroupName}" - else - s"${application}.${normalizedLocalhostName}.${groupIdentity.category.name}.${normalizedGroupName}.${metricIdentity.name}" + if (isUserMetric(groupIdentity)) key + else s"${key}.${metricIdentity.name}" } def isUserMetric(groupIdentity: MetricGroupIdentity): Boolean = groupIdentity match { @@ -126,4 +131,3 @@ class SimpleMetricKeyGenerator(config: Config) extends StatsD.MetricKeyGenerator case everythingElse ⇒ false } } - diff --git a/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala b/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala index 3bc1364c..5f3934a5 100644 --- a/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala +++ b/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala @@ -44,9 +44,51 @@ class StatsDMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers | """.stripMargin)) + implicit val metricKeyGenerator = new SimpleMetricKeyGenerator(system.settings.config) { + override def normalizedLocalhostName: String = "localhost_local" + } + val collectionContext = Kamon(Metrics).buildDefaultCollectionContext "the StatsDMetricSender" should { + "removes host name when attribute 'include-hostname' is set to false" in new UdpListenerFixture { + val config = ConfigFactory.parseString( + """ + |kamon { + | statsd { + | simple-metric-key-generator.application = "api" + | simple-metric-key-generator.include-hostname = false + | } + |} + | + """.stripMargin) + implicit val metricKeyGenerator = new SimpleMetricKeyGenerator(config) { + override def normalizedLocalhostName: String = "localhost_local" + } + + val testMetricKey = buildMetricKey("trace", "POST: /kamon/example", "elapsed-time") + testMetricKey should be(s"api.trace.POST-_kamon_example.elapsed-time") + } + + "uses aplication prefix when present" in new UdpListenerFixture { + val config = ConfigFactory.parseString( + """ + |kamon { + | statsd { + | simple-metric-key-generator.application = "api" + | simple-metric-key-generator.include-hostname = true + | } + |} + | + """.stripMargin) + implicit val metricKeyGenerator = new SimpleMetricKeyGenerator(config) { + override def normalizedLocalhostName: String = "localhost_local" + } + + val testMetricKey = buildMetricKey("trace", "POST: /kamon/example", "elapsed-time") + testMetricKey should be(s"api.localhost_local.trace.POST-_kamon_example.elapsed-time") + } + "normalize the group entity name to remove spaces, colons and replace '/' with '_'" in new UdpListenerFixture { val testMetricKey = buildMetricKey("trace", "POST: /kamon/example", "elapsed-time") testMetricKey should be(s"kamon.localhost_local.trace.POST-_kamon_example.elapsed-time") @@ -138,10 +180,6 @@ class StatsDMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers trait UdpListenerFixture { val testMaxPacketSize = system.settings.config.getBytes("kamon.statsd.max-packet-size") - val metricKeyGenerator = new SimpleMetricKeyGenerator(system.settings.config) { - override def normalizedLocalhostName: String = "localhost_local" - } - val testGroupIdentity = new MetricGroupIdentity { val name: String = "/user/kamon" val category: MetricGroupCategory = new MetricGroupCategory { @@ -149,7 +187,7 @@ class StatsDMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers } } - def buildMetricKey(categoryName: String, entityName: String, metricName: String): String = { + def buildMetricKey(categoryName: String, entityName: String, metricName: String)(implicit metricKeyGenerator: SimpleMetricKeyGenerator): String = { val metricIdentity = new MetricIdentity { val name: String = metricName } val groupIdentity = new MetricGroupIdentity { val name: String = entityName |