diff --git a/metrics/zio-metrics/src/main/scala/sttp/tapir/server/metrics/zio/ZioMetrics.scala b/metrics/zio-metrics/src/main/scala/sttp/tapir/server/metrics/zio/ZioMetrics.scala index 9515ed50d1..ff96d2c4ae 100644 --- a/metrics/zio-metrics/src/main/scala/sttp/tapir/server/metrics/zio/ZioMetrics.scala +++ b/metrics/zio-metrics/src/main/scala/sttp/tapir/server/metrics/zio/ZioMetrics.scala @@ -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") @@ -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 */ @@ -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 ) } } @@ -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 ) } } @@ -168,7 +169,6 @@ object ZioMetrics { histogram .tagged(asZioLabel(labels, ep, req) ++ asZioLabel(labels, Right(res), Some(labels.forResponsePhase.headersValue))) .update(duration) - .unit ) } } @@ -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) ) } } diff --git a/metrics/zio-metrics/src/test/scala/sttp/tapir/server/metrics/zio/ZioMetricsTest.scala b/metrics/zio-metrics/src/test/scala/sttp/tapir/server/metrics/zio/ZioMetricsTest.scala index 00cf1b4896..ef89e94168 100644 --- a/metrics/zio-metrics/src/test/scala/sttp/tapir/server/metrics/zio/ZioMetricsTest.scala +++ b/metrics/zio-metrics/src/test/scala/sttp/tapir/server/metrics/zio/ZioMetricsTest.scala @@ -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 { @@ -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")) ) @@ -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") {