Skip to content

Commit

Permalink
metrics tags
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetkhanduri committed Sep 2, 2023
1 parent 7909eb9 commit 012fc9e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import scala.jdk.CollectionConverters.ListHasAsScala
import scala.util.Try

class InMemoryDifferenceCounter(name: String) extends DifferenceCounter {
lazy val receiver = MetricsReceiver.root.get(name)
lazy val receiver = MetricsReceiver.root.withNameToken(name)

val endpointsMap: mutable.Map[String, InMemoryEndpointMetadata] = mutable.Map.empty

protected[this] def endpointCollector(ep: String) =
endpointsMap.getOrElseUpdate(ep, new InMemoryEndpointMetadata(receiver.get(ep)))
endpointsMap.getOrElseUpdate(ep, new InMemoryEndpointMetadata(receiver.withAdditionalTags(Map("endpoint" -> ep))))

override def endpoints: Map[String, EndpointMetadata] =
endpointsMap.toMap filter { _._2.total > 0 }
Expand All @@ -29,8 +29,8 @@ class InMemoryDifferenceCounter(name: String) extends DifferenceCounter {
}

class InMemoryFieldMetadata(receiver: MetricsReceiver) extends FieldMetadata {
val differenceCounter = receiver.get("differences").counter
val siblingsCounter = receiver.get("siblings").counter
val differenceCounter = receiver.withNameToken("differences").counter
val siblingsCounter = receiver.withNameToken("siblings").counter

val Seq(diffsAtomic,sibsAtomic) = Seq.fill(2)(new AtomicInteger(0))
def differences = diffsAtomic.get()//differenceCounter.count().toInt
Expand All @@ -46,8 +46,8 @@ class InMemoryFieldMetadata(receiver: MetricsReceiver) extends FieldMetadata {
}

class InMemoryEndpointMetadata(receiver: MetricsReceiver) extends EndpointMetadata {
val totalCounter = receiver.get("all").counter
val differenceCounter = receiver.get("different").counter
val totalCounter = receiver.withNameToken("all").counter
val differenceCounter = receiver.withNameToken("different").counter

val totalAtomic = new AtomicInteger(0)
val differenceAtomic = new AtomicInteger(0)
Expand All @@ -59,7 +59,7 @@ class InMemoryEndpointMetadata(receiver: MetricsReceiver) extends EndpointMetada

def getMetadata(field: String): InMemoryFieldMetadata = {
if (!_fields.contains(field)) {
_fields += (field -> new InMemoryFieldMetadata(receiver.get(field)))
_fields += (field -> new InMemoryFieldMetadata(receiver.withAdditionalTags(Map("field" -> field))))
}
_fields(field)
}
Expand Down
26 changes: 16 additions & 10 deletions src/main/scala/ai/diffy/metrics/MetricsReceiver.scala
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package ai.diffy.metrics

import ai.diffy.util.Memoize
import io.micrometer.core.instrument.{Counter, Metrics}
import io.micrometer.core.instrument.{Counter, Metrics, Tag, Tags}

import scala.jdk.CollectionConverters.IterableHasAsJava
import scala.language.postfixOps

object MetricsReceiver {
private class MemoizedMetricsReceiver(val tokens: Seq[String]) extends MetricsReceiver {
val name = tokens map { _.substring(0,1) } reduce {
_ + "." + _
}
private class MemoizedMetricsReceiver(val name: String, val tags: Map[String, String]) extends MetricsReceiver {

override lazy val counter = Metrics.globalRegistry.counter(name, Tags.of(tags map {case (k, v) => Tag.of(k,v) } asJava))

override lazy val counter = Metrics.globalRegistry.counter(name.take(63))
override def withNameToken(token: String): MetricsReceiver =
new MemoizedMetricsReceiver(name + "_" + token, tags)

override def get(name: String): MetricsReceiver = new MemoizedMetricsReceiver(tokens :+ name)
override def withAdditionalTags(additionalTags: Map[String, String]): MetricsReceiver =
new MemoizedMetricsReceiver(name, tags ++ additionalTags)
}

private def apply: Seq[String] => MetricsReceiver = Memoize { new MemoizedMetricsReceiver(_) }
def root: MetricsReceiver = apply(Seq("diffy"))
type MetricsConstructionArgs = (String, Map[String, String])
private def apply: MetricsConstructionArgs => MetricsReceiver = Memoize { args => new MemoizedMetricsReceiver(args._1, args._2) }
def root: MetricsReceiver = apply("diffy", Map.empty[String, String])
}

trait MetricsReceiver {
def get(name: String): MetricsReceiver
def withNameToken(name: String): MetricsReceiver
def withAdditionalTags(tags: Map[String, String]): MetricsReceiver
def counter: Counter
}
2 changes: 1 addition & 1 deletion src/main/scala/ai/diffy/util/Memoize.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Memoize {
def apply[A, B](function: A => B): A => B = {
val map = new ConcurrentHashMap[A, B]()
(a: A) => {
map.putIfAbsent(a, function(a))
map.computeIfAbsent(a, function.apply)
map.get(a)
}
}
Expand Down

0 comments on commit 012fc9e

Please sign in to comment.