-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7909eb9
commit 012fc9e
Showing
3 changed files
with
24 additions
and
18 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -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 | ||
} |
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