Skip to content

Commit

Permalink
Scala 3 support (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
arixmkii authored Oct 25, 2021
1 parent 913f2d7 commit 56a4a16
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: false
matrix:
java: ['adopt@1.8', 'adopt@1.11']
scala: ['2.12.14', '2.13.6']
scala: ['2.12.14', '2.13.6', '3.1.0']
steps:
- uses: actions/checkout@v2.3.4
- uses: olafurpg/setup-scala@v10
Expand Down
25 changes: 16 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ inThisBuild(
Test / fork := true
run / fork := true

val http4sVersion = "0.21.24"
val zioVersion = "1.0.9"
val http4sVersion = "0.22.7"
val zioVersion = "1.0.12"
val interopVersion = "2.5.1.0"
val catsEffectVersion = "2.5.1"
val catsEffectVersion = "2.5.4"
//val zioNIOVersion = "1.0.0-RC11"
val prometheusVersion = "0.11.0"
val prometheusVersion = "0.12.0"
val dropwizardVersion = "4.2.0"
val circeVersion = "0.14.1"

Expand Down Expand Up @@ -115,6 +115,7 @@ lazy val docs = project
publish / skip := true,
// skip 2.13 mdoc until mdoc is available for 2.13
crossScalaVersions -= Scala213,
crossScalaVersions -= Scala3,
moduleName := "zio-metrics-docs",
scalacOptions -= "-Yno-imports",
scalacOptions -= "-Xfatal-warnings",
Expand All @@ -124,11 +125,17 @@ lazy val docs = project
.enablePlugins(MdocPlugin, DocusaurusPlugin)

lazy val settings = Seq(
scalacOptions ++= (CrossVersion.partialVersion(scalaBinaryVersion.value) match {
case Some((2, 11)) => Seq("-Ypartial-unification", "-Ywarn-value-discard", "-target:jvm-1.8")
case Some((2, 13)) => Seq("-Ywarn-value-discard", "-target:jvm-1.8")
case _ => Seq("-Ypartial-unification", "-Ywarn-value-discard")
})
scalacOptions ++=
(CrossVersion.partialVersion(scalaVersion.value) match {
case Some((3, _)) =>
Seq() // Seq("-release:8") has issue running with JDK8 https://github.com/lampepfl/dotty/issues/13810
case _ =>
(CrossVersion.partialVersion(scalaBinaryVersion.value) match {
case Some((2, 11)) => Seq("-Ypartial-unification", "-Ywarn-value-discard", "-target:jvm-1.8")
case Some((2, 13)) => Seq("-Ywarn-value-discard", "-target:jvm-1.8")
case _ => Seq("-Ypartial-unification", "-Ywarn-value-discard")
})
})
)

lazy val http4s = Seq(
Expand Down
23 changes: 23 additions & 0 deletions common/src/main/scala-2/zio.metrics/Show.scala
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)
}
24 changes: 24 additions & 0 deletions common/src/main/scala-3/zio.metrics/Show.scala
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)
}

27 changes: 0 additions & 27 deletions common/src/main/scala/zio/metrics/typeclasses.scala
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
package zio.metrics

import scala.math.Numeric
import scala.math.Numeric.Implicits._

final case class Label[A](name: A, labels: Array[String], help: String)

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)
}

trait Semigroup[A] {
def combine(x: A, y: A): A
}
Expand All @@ -42,7 +17,5 @@ object Semigroup {
def |+|(y: A): A = self.combine(y)
}

implicit def numericAddSG[N: Numeric[*]]: Semigroup[N] = _ + _

implicit val strConcatSG: Semigroup[String] = _ + _
}
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

}
3 changes: 1 addition & 2 deletions dropwizard/src/main/scala/zio/metrics/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import org.http4s.HttpRoutes
import org.http4s.Request
import org.http4s.Response
import org.http4s.circe._
import org.http4s.dsl.impl.Root
import org.http4s.dsl.io._
import org.http4s.server.blaze._
import org.http4s.blaze.server._
import zio.RIO
import zio.ZIO
import zio.blocking.Blocking
Expand Down
74 changes: 44 additions & 30 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,52 @@ import sbt.Keys._
object Build {
def stdSettings(prjName: String) = Seq(
name := s"zio-$prjName",
scalacOptions := stdOptions,
crossScalaVersions := Seq(Scala212, Scala213),
crossScalaVersions := Seq(Scala212, Scala213, Scala3),
ThisBuild / scalaVersion := Scala213,
scalacOptions := stdOptions ++ extraOptions(scalaVersion.value),
libraryDependencies ++=
Seq(
("com.github.ghik" % "silencer-lib" % SilencerVersion % Provided)
.cross(CrossVersion.full),
compilerPlugin(("com.github.ghik" % "silencer-plugin" % SilencerVersion).cross(CrossVersion.full)),
compilerPlugin("org.typelevel" %% "kind-projector" % "0.10.3")
),
scalacOptions := stdOptions(scalaVersion.value) ++ extraOptions(scalaVersion.value),
libraryDependencies ++= (CrossVersion.partialVersion(scalaVersion.value) match {
case Some((3, _)) => Seq()
case _ => Seq(compilerPlugin("org.typelevel" %% "kind-projector" % "0.10.3"))
}),
incOptions ~= (_.withLogRecompileOnMacro(false))
)

val Scala212 = "2.12.14"
val Scala213 = "2.13.6"
val Scala3 = "3.1.0"

private val SilencerVersion = "1.7.5"

private val stdOptions = Seq(
"-encoding",
"UTF-8",
"-explaintypes",
"-Yrangepos",
"-feature",
"-language:higherKinds",
"-language:existentials",
"-Xlint:_,-type-parameter-shadow",
"-Xsource:2.13",
"-Ywarn-dead-code",
"-Ywarn-numeric-widen",
"-Ywarn-value-discard",
"-unchecked",
"-deprecation",
"-Xfatal-warnings"
)
private def stdOptions(scalaVersion: String) = CrossVersion.partialVersion(scalaVersion) match {
case Some((3, _)) =>
Seq(
"-encoding",
"utf8",
"-feature",
"-deprecation",
"-unchecked",
"-language:experimental.macros",
"-language:higherKinds",
"-language:implicitConversions",
"-Xfatal-warnings"
)
case _ =>
Seq(
"-encoding",
"UTF-8",
"-explaintypes",
"-Yrangepos",
"-feature",
"-language:higherKinds",
"-language:existentials",
"-Xlint:_,-type-parameter-shadow",
"-Xsource:2.13",
"-Ywarn-dead-code",
"-Ywarn-numeric-widen",
"-Ywarn-value-discard",
"-unchecked",
"-deprecation",
"-Xfatal-warnings"
)
}

private val stdOpts213 = Seq(
"-Wunused:imports",
Expand All @@ -48,7 +58,9 @@ object Build {
"-Wunused:privates",
"-Wunused:params",
"-Wvalue-discard",
"-Wdead-code"
"-Wdead-code",
"-Xsource:3",
"-Wconf:cat=unused-nowarn:s"
)

private val stdOptsUpto212 = Seq(
Expand All @@ -64,6 +76,8 @@ object Build {

private def extraOptions(scalaVersion: String) =
CrossVersion.partialVersion(scalaVersion) match {
case Some((3, _)) =>
Seq()
case Some((2, 13)) =>
stdOpts213
case Some((2, 12)) =>
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.5.3
sbt.version = 1.5.5
Loading

0 comments on commit 56a4a16

Please sign in to comment.