-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: merge cats2 and cats3 repos into one
- Loading branch information
Showing
57 changed files
with
560 additions
and
3 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
scala-effect-api-cats2/src/main/scala/com/avast/metrics/scalaeffectapi/GaugeFactory.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,7 @@ | ||
package com.avast.metrics.scalaeffectapi | ||
|
||
trait GaugeFactory[F[_]] { | ||
def settableLong(name: String, replaceExisting: Boolean = false): SettableGauge[F, Long] | ||
def settableDouble(name: String, replaceExisting: Boolean = false): SettableGauge[F, Double] | ||
def generic[T](name: String, replaceExisting: Boolean = false)(gauge: () => T): Gauge[F, T] | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
53 changes: 53 additions & 0 deletions
53
...ect-api-cats2/src/main/scala/com/avast/metrics/scalaeffectapi/impl/GaugeFactoryImpl.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,53 @@ | ||
package com.avast.metrics.scalaeffectapi.impl | ||
|
||
import cats.effect.Sync | ||
import com.avast.metrics.scalaapi.{Monitor => SMonitor} | ||
import com.avast.metrics.scalaeffectapi.{Gauge, GaugeFactory, SettableGauge} | ||
|
||
import java.util.concurrent.atomic.{AtomicLong, AtomicReference} | ||
|
||
private class GaugeFactoryImpl[F[_]: Sync](monitor: SMonitor) extends GaugeFactory[F] { | ||
|
||
override def settableLong(gaugeName: String, replaceExisting: Boolean = false): SettableGauge[F, Long] = new SettableGauge[F, Long] { | ||
private[this] val valueRef = new AtomicLong(0L) | ||
private[this] val gauge = monitor.gauge(gaugeName, replaceExisting)(valueRef.get) | ||
|
||
override def set(value: Long): F[Unit] = Sync[F].delay(valueRef.set(value)) | ||
|
||
override def name: String = gauge.name | ||
|
||
override def value: F[Long] = Sync[F].delay(gauge.value) | ||
|
||
override def update(f: Long => Long): F[Long] = Sync[F].delay(valueRef.updateAndGet(f(_))) | ||
|
||
override def inc: F[Long] = Sync[F].delay(valueRef.incrementAndGet()) | ||
|
||
override def dec: F[Long] = Sync[F].delay(valueRef.decrementAndGet()) | ||
} | ||
|
||
override def settableDouble(gaugeName: String, replaceExisting: Boolean = false): SettableGauge[F, Double] = | ||
new SettableGauge[F, Double] { | ||
private[this] val valueRef = new AtomicReference(0.0) | ||
private[this] val gauge = monitor.gauge(gaugeName, replaceExisting)(valueRef.get) | ||
|
||
override def set(value: Double): F[Unit] = Sync[F].delay(valueRef.set(value)) | ||
|
||
override def name: String = gauge.name | ||
|
||
override def value: F[Double] = Sync[F].delay(gauge.value) | ||
|
||
override def update(f: Double => Double): F[Double] = Sync[F].delay(valueRef.updateAndGet(f(_))) | ||
|
||
override def inc: F[Double] = Sync[F].delay(valueRef.accumulateAndGet(1, (a, b) => a + b)) | ||
|
||
override def dec: F[Double] = Sync[F].delay(valueRef.accumulateAndGet(1, (a, b) => a - b)) | ||
} | ||
|
||
override def generic[T](gaugeName: String, replaceExisting: Boolean = false)(gauge: () => T): Gauge[F, T] = new Gauge[F, T] { | ||
private[this] val sGauge = monitor.gauge(gaugeName, replaceExisting)(gauge) | ||
|
||
override def value: F[T] = Sync[F].delay(sGauge.value) | ||
|
||
override def name: String = sGauge.name | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
...api-cats2/src/main/scala/com/avast/metrics/scalaeffectapi/perkey/PerKeyGaugeFactory.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,9 @@ | ||
package com.avast.metrics.scalaeffectapi.perkey | ||
|
||
import com.avast.metrics.scalaeffectapi.{Gauge, SettableGauge} | ||
|
||
trait PerKeyGaugeFactory[F[_]] { | ||
def settableLong(baseName: String, replaceExisting: Boolean = false): PerKeyMetric[SettableGauge[F, Long]] | ||
def settableDouble(baseName: String, replaceExisting: Boolean = false): PerKeyMetric[SettableGauge[F, Double]] | ||
def generic[T](baseName: String, replaceExisting: Boolean = false)(gauge: () => T): PerKeyMetric[Gauge[F, T]] | ||
} |
5 changes: 5 additions & 0 deletions
5
...ffect-api-cats2/src/main/scala/com/avast/metrics/scalaeffectapi/perkey/PerKeyMetric.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,5 @@ | ||
package com.avast.metrics.scalaeffectapi.perkey | ||
|
||
trait PerKeyMetric[M] { | ||
def forKey(str: String): M | ||
} |
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions
28
.../src/main/scala/com/avast/metrics/scalaeffectapi/perkey/impl/PerKeyGaugeFactoryImpl.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,28 @@ | ||
package com.avast.metrics.scalaeffectapi.perkey.impl | ||
|
||
import com.avast.metrics.scalaeffectapi.{Gauge, Monitor, SettableGauge} | ||
import com.avast.metrics.scalaeffectapi.perkey.{PerKeyGaugeFactory, PerKeyMetric} | ||
|
||
import scala.collection.concurrent.TrieMap | ||
|
||
class PerKeyGaugeFactoryImpl[F[_]](monitor: Monitor[F]) extends PerKeyGaugeFactory[F] { | ||
private def emptyMap[M] = TrieMap.empty[String, M] | ||
|
||
override def settableLong(baseName: String, replaceExisting: Boolean = false): PerKeyMetric[SettableGauge[F, Long]] = { | ||
val instanceBuilder = monitor.named(baseName) | ||
new PerKeyMetricImpl[SettableGauge[F, Long]](emptyMap[SettableGauge[F, Long]], instanceBuilder.gauge.settableLong(_, replaceExisting)) | ||
} | ||
|
||
override def settableDouble(baseName: String, replaceExisting: Boolean = false): PerKeyMetric[SettableGauge[F, Double]] = { | ||
val instanceBuilder = monitor.named(baseName) | ||
new PerKeyMetricImpl[SettableGauge[F, Double]]( | ||
emptyMap[SettableGauge[F, Double]], | ||
instanceBuilder.gauge.settableDouble(_, replaceExisting) | ||
) | ||
} | ||
|
||
override def generic[T](baseName: String, replaceExisting: Boolean = false)(gauge: () => T): PerKeyMetric[Gauge[F, T]] = { | ||
val instanceBuilder = monitor.named(baseName) | ||
new PerKeyMetricImpl[Gauge[F, T]](emptyMap[Gauge[F, T]], instanceBuilder.gauge.generic(_, replaceExisting)(gauge)) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...-cats2/src/main/scala/com/avast/metrics/scalaeffectapi/perkey/impl/PerKeyMetricImpl.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,11 @@ | ||
package com.avast.metrics.scalaeffectapi.perkey.impl | ||
|
||
import com.avast.metrics.scalaeffectapi.perkey.PerKeyMetric | ||
|
||
import scala.collection.concurrent.{Map => CMap} | ||
|
||
private[perkey] class PerKeyMetricImpl[A](map: CMap[String, A], metricBuilder: String => A) extends PerKeyMetric[A] { | ||
override def forKey(str: String): A = { | ||
map.getOrElseUpdate(str, metricBuilder(str)) | ||
} | ||
} |
File renamed without changes.
40 changes: 40 additions & 0 deletions
40
...api-cats2/src/main/scala/com/avast/metrics/scalaeffectapi/perkey/impl/PerKeyOpsImpl.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,40 @@ | ||
package com.avast.metrics.scalaeffectapi.perkey.impl | ||
|
||
import com.avast.metrics.scalaeffectapi._ | ||
import com.avast.metrics.scalaeffectapi.perkey.{PerKeyGaugeFactory, PerKeyMetric, PerKeyOps} | ||
|
||
import scala.collection.concurrent.TrieMap | ||
|
||
private[perkey] class PerKeyOpsImpl[F[_]](monitor: Monitor[F]) extends PerKeyOps[F] { | ||
private def emptyMap[M] = TrieMap.empty[String, M] | ||
|
||
override def meter(baseName: String): PerKeyMetric[Meter[F]] = { | ||
val instanceBuilder = monitor.named(baseName) | ||
new PerKeyMetricImpl[Meter[F]](emptyMap[Meter[F]], instanceBuilder.meter) | ||
} | ||
|
||
override def counter(baseName: String): PerKeyMetric[Counter[F]] = { | ||
val instanceBuilder = monitor.named(baseName) | ||
new PerKeyMetricImpl[Counter[F]](emptyMap, instanceBuilder.counter) | ||
} | ||
|
||
override def timer(baseName: String): PerKeyMetric[Timer[F]] = { | ||
val instanceBuilder = monitor.named(baseName) | ||
new PerKeyMetricImpl[Timer[F]](emptyMap, instanceBuilder.timer) | ||
} | ||
|
||
override def timerPair(baseName: String): PerKeyMetric[TimerPair[F]] = { | ||
val instanceBuilder = monitor.named(baseName) | ||
new PerKeyMetricImpl[TimerPair[F]](emptyMap, instanceBuilder.timerPair) | ||
} | ||
|
||
override def histogram(baseName: String): PerKeyMetric[Histogram[F]] = { | ||
val instanceBuilder = monitor.named(baseName) | ||
new PerKeyMetricImpl[Histogram[F]](emptyMap, instanceBuilder.histogram) | ||
} | ||
|
||
override def gauge: PerKeyGaugeFactory[F] = { | ||
new PerKeyGaugeFactoryImpl[F](monitor) | ||
} | ||
|
||
} |
File renamed without changes.
26 changes: 26 additions & 0 deletions
26
scala-effect-api-cats2/src/test/scala/com/avast/metrics/examples/EffectMonitor.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,26 @@ | ||
package com.avast.metrics.examples | ||
|
||
import cats.effect.{ExitCode, IO, IOApp, Timer} | ||
import com.avast.metrics.dropwizard.JmxMetricsMonitor | ||
import com.avast.metrics.scalaeffectapi.Monitor | ||
|
||
import java.util.concurrent.TimeUnit | ||
import scala.concurrent.duration.FiniteDuration | ||
|
||
object EffectMonitor extends IOApp { | ||
|
||
val jmxMetricsMonitor = new JmxMetricsMonitor("com.avast.some.app") | ||
|
||
override def run(args: List[String]): IO[ExitCode] = { | ||
val monitor = Monitor.wrapJava[IO](jmxMetricsMonitor) | ||
val counter = monitor.counter("counter") | ||
val timer = monitor.timer("timer") | ||
|
||
for { | ||
_ <- counter.inc | ||
_ <- timer.time { | ||
Timer[IO](IO.timer(executionContext)).sleep(FiniteDuration(500, TimeUnit.MILLISECONDS)) | ||
} | ||
} yield ExitCode.Success | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
scala-effect-api-cats3/src/main/scala/com/avast/metrics/scalaeffectapi/Counter.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,10 @@ | ||
package com.avast.metrics.scalaeffectapi | ||
|
||
import com.avast.metrics.scalaapi.Metric | ||
|
||
trait Counter[F[_]] extends Counting[F] with Metric { | ||
def inc: F[Unit] | ||
def inc(n: Long): F[Unit] | ||
def dec: F[Unit] | ||
def dec(n: Int): F[Unit] | ||
} |
5 changes: 5 additions & 0 deletions
5
scala-effect-api-cats3/src/main/scala/com/avast/metrics/scalaeffectapi/Counting.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,5 @@ | ||
package com.avast.metrics.scalaeffectapi | ||
|
||
trait Counting[F[_]] { | ||
def count: F[Long] | ||
} |
7 changes: 7 additions & 0 deletions
7
scala-effect-api-cats3/src/main/scala/com/avast/metrics/scalaeffectapi/Gauge.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,7 @@ | ||
package com.avast.metrics.scalaeffectapi | ||
|
||
import com.avast.metrics.scalaapi.Metric | ||
|
||
trait Gauge[F[_], T] extends Metric { | ||
def value: F[T] | ||
} |
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
scala-effect-api-cats3/src/main/scala/com/avast/metrics/scalaeffectapi/Histogram.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,7 @@ | ||
package com.avast.metrics.scalaeffectapi | ||
|
||
import com.avast.metrics.scalaapi.Metric | ||
|
||
trait Histogram[F[_]] extends Metric { | ||
def update(value: Long): F[Unit] | ||
} |
8 changes: 8 additions & 0 deletions
8
scala-effect-api-cats3/src/main/scala/com/avast/metrics/scalaeffectapi/Meter.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,8 @@ | ||
package com.avast.metrics.scalaeffectapi | ||
|
||
import com.avast.metrics.scalaapi.Metric | ||
|
||
trait Meter[F[_]] extends Counting[F] with Metric { | ||
def mark: F[Unit] | ||
def mark(n: Long): F[Unit] | ||
} |
36 changes: 36 additions & 0 deletions
36
scala-effect-api-cats3/src/main/scala/com/avast/metrics/scalaeffectapi/Monitor.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,36 @@ | ||
package com.avast.metrics.scalaeffectapi | ||
|
||
import cats.effect.Sync | ||
import com.avast.metrics.api.{Monitor => JMonitor, Naming} | ||
import com.avast.metrics.scalaapi.{Monitor => SMonitor} | ||
import com.avast.metrics.test.NoOpMonitor | ||
|
||
trait Monitor[F[_]] extends AutoCloseable { | ||
def named(name: String): Monitor[F] | ||
def named(name1: String, name2: String, restOfNames: String*): Monitor[F] | ||
def getName: String | ||
def meter(name: String): Meter[F] | ||
def counter(name: String): Counter[F] | ||
def timer(name: String): Timer[F] | ||
def timerPair(name: String): TimerPair[F] | ||
def histogram(name: String): Histogram[F] | ||
|
||
def gauge: GaugeFactory[F] | ||
|
||
def asJava: JMonitor | ||
def asPlainScala: SMonitor | ||
} | ||
|
||
object Monitor { | ||
|
||
def wrapJava[F[_]: Sync](monitor: JMonitor): Monitor[F] = wrapJava(monitor, Naming.defaultNaming()) | ||
def wrapJava[F[_]: Sync](monitor: JMonitor, naming: Naming): Monitor[F] = wrap(SMonitor(monitor), naming) | ||
|
||
def wrap[F[_]: Sync](monitor: SMonitor): Monitor[F] = wrap(monitor, Naming.defaultNaming()) | ||
def wrap[F[_]: Sync](monitor: SMonitor, naming: Naming): Monitor[F] = new impl.MonitorImpl(monitor, naming) | ||
|
||
def noOp[F[_]: Sync](): Monitor[F] = { | ||
wrapJava(NoOpMonitor.INSTANCE) | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
scala-effect-api-cats3/src/main/scala/com/avast/metrics/scalaeffectapi/SettableGauge.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,8 @@ | ||
package com.avast.metrics.scalaeffectapi | ||
|
||
trait SettableGauge[F[_], T] extends Gauge[F, T] { | ||
def set(value: T): F[Unit] | ||
def update(f: T => T): F[T] | ||
def inc: F[T] | ||
def dec: F[T] | ||
} |
15 changes: 15 additions & 0 deletions
15
scala-effect-api-cats3/src/main/scala/com/avast/metrics/scalaeffectapi/Timer.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,15 @@ | ||
package com.avast.metrics.scalaeffectapi | ||
|
||
import java.time.{Duration => JDuration} | ||
import scala.concurrent.duration.Duration | ||
|
||
trait Timer[F[_]] { | ||
trait TimeContext extends AutoCloseable { | ||
def stop: F[Duration] | ||
} | ||
|
||
def start: F[TimeContext] | ||
def update(duration: JDuration): F[Unit] | ||
def update(duration: Duration): F[Unit] | ||
def time[A](block: F[A]): F[A] | ||
} |
19 changes: 19 additions & 0 deletions
19
scala-effect-api-cats3/src/main/scala/com/avast/metrics/scalaeffectapi/TimerPair.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,19 @@ | ||
package com.avast.metrics.scalaeffectapi | ||
|
||
import java.time.{Duration => JDuration} | ||
import scala.concurrent.duration.Duration | ||
|
||
trait TimerPair[F[_]] { | ||
trait TimerPairContext extends AutoCloseable { | ||
def stop: F[Duration] | ||
def stopFailure: F[Duration] | ||
} | ||
|
||
def start: F[TimerPairContext] | ||
def update(duration: JDuration): F[Unit] | ||
def updateFailure(duration: JDuration): F[Unit] | ||
def update(duration: Duration): F[Unit] | ||
def updateFailure(duration: Duration): F[Unit] | ||
def time[T](action: F[T]): F[T] | ||
def time[T](action: F[T])(successCheck: T => Boolean): F[T] | ||
} |
19 changes: 19 additions & 0 deletions
19
...a-effect-api-cats3/src/main/scala/com/avast/metrics/scalaeffectapi/impl/CounterImpl.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,19 @@ | ||
package com.avast.metrics.scalaeffectapi.impl | ||
|
||
import cats.effect.Sync | ||
import com.avast.metrics.scalaapi.{Counter => SCounter} | ||
import com.avast.metrics.scalaeffectapi.Counter | ||
|
||
private class CounterImpl[F[_]: Sync](inner: SCounter) extends Counter[F] { | ||
override def inc: F[Unit] = Sync[F].delay(inner.inc()) | ||
|
||
override def inc(n: Long): F[Unit] = Sync[F].delay(inner.inc(n)) | ||
|
||
override def dec: F[Unit] = Sync[F].delay(inner.dec()) | ||
|
||
override def dec(n: Int): F[Unit] = Sync[F].delay(inner.dec(n)) | ||
|
||
override def count: F[Long] = Sync[F].delay(inner.count) | ||
|
||
override def name: String = inner.name | ||
} |
File renamed without changes.
Oops, something went wrong.