Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed zio metric type for active requests from Count to Gauge. #2990

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ object ZioMetrics {
/** ZIO Default Runtime */
val runtime: Runtime[Any] = Runtime.default

/** Active/Inprogress Counter +1 active, -1 complete. */
def getActiveRequestCounter(namespace: String): Counter[Long] = zio.metrics.Metric.counter(s"${namespace}_request_active")
/** Active/Inprogress Gauge +1 active, -1 complete. */
def getActiveRequestGauge(namespace: String): Gauge[Long] =
zio.metrics.Metric.gauge(s"${namespace}_request_active").contramap(_.toDouble)

/** Total request counter. */
def getRequestsTotalCounter(namespace: String): Counter[Long] = zio.metrics.Metric.counter(s"${namespace}_request_total")
Expand All @@ -76,8 +77,8 @@ object ZioMetrics {
zio.metrics.Metric.histogram(s"${namespace}_request_duration_seconds", DurationBoundaries)

/** ZIO Unsafe Run Wrapper */
private def unsafeRun[T](task: Task[T]): T = Unsafe.unsafe { implicit u =>
runtime.unsafe.run(task.orDie).getOrThrowFiberFailure()
private def unsafeRun[T](effect: UIO[T]): T = Unsafe.unsafe { implicit u =>
runtime.unsafe.run(effect).getOrThrowFiberFailure()
}

/** Convert into zio metric labels */
Expand All @@ -91,30 +92,30 @@ object ZioMetrics {
}.toSet

/** Requests active metric collector. */
def requestActive[F[_]](namespace: String, labels: MetricLabels): Metric[F, Counter[Long]] = {
Metric[F, Counter[Long]](
getActiveRequestCounter(namespace),
onRequest = (req, counter, m) => {
def requestActive[F[_]](namespace: String, labels: MetricLabels): Metric[F, Gauge[Long]] = {
Metric[F, Gauge[Long]](
getActiveRequestGauge(namespace),
onRequest = (req, gauge, m) => {
m.unit {
EndpointMetric()
.onEndpointRequest { ep =>
m.eval {
unsafeRun(
counter.tagged(asZioLabel(labels, ep, req)).update(1).unit
gauge.tagged(asZioLabel(labels, ep, req)).increment
)
}
}
.onResponseBody { (ep, _) =>
m.eval {
unsafeRun(
counter.tagged(asZioLabel(labels, ep, req)).update(-1).unit
gauge.tagged(asZioLabel(labels, ep, req)).decrement
)
}
}
.onException { (ep, _) =>
m.eval {
unsafeRun(
counter.tagged(asZioLabel(labels, ep, req)).update(-1).unit
gauge.tagged(asZioLabel(labels, ep, req)).decrement
)
}
}
Expand All @@ -133,14 +134,14 @@ object ZioMetrics {
.onResponseBody { (ep, res) =>
m.eval {
unsafeRun(
counter.tagged(asZioLabel(labels, ep, req) ++ asZioLabel(labels, Right(res), None)).update(1).unit
counter.tagged(asZioLabel(labels, ep, req) ++ asZioLabel(labels, Right(res), None)).increment
)
}
}
.onException { (ep, ex) =>
m.eval {
unsafeRun(
counter.tagged(asZioLabel(labels, ep, req) ++ asZioLabel(labels, Left(ex), None)).update(1).unit
counter.tagged(asZioLabel(labels, ep, req) ++ asZioLabel(labels, Left(ex), None)).increment
)
}
}
Expand Down Expand Up @@ -168,7 +169,6 @@ object ZioMetrics {
histogram
.tagged(asZioLabel(labels, ep, req) ++ asZioLabel(labels, Right(res), Some(labels.forResponsePhase.headersValue)))
.update(duration)
.unit
)
}
}
Expand All @@ -178,14 +178,13 @@ object ZioMetrics {
histogram
.tagged(asZioLabel(labels, ep, req) ++ asZioLabel(labels, Right(res), Some(labels.forResponsePhase.bodyValue)))
.update(duration)
.unit
)
}
}
.onException { (ep: AnyEndpoint, ex: Throwable) =>
m.eval {
unsafeRun(
histogram.tagged(asZioLabel(labels, ep, req) ++ asZioLabel(labels, Left(ex), None)).update(duration).unit
histogram.tagged(asZioLabel(labels, ep, req) ++ asZioLabel(labels, Left(ex), None)).update(duration)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import sttp.tapir.server.TestUtil._
import sttp.tapir.server.interceptor.decodefailure.{DecodeFailureInterceptor, DefaultDecodeFailureHandler}
import sttp.tapir.server.interpreter.ServerInterpreter
import sttp.tapir.server.metrics.zio.ZioMetrics.DefaultNamespace
import zio.metrics.Metric.Counter
import zio._
import zio.metrics.Metric.{Counter, Gauge}
import zio.metrics._
import zio.test._
import zio._

object ZioMetricsTest extends ZIOSpecDefault {

Expand All @@ -32,8 +32,8 @@ object ZioMetricsTest extends ZIOSpecDefault {
)

// when
val active: Counter[Long] = ZioMetrics
.getActiveRequestCounter("tapir")
val active: Gauge[Long] = ZioMetrics
.getActiveRequestGauge("tapir")
.tagged(
Set(MetricLabel("path", "/person"), MetricLabel("method", "GET"))
)
Expand All @@ -48,7 +48,7 @@ object ZioMetricsTest extends ZIOSpecDefault {
state <- active.value
_ <- ZIO.succeed(Thread.sleep(150))
state2 <- active.value
} yield assertTrue(state == MetricState.Counter(1)) && assertTrue(state2 == MetricState.Counter(0))
} yield assertTrue(state == MetricState.Gauge(1)) && assertTrue(state2 == MetricState.Gauge(0))

} @@ TestAspect.retry(Schedule.recurs(5)),
test("can collect requests total") {
Expand Down