-
Notifications
You must be signed in to change notification settings - Fork 39
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
Showing
17 changed files
with
262 additions
and
88 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
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,23 @@ | ||
package zio.metrics | ||
|
||
trait Show[A] { | ||
def show(value: A): String | ||
} | ||
|
||
object Show { | ||
|
||
def apply[A](implicit sh: Show[A]): Show[A] = sh | ||
|
||
def show[A: Show](a: A): String = Show[A].show(a) | ||
|
||
def fixClassName[A](c: Class[A]): String = | ||
c.getName().replaceAll("\\.", "_").replace("$", "") | ||
|
||
implicit class ShowSyntax[A: Show](a: A) { | ||
def show(): String = Show[A].show(a) | ||
} | ||
|
||
implicit val showString: Show[String] = s => s | ||
|
||
implicit def showClass[A]: Show[Class[A]] = (f: Class[A]) => fixClassName(f) | ||
} |
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,24 @@ | ||
package zio.metrics | ||
|
||
trait Show[A] { | ||
def show(value: A): String | ||
} | ||
|
||
object Show { | ||
|
||
def apply[A](implicit sh: Show[A]): Show[A] = sh | ||
|
||
def show[A: Show](a: A): String = Show[A].show(a) | ||
|
||
def fixClassName[A](c: Class[A]): String = | ||
c.getName().replaceAll("\\.", "_").replace("$", "") | ||
|
||
implicit class ShowSyntax[A: Show](a: A) { | ||
def show(): String = Show[A].show(a) | ||
} | ||
|
||
implicit val showString: Show[String] = s => s | ||
|
||
implicit def showClass[A]: Show[Class[_]] = (f: Class[_]) => fixClassName(f) | ||
} | ||
|
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
132 changes: 132 additions & 0 deletions
132
dropwizard/src/main/scala-3/zio/metrics/dropwizard/DropwizardExtractor.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,132 @@ | ||
package zio.metrics.dropwizard | ||
|
||
import zio.{ RIO, Task } | ||
import com.codahale.metrics.Snapshot | ||
|
||
import scala.jdk.CollectionConverters._ | ||
|
||
import io.circe._ | ||
import io.circe.Json | ||
import com.codahale.metrics.MetricRegistry | ||
|
||
object DropwizardExtractor { | ||
|
||
implicit val jsonDWExtractor: Extractor[List, Json] = | ||
new Extractor[List, Json] { | ||
override val extractCounters: MetricRegistry => Filter => RIO[Registry, List[Json]] = registry => | ||
(filter: Filter) => { | ||
val metricFilter = Registry.makeFilter(filter) | ||
Task( | ||
registry | ||
.getCounters(metricFilter) | ||
.asScala | ||
.map(entry => Json.obj((entry._1, Json.fromLong(entry._2.getCount)))) | ||
.toList | ||
) | ||
} | ||
|
||
override val extractGauges: MetricRegistry => Filter => RIO[Registry, List[Json]] = registry => | ||
(filter: Filter) => { | ||
val metricFilter = Registry.makeFilter(filter) | ||
Task( | ||
registry | ||
.getGauges(metricFilter) | ||
.asScala | ||
.map(entry => Json.obj((entry._1, Json.fromString(entry._2.getValue.toString)))) | ||
.toList | ||
) | ||
} | ||
|
||
def extractSnapshot(name: String, snapshot: Snapshot): Json = | ||
Json.obj( | ||
(s"${name}_max", Json.fromLong(snapshot.getMax)), | ||
(s"${name}_min", Json.fromLong(snapshot.getMin)), | ||
(s"${name}_mean", Json.fromDoubleOrNull(snapshot.getMean)), | ||
(s"${name}_median", Json.fromDoubleOrNull(snapshot.getMedian)), | ||
(s"${name}_stdDev", Json.fromDoubleOrNull(snapshot.getStdDev)), | ||
(s"${name}_75th", Json.fromDoubleOrNull(snapshot.get75thPercentile())), | ||
(s"${name}_95th", Json.fromDoubleOrNull(snapshot.get95thPercentile())), | ||
(s"${name}_98th", Json.fromDoubleOrNull(snapshot.get98thPercentile())), | ||
(s"${name}_99th", Json.fromDoubleOrNull(snapshot.get99thPercentile())), | ||
(s"${name}_999th", Json.fromDoubleOrNull(snapshot.get999thPercentile())) | ||
) | ||
|
||
override val extractTimers: MetricRegistry => Filter => RIO[Registry, List[Json]] = registry => | ||
(filter: Filter) => { | ||
val metricFilter = Registry.makeFilter(filter) | ||
Task( | ||
registry | ||
.getTimers(metricFilter) | ||
.asScala | ||
.map(entry => { | ||
val j1 = Json.obj( | ||
(s"${entry._1}_count", Json.fromLong(entry._2.getCount)), | ||
(s"${entry._1}_meanRate", Json.fromDoubleOrNull(entry._2.getMeanRate)), | ||
(s"${entry._1}_oneMinRate", Json.fromDoubleOrNull(entry._2.getOneMinuteRate)), | ||
(s"${entry._1}_fiveMinRate", Json.fromDoubleOrNull(entry._2.getFiveMinuteRate)), | ||
(s"${entry._1}_fifteenMinRate", Json.fromDoubleOrNull(entry._2.getFifteenMinuteRate)) | ||
) //.deepmerge(extractSnapshot(entry._1, entry._2.getSnapshot)) | ||
val j2 = extractSnapshot(entry._1, entry._2.getSnapshot) | ||
j1.deepMerge(j2) | ||
}) | ||
.toList | ||
) | ||
} | ||
|
||
override val extractHistograms: MetricRegistry => Filter => RIO[Registry, List[Json]] = registry => | ||
(filter: Filter) => { | ||
val metricFilter = Registry.makeFilter(filter) | ||
Task( | ||
registry | ||
.getHistograms(metricFilter) | ||
.asScala | ||
.map(entry => { | ||
val jObj: JsonObject = extractSnapshot( | ||
entry._1, | ||
entry._2.getSnapshot | ||
).asObject.getOrElse(JsonObject.empty) | ||
|
||
Json.fromJsonObject((s"${entry._1}_count", Json.fromLong(entry._2.getCount)) +: jObj) | ||
}) | ||
.toList | ||
) | ||
} | ||
|
||
override val extractMeters: MetricRegistry => Filter => RIO[Registry, List[Json]] = registry => | ||
(filter: Filter) => { | ||
val metricFilter = Registry.makeFilter(filter) | ||
Task( | ||
registry | ||
.getMeters(metricFilter) | ||
.asScala | ||
.map(entry => { | ||
Json.obj( | ||
(s"${entry._1}_count", Json.fromLong(entry._2.getCount)), | ||
(s"${entry._1}_meanRate", Json.fromDoubleOrNull(entry._2.getMeanRate)), | ||
(s"${entry._1}_oneMinRate", Json.fromDoubleOrNull(entry._2.getOneMinuteRate)), | ||
(s"${entry._1}_fiveMinRate", Json.fromDoubleOrNull(entry._2.getFiveMinuteRate)), | ||
(s"${entry._1}_fifteenMinRate", Json.fromDoubleOrNull(entry._2.getFifteenMinuteRate)) | ||
) | ||
}) | ||
.toList | ||
) | ||
} | ||
} | ||
|
||
import cats.instances.list._ | ||
import zio.metrics.dropwizard.typeclasses._ | ||
|
||
object types { | ||
type Filter = Option[String] | ||
} | ||
import types._ | ||
|
||
val writeJson: MetricRegistry => Filter => Task[Json] = registry => | ||
filter => | ||
for { | ||
j <- RegistryPrinter.report[List, Json](registry, filter)( | ||
(k: String, v: Json) => Json.obj((k, v)) | ||
) | ||
} yield j | ||
|
||
} |
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
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 +1 @@ | ||
sbt.version = 1.5.3 | ||
sbt.version = 1.5.5 |
Oops, something went wrong.