Skip to content

Instantly share code, notes, and snippets.

@atooni
Last active December 2, 2020 15:12
Show Gist options
  • Save atooni/6e7892bc9b00878f745b7b0743782c47 to your computer and use it in GitHub Desktop.
Save atooni/6e7892bc9b00878f745b7b0743782c47 to your computer and use it in GitHub Desktop.
Draft for effects with metrics
package zio.zmx
import zio._
package object metrics extends MetricsDataModel {
type ZMetrics = Has[ZMetrics.Service]
// A generic metrics service
object ZMetrics {
trait Service {
def counter(name: String): ZIO[Any, Nothing, Option[Metric.Counter]]
def increment(m: Metric.Counter): ZIO[Any, Nothing, Option[Metric.Counter]]
}
def counter(name: String) = ZIO.accessM[ZMetrics](_.get.counter(name))
def increment(m: Metric.Counter) = ZIO.accessM[ZMetrics](_.get.increment(m))
def count[R, E, A](name: String)(e: ZIO[R, E, A]): ZIO[R with ZMetrics, Any, A] = for {
cnt <- counter(name)
r <- e
_ <- ZIO.foreach(cnt)(cnt => increment(cnt))
} yield r
}
}
object foo {
import metrics._
def doSomething = ZMetrics.count("myCounter")(ZIO.succeed(()))
def countSomething = ZIO.foreach(1.to(100))(_ => doSomething)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment