-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding MetricDefinition Key Generator (#78)
- Loading branch information
1 parent
4c963d9
commit 2352eda
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/main/scala/com/expedia/www/haystack/commons/util/MetricDefinitionKeyGenerator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(",") | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/test/scala/com/expedia/www/haystack/commons/util/MetricDefinitionKeyGeneratorSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |