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/main/scala/kamon/statsd | |
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/main/scala/kamon/statsd')
-rw-r--r-- | kamon-statsd/src/main/scala/kamon/statsd/SimpleMetricKeyGenerator.scala | 69 | ||||
-rw-r--r-- | kamon-statsd/src/main/scala/kamon/statsd/StatsD.scala | 40 |
2 files changed, 70 insertions, 39 deletions
diff --git a/kamon-statsd/src/main/scala/kamon/statsd/SimpleMetricKeyGenerator.scala b/kamon-statsd/src/main/scala/kamon/statsd/SimpleMetricKeyGenerator.scala new file mode 100644 index 00000000..f0bc6d64 --- /dev/null +++ b/kamon-statsd/src/main/scala/kamon/statsd/SimpleMetricKeyGenerator.scala @@ -0,0 +1,69 @@ +package kamon.statsd + +import java.lang.management.ManagementFactory + +import com.typesafe.config.Config +import kamon.metric.UserMetrics.UserMetricGroup +import kamon.metric.{ MetricIdentity, MetricGroupIdentity } + +class SimpleMetricKeyGenerator(config: Config) extends StatsD.MetricKeyGenerator { + type Normalizer = String ⇒ String + + val configSettings = config.getConfig("kamon.statsd.simple-metric-key-generator") + val application = configSettings.getString("application") + val includeHostname = configSettings.getBoolean("include-hostname") + val hostnameOverride = configSettings.getString("hostname-override") + val normalizer = createNormalizer(configSettings.getString("metric-name-normalization-strategy")) + + val normalizedHostname = + if (hostnameOverride.equals("none")) normalizer(hostName) + else normalizer(hostnameOverride) + + val baseName: String = + if (includeHostname) s"$application.$normalizedHostname" + else application + + def generateKey(groupIdentity: MetricGroupIdentity, metricIdentity: MetricIdentity): String = { + val normalizedGroupName = normalizer(groupIdentity.name) + val key = s"${baseName}.${groupIdentity.category.name}.${normalizedGroupName}" + + if (isUserMetric(groupIdentity)) key + else s"${key}.${metricIdentity.name}" + } + + def isUserMetric(groupIdentity: MetricGroupIdentity): Boolean = groupIdentity.isInstanceOf[UserMetricGroup] + + def hostName: String = ManagementFactory.getRuntimeMXBean.getName.split('@')(1) + + def createNormalizer(strategy: String): Normalizer = strategy match { + case "percent-encode" ⇒ PercentEncoder.encode + case "normalize" ⇒ (s: String) ⇒ s.replace(": ", "-").replace(" ", "_").replace("/", "_").replace(".", "_") + } +} + +object PercentEncoder { + + def encode(originalString: String): String = { + val encodedString = new StringBuilder() + + for (character ← originalString) { + if (shouldEncode(character)) { + encodedString.append('%') + val charHexValue = Integer.toHexString(character).toUpperCase + if (charHexValue.length < 2) + encodedString.append('0') + + encodedString.append(charHexValue) + + } else { + encodedString.append(character) + } + } + encodedString.toString() + } + + def shouldEncode(ch: Char): Boolean = { + if (ch > 128 || ch < 0) true + else " %$&+,./:;=?@<>#%".indexOf(ch) >= 0; + } +} diff --git a/kamon-statsd/src/main/scala/kamon/statsd/StatsD.scala b/kamon-statsd/src/main/scala/kamon/statsd/StatsD.scala index 9c1ccbb0..c8f647a8 100644 --- a/kamon-statsd/src/main/scala/kamon/statsd/StatsD.scala +++ b/kamon-statsd/src/main/scala/kamon/statsd/StatsD.scala @@ -34,8 +34,6 @@ object StatsD extends ExtensionId[StatsDExtension] with ExtensionIdProvider { override def createExtension(system: ExtendedActorSystem): StatsDExtension = new StatsDExtension(system) trait MetricKeyGenerator { - def localhostName: String - def normalizedLocalhostName: String def generateKey(groupIdentity: MetricGroupIdentity, metricIdentity: MetricIdentity): String } } @@ -107,40 +105,4 @@ class StatsDExtension(system: ExtendedActorSystem) extends Kamon.Extension { system.actorOf(TickMetricSnapshotBuffer.props(flushInterval.toInt.millis, metricsSender), "statsd-metrics-buffer") } } -} - -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 hostnameOverride = - config.getString("kamon.statsd.simple-metric-key-generator.hostname-override") - - val _localhostName = ManagementFactory.getRuntimeMXBean.getName.split('@')(1) - val _normalizedLocalhostName = _localhostName.replace('.', '_') - - def localhostName: String = _localhostName - - def normalizedLocalhostName: String = _normalizedLocalhostName - - val hostname: String = - if (hostnameOverride == "none") normalizedLocalhostName - else hostnameOverride - - val baseName: String = - if (includeHostnameInMetrics) s"${application}.${hostname}" - 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)) key - else s"${key}.${metricIdentity.name}" - } - - def isUserMetric(groupIdentity: MetricGroupIdentity): Boolean = groupIdentity match { - case someUserMetric: UserMetricGroup ⇒ true - case everythingElse ⇒ false - } -} +}
\ No newline at end of file |