diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2014-09-15 21:31:16 -0300 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2014-09-15 21:31:26 -0300 |
commit | 581b621d7e86106e367967811f9c1b8a7a5e63a0 (patch) | |
tree | cc7ce079a67b3f2cb17461155fd9716d6d5a0a25 /kamon-statsd/src/test | |
parent | 315d97c1d047db359c7b436c87a6a6b358f2a8de (diff) | |
download | Kamon-581b621d7e86106e367967811f9c1b8a7a5e63a0.tar.gz Kamon-581b621d7e86106e367967811f9c1b8a7a5e63a0.tar.bz2 Kamon-581b621d7e86106e367967811f9c1b8a7a5e63a0.zip |
+ statsd: allow percent-encoding of metric section names, related to #46
Diffstat (limited to 'kamon-statsd/src/test')
-rw-r--r-- | kamon-statsd/src/test/scala/kamon/statsd/SimpleMetricKeyGeneratorSpec.scala | 80 | ||||
-rw-r--r-- | kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala | 74 |
2 files changed, 86 insertions, 68 deletions
diff --git a/kamon-statsd/src/test/scala/kamon/statsd/SimpleMetricKeyGeneratorSpec.scala b/kamon-statsd/src/test/scala/kamon/statsd/SimpleMetricKeyGeneratorSpec.scala new file mode 100644 index 00000000..ed3fae5b --- /dev/null +++ b/kamon-statsd/src/test/scala/kamon/statsd/SimpleMetricKeyGeneratorSpec.scala @@ -0,0 +1,80 @@ +package kamon.statsd + +import com.typesafe.config.ConfigFactory +import kamon.metric.{ MetricGroupCategory, MetricGroupIdentity, MetricIdentity } +import org.scalatest.{ Matchers, WordSpec } + +class SimpleMetricKeyGeneratorSpec extends WordSpec with Matchers { + + val defaultConfiguration = ConfigFactory.parseString( + """ + |kamon.statsd.simple-metric-key-generator { + | application = kamon + | hostname-override = none + | include-hostname = true + | metric-name-normalization-strategy = normalize + |} + """.stripMargin) + + "the StatsDMetricSender" should { + "generate metric names that follow the application.host.entity.entity-name.metric-name pattern by default" in { + implicit val metricKeyGenerator = new SimpleMetricKeyGenerator(defaultConfiguration) { + override def hostName: String = "localhost" + } + + buildMetricKey("actor", "/user/example", "processing-time") should be("kamon.localhost.actor._user_example.processing-time") + buildMetricKey("trace", "POST: /kamon/example", "elapsed-time") should be("kamon.localhost.trace.POST-_kamon_example.elapsed-time") + } + + "allow to override the hostname" in { + val hostOverrideConfig = ConfigFactory.parseString("kamon.statsd.simple-metric-key-generator.hostname-override = kamon-host") + implicit val metricKeyGenerator = new SimpleMetricKeyGenerator(hostOverrideConfig.withFallback(defaultConfiguration)) { + override def hostName: String = "localhost" + } + + buildMetricKey("actor", "/user/example", "processing-time") should be("kamon.kamon-host.actor._user_example.processing-time") + buildMetricKey("trace", "POST: /kamon/example", "elapsed-time") should be("kamon.kamon-host.trace.POST-_kamon_example.elapsed-time") + } + + "removes host name when attribute 'include-hostname' is set to false" in { + val hostOverrideConfig = ConfigFactory.parseString("kamon.statsd.simple-metric-key-generator.include-hostname = false") + implicit val metricKeyGenerator = new SimpleMetricKeyGenerator(hostOverrideConfig.withFallback(defaultConfiguration)) { + override def hostName: String = "localhost" + } + + buildMetricKey("actor", "/user/example", "processing-time") should be("kamon.actor._user_example.processing-time") + buildMetricKey("trace", "POST: /kamon/example", "elapsed-time") should be("kamon.trace.POST-_kamon_example.elapsed-time") + } + + "remove spaces, colons and replace '/' with '_' when the normalization strategy is 'normalize'" in { + val hostOverrideConfig = ConfigFactory.parseString("kamon.statsd.simple-metric-key-generator.metric-name-normalization-strategy = normalize") + implicit val metricKeyGenerator = new SimpleMetricKeyGenerator(hostOverrideConfig.withFallback(defaultConfiguration)) { + override def hostName: String = "localhost.local" + } + + buildMetricKey("actor", "/user/example", "processing-time") should be("kamon.localhost_local.actor._user_example.processing-time") + buildMetricKey("trace", "POST: /kamon/example", "elapsed-time") should be("kamon.localhost_local.trace.POST-_kamon_example.elapsed-time") + } + + "percent-encode special characters in the group name and hostname when the normalization strategy is 'normalize'" in { + val hostOverrideConfig = ConfigFactory.parseString("kamon.statsd.simple-metric-key-generator.metric-name-normalization-strategy = percent-encode") + implicit val metricKeyGenerator = new SimpleMetricKeyGenerator(hostOverrideConfig.withFallback(defaultConfiguration)) { + override def hostName: String = "localhost.local" + } + + buildMetricKey("actor", "/user/example", "processing-time") should be("kamon.localhost%2Elocal.actor.%2Fuser%2Fexample.processing-time") + buildMetricKey("trace", "POST: /kamon/example", "elapsed-time") should be("kamon.localhost%2Elocal.trace.POST%3A%20%2Fkamon%2Fexample.elapsed-time") + } + } + + 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 + val category: MetricGroupCategory = new MetricGroupCategory { + val name: String = categoryName + } + } + metricKeyGenerator.generateKey(groupIdentity, metricIdentity) + } +} diff --git a/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala b/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala index 28ead7dc..5d37bb75 100644 --- a/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala +++ b/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala @@ -37,85 +37,23 @@ class StatsDMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers | disable-aspectj-weaver-missing-error = true | } | - | statsd { - | max-packet-size = 256 bytes - | simple-metric-key-generator.hostname-override = "none" + | statsd.simple-metric-key-generator { + | application = kamon + | hostname-override = kamon-host + | include-hostname = true + | metric-name-normalization-strategy = normalize | } |} | """.stripMargin)) implicit val metricKeyGenerator = new SimpleMetricKeyGenerator(system.settings.config) { - override def normalizedLocalhostName: String = "localhost_local" + override def hostName: String = "localhost_local" } val collectionContext = Kamon(Metrics).buildDefaultCollectionContext "the StatsDMetricSender" should { - "allows to override the hostname" in new UdpListenerFixture { - val config = ConfigFactory.parseString( - """ - |kamon { - | statsd { - | simple-metric-key-generator.application = "api" - | simple-metric-key-generator.hostname-override = "kamonhost" - | 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.kamonhost.trace.POST-_kamon_example.elapsed-time") - } - - "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 - | simple-metric-key-generator.hostname-override = "none" - | } - |} - | - """.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 - | simple-metric-key-generator.hostname-override = "none" - | } - |} - | - """.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") - } "flush the metrics data after processing the tick, even if the max-packet-size is not reached" in new UdpListenerFixture { val testMetricName = "processing-time" |