Skip to content

Commit

Permalink
Split projects into modules
Browse files Browse the repository at this point in the history
* Update libraries (ZIO HTTP & ZIO JSON)
* Update Scala 3 version
* Update SBT
  • Loading branch information
calvinlfer committed Jun 12, 2023
1 parent 9e1aacc commit c93c8b2
Show file tree
Hide file tree
Showing 30 changed files with 118 additions and 73 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.17", "2.13.10", "3.2.1"]
scala: ["2.12.17", "2.13.10", "3.2.2"]
steps:
- uses: actions/checkout@v3
- uses: olafurpg/setup-scala@v13
Expand Down
65 changes: 57 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,78 @@ addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")

lazy val root =
project.in(file(".")).settings(publish / skip := true).aggregate(core, docs)
project
.in(file("."))
.settings(publish / skip := true)
.aggregate(core, statsd, datadog, newrelic, prometheus, docs)

lazy val core =
project
.in(file("core"))
.settings(
run / fork := true,
Test / run / javaOptions += "-Djava.net.preferIPv4Stack=true",
Test / run / mainClass := Some("zio.sample.SampleApp"),
cancelable := true,
stdSettings("zio.metrics.connectors"),
libraryDependencies ++= Seq(
"dev.zio" %%% "zio" % Version.zio,
"dev.zio" %%% "zio-json" % Version.zioJson,
"dev.zio" %%% "zio-streams" % Version.zio,
"dev.zio" %% "zio-http" % Version.zioHttp,
"dev.zio" %%% "zio-test" % Version.zio % Test,
"dev.zio" %%% "zio-test-sbt" % Version.zio % Test,
),
)
.settings(buildInfoSettings("zio.metrics.connectors"))
.enablePlugins(BuildInfoPlugin)

lazy val statsd =
project
.in(file("statsd"))
.settings(stdSettings("zio.metrics.connectors.statsd"))
.enablePlugins(BuildInfoPlugin)
.dependsOn(core % "compile->compile;test->test")

lazy val datadog =
project
.in(file("datadog"))
.settings(stdSettings("zio.metrics.connectors.datadog"))
.settings(buildInfoSettings("zio.metrics.connectors.datadog"))
.enablePlugins(BuildInfoPlugin)
.dependsOn(
core % "compile->compile;test->test",
statsd % "compile->compile;test->test",
)

lazy val newrelic =
project
.in(file("newrelic"))
.settings(
stdSettings("zio.metrics.connectors.newrelic"),
libraryDependencies ++= Seq(
"dev.zio" %% "zio-http" % Version.zioHttp,
"dev.zio" %%% "zio-json" % Version.zioJson,
),
)
.settings(buildInfoSettings("zio.metrics.connectors.newrelic"))
.enablePlugins(BuildInfoPlugin)
.dependsOn(core % "compile->compile;test->test")

lazy val prometheus =
project
.in(file("prometheus"))
.settings(stdSettings("zio.metrics.connectors.prometheus"))
.settings(buildInfoSettings("zio.metrics.connectors.prometheus"))
.enablePlugins(BuildInfoPlugin)
.dependsOn(core % "compile->compile;test->test")

lazy val sampleApp =
project
.in(file("sample-app"))
.settings(
run / fork := true,
run / javaOptions += "-Djava.net.preferIPv4Stack=true",
libraryDependencies ++= Seq(
"dev.zio" %% "zio-http" % Version.zioHttp,
"dev.zio" %%% "zio-json" % Version.zioJson,
),
)
.dependsOn(statsd, prometheus)

lazy val docs = project
.in(file("zio-metrics-connectors-docs"))
.settings(
Expand Down
67 changes: 35 additions & 32 deletions core/src/test/scala/zio/metrics/connectors/Generators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import zio.metrics.MetricPair.Untyped
import zio.test._

trait Generators {
val genPosDouble = Gen.double(0.0, Double.MaxValue)
val genNegDouble = Gen.double(Double.MinValue, 0.0)
def genSomeDoubles(n: Int) = Gen.chunkOfBounded(1, n)(genPosDouble)
val genPosDouble: Gen[Any, Double] = Gen.double(0.0, Double.MaxValue)
val genNegDouble: Gen[Any, Double] = Gen.double(Double.MinValue, 0.0)
def genSomeDoubles(n: Int): Gen[Any, Chunk[Double]] = Gen.chunkOfBounded(1, n)(genPosDouble)

val genPosLong = Gen.long(0L, Long.MaxValue)
val genPosLong: Gen[Any, Long] = Gen.long(0L, Long.MaxValue)

val nonEmptyString = Gen.alphaNumericString.filter(_.nonEmpty)
val nonEmptyString: Gen[Any, String] = Gen.alphaNumericString.filter(_.nonEmpty)

val descriptionKey = "testDescription"

val genLabel =
val genLabel: Gen[Any, MetricLabel] =
Gen.oneOf(Gen.const(descriptionKey), Gen.alphaNumericString).zipWith(Gen.alphaNumericString)(MetricLabel.apply)

val genTags = Gen.setOf(genLabel)
val genTags: Gen[Any, Set[MetricLabel]] = Gen.setOf(genLabel)

val genCounter = for {
val genCounter: Gen[Any, (MetricPair.Untyped, MetricState.Counter)] = for {
name <- nonEmptyString
tags <- genTags
count <- Gen.double(1, 100)
Expand All @@ -30,25 +30,31 @@ trait Generators {
(Unsafe.unsafe(implicit u => MetricPair.make(MetricKey.counter(name).tagged(tags), state)), state)
}

def genCounterNamed(name: String, min: Double = 1.0, max: Double = 100) = for {
def genCounterNamed(
name: String,
min: Double = 1.0,
max: Double = 100,
): Gen[Any, (MetricPair.Untyped, MetricState.Counter)] = for {
count <- Gen.double(min, max)
tags <- genTags
} yield {
val state = MetricState.Counter(count)
(Unsafe.unsafe(implicit u => MetricPair.make(MetricKey.counter(name).tagged(tags), state)), state)
}

def genFrequency(count: Int, pastValues: Ref[Set[String]]) = for {
name <- nonEmptyString
tags <- genTags
occurrences <- Gen.listOfN(count)(unqiueNonEmptyString(pastValues).flatMap(occName => genPosLong.map(occName -> _)))
} yield {
val asMap = occurrences.toMap
val state = MetricState.Frequency(asMap)
(Unsafe.unsafe(implicit u => MetricPair.make(MetricKey.frequency(name).tagged(tags), state)), state)
}

val genFrequency1 = for {
def genFrequency(count: Int, pastValues: Ref[Set[String]]): Gen[Any, (MetricPair.Untyped, MetricState.Frequency)] =
for {
name <- nonEmptyString
tags <- genTags
occurrences <-
Gen.listOfN(count)(uniqueNonEmptyString(pastValues).flatMap(occName => genPosLong.map(occName -> _)))
} yield {
val asMap = occurrences.toMap
val state = MetricState.Frequency(asMap)
(Unsafe.unsafe(implicit u => MetricPair.make(MetricKey.frequency(name).tagged(tags), state)), state)
}

val genFrequency1: Gen[Any, (MetricPair.Untyped, MetricState.Frequency)] = for {
name <- nonEmptyString
tags <- genTags
occName <- nonEmptyString
Expand All @@ -59,7 +65,7 @@ trait Generators {
(Unsafe.unsafe(implicit u => MetricPair.make(MetricKey.frequency(name).tagged(tags), state)), state)
}

val genGauge: Gen[Any, (Untyped, MetricState.Gauge)] = for {
val genGauge: Gen[Any, (MetricPair.Untyped, MetricState.Gauge)] = for {
name <- nonEmptyString
tags <- genTags
count <- genPosDouble
Expand All @@ -68,7 +74,7 @@ trait Generators {
(Unsafe.unsafe(implicit u => MetricPair.make(MetricKey.counter(name).tagged(tags), state)), state)
}

def genHistogram = for {
def genHistogram: Gen[Any, (MetricPair.Untyped, MetricState.Histogram)] = for {
name <- nonEmptyString
tags <- genTags
count <- genPosLong
Expand All @@ -95,7 +101,7 @@ trait Generators {
(Unsafe.unsafe(implicit u => MetricPair.make(MetricKey.histogram(name, boundaries).tagged(tags), state)), state)
}

def genSummary = for {
def genSummary: Gen[Any, (MetricPair.Untyped, MetricState.Summary)] = for {
name <- nonEmptyString
tags <- genTags
count <- genPosLong
Expand All @@ -116,16 +122,13 @@ trait Generators {
)
}

def unqiueNonEmptyString(ref: Ref[Set[String]]) =
def uniqueNonEmptyString(ref: Ref[Set[String]]): Gen[Any, String] =
Gen(
nonEmptyString.sample.filterZIO {
case Some(s) =>
for {
exists <- ref.get.map(_.contains(s.value))
_ <- if (!exists) ref.update(_ + s.value) else ZIO.unit
} yield !exists
case _ => ZIO.succeed(true)
nonEmptyString.sample.filterZIO { sample =>
for {
exists <- ref.get.map(_.contains(sample.value))
_ <- ZIO.when(!exists)(ref.update(_ + sample.value))
} yield !exists
},
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zio.metrics.connectors.datadog
import zio.Unsafe
import zio.internal.RingBuffer
import zio.metrics.{MetricKey, MetricKeyType, MetricListener}
import zio.metrics.MetricKeyType.Gauge

class DataDogListener(queue: RingBuffer[(MetricKey[MetricKeyType.Histogram], Double)]) extends MetricListener {
def updateHistogram(key: MetricKey[MetricKeyType.Histogram], value: Double)(implicit unsafe: Unsafe): Unit = {
Expand All @@ -11,6 +12,8 @@ class DataDogListener(queue: RingBuffer[(MetricKey[MetricKeyType.Histogram], Dou

def updateGauge(key: MetricKey[MetricKeyType.Gauge], value: Double)(implicit unsafe: Unsafe): Unit = ()

def modifyGauge(key: MetricKey[Gauge], value: Double)(implicit unsafe: Unsafe): Unit = ()

def updateFrequency(key: MetricKey[MetricKeyType.Frequency], value: String)(implicit unsafe: Unsafe): Unit = ()

def updateSummary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ package object datadog {
clt <- StatsdClient.make.provideSome[Scope](ZLayer.succeed(StatsdConfig(config.host, config.port)))
queue = RingBuffer.apply[(MetricKey[MetricKeyType.Histogram], Double)](config.maxQueueSize)
listener = new DataDogListener(queue)
_ <- Unsafe.unsafe(implicit unsafe =>
ZIO.acquireRelease(ZIO.succeed(MetricClient.addListener(listener)))(_ =>
ZIO.succeed(MetricClient.removeListener(listener)),
_ <- Unsafe.unsafe(unsafe =>
ZIO.acquireRelease(ZIO.succeed(MetricClient.addListener(listener)(unsafe)))(_ =>
ZIO.succeed(MetricClient.removeListener(listener)(unsafe)),
),
)
_ <- DataDogEventProcessor.make(clt, queue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package zio.metrics.connectors.newrelic

import zio._
import zio.http._
import zio.http.model.{Header, Headers}
import zio.json.ast.Json
import zio.stream._

Expand Down Expand Up @@ -37,7 +36,7 @@ object NewRelicClient {
)
.toString

URL.fromString(cfg.newRelicURI.endpoint).map { url =>
URL.decode(cfg.newRelicURI.endpoint).map { url =>
Request
.post(
url = url,
Expand All @@ -59,9 +58,9 @@ object NewRelicClient {
.unit

private lazy val headers = Headers(
Header("Api-Key", cfg.apiKey),
Header("Content-Type", "application/json"),
Header("Accept", "*/*"),
Header.Custom("Api-Key", cfg.apiKey),
Header.ContentType(MediaType.application.json),
Header.Accept(MediaType.any),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package object newrelic {
ZLayer.makeSome[MetricsConfig & NewRelicConfig, Unit](
make,
Client.default.orDie,
Scope.default,
)

private lazy val make: URLayer[MetricsConfig & NewRelicConfig & Client, Unit] = ZLayer(for {
Expand Down
6 changes: 3 additions & 3 deletions project/BuildHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ object BuildHelper {

private def extraOptions(scalaVersion: String) =
CrossVersion.partialVersion(scalaVersion) match {
case Some((3, 1)) => dottyOptions
case Some((3, _)) => dottyOptions
case Some((2, 13)) =>
stdOpts213 ++ stdOpts2X
case Some((2, 12)) =>
Expand All @@ -86,11 +86,11 @@ object BuildHelper {
def stdSettings(prjName: String) =
Seq(
name := s"$prjName",
crossScalaVersions := Seq(Scala212, Scala213, ScalaDotty),
crossScalaVersions := Seq(Scala212, Scala213, Scala3),
ThisBuild / scalaVersion := Scala213,
scalacOptions := stdOptions ++ extraOptions(scalaVersion.value),
libraryDependencies ++= {
if (scalaVersion.value != ScalaDotty)
if (scalaVersion.value != Scala3)
Seq(
("com.github.ghik" % "silencer-lib" % silencerVersion(scalaVersion.value) % Provided)
.cross(CrossVersion.full),
Expand Down
14 changes: 7 additions & 7 deletions project/Version.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
object Version {
val Scala211 = "2.11.12"
val Scala212 = "2.12.17"
val Scala213 = "2.13.10"
val ScalaDotty = "3.2.1"
val Scala211 = "2.11.12"
val Scala212 = "2.12.17"
val Scala213 = "2.13.10"
val Scala3 = "3.2.2"

val zio = "2.0.4"
val zioJson = "0.3.0"
val zioHttp = "0.0.3"
val zio = "2.0.13"
val zioJson = "0.5.0"
val zioHttp = "3.0.0-RC1"
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.7.2
sbt.version = 1.8.3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zio.sample
package sample

import zio._
import zio.metrics._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package zio.sample
package sample

import java.net.InetSocketAddress

import zio._
import zio.http._
//import zio.metrics.connectors.newrelic.NewRelicConfig
import zio.http.html._
import zio.http.model.{Headers, Method}
import zio.metrics.connectors.{prometheus, statsd, MetricsConfig}
Expand Down Expand Up @@ -68,10 +67,6 @@ object SampleApp extends ZIOAppDefault with InstrumentedSample {
ZLayer.succeed(StatsdConfig("127.0.0.1", 8125)),
statsd.statsdLayer,

// The NewRelic reporting layer
// NewRelicConfig.fromEnvEULayer,
// newrelic.newRelicLayer,

// Enable the ZIO internal metrics and the default JVM metricsConfig
// Do NOT forget the .unit for the JVM metrics layer
Runtime.enableRuntimeMetrics,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.nio.channels.DatagramChannel

import scala.util.Failure
import scala.util.Success
import scala.util.Try
import scala.util.{Failure, Success, Try}

import zio._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zio.metrics.connectors.statsd

import zio._
import zio.ULayer
import zio.{ULayer, _}

final case class StatsdConfig(
host: String,
Expand Down

0 comments on commit c93c8b2

Please sign in to comment.