Skip to content

Commit

Permalink
Adding MetricDefinition Key Generator (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
keshavpeswani authored and ayansen committed Nov 9, 2018
1 parent 4c963d9 commit 2352eda
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.expedia.www.haystack.commons.util

import com.expedia.metrics.{MetricDefinition, TagCollection}

import scala.collection.JavaConverters._
import scala.collection.immutable.ListMap

object MetricDefinitionKeyGenerator {

def generateKey(metricDefinition: MetricDefinition): String = {
List(s"key=${metricDefinition.getKey}", getTagsAsString(metricDefinition.getTags),
getTagsAsString(metricDefinition.getMeta)).filter(!_.isEmpty).mkString(",")
}

def getTagsAsString(tags: TagCollection): String = {
ListMap(tags.getKv.asScala.toSeq.sortBy(_._1): _*).map(tag => s"${tag._1}=${tag._2}").mkString(",")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.expedia.www.haystack.commons.util

import java.util

import com.expedia.metrics.{MetricDefinition, TagCollection}
import com.expedia.www.haystack.commons.entities.TagKeys.PRODUCT_KEY
import com.expedia.www.haystack.commons.unit.UnitTestSpec

class MetricDefinitionKeyGeneratorSpec extends UnitTestSpec {
"Metric Definition Key Generator" should {
"generate a unique key based on key and tags in MetricDefinition" in {
Given("a Metric Definition")
val metricDefinition = getMetricDefinition

When("MetricDefinitionKeyGenerator is called")
val key = MetricDefinitionKeyGenerator.generateKey(metricDefinition)

Then("a unique key is generated")
key should equal("key=duration,mtype=gauge,op=some-op,product=haystack,svc=some-svc,unit=short")
}
}

private def getMetricDefinition: MetricDefinition = {
val metricTags = new util.LinkedHashMap[String, String] {
put("svc", "some-svc")
put("op", "some-op")
}
val tags = new util.LinkedHashMap[String, String] {
putAll(metricTags)
put(MetricDefinition.MTYPE, "gauge")
put(MetricDefinition.UNIT, "short")
put(PRODUCT_KEY, "haystack")
}
val tc = new TagCollection(tags)
new MetricDefinition("duration", tc, TagCollection.EMPTY)
}
}

0 comments on commit 2352eda

Please sign in to comment.