From 9685eb3aeb2af2dcbbadf5608688403d212c025b Mon Sep 17 00:00:00 2001 From: Maksym Ochenashko Date: Wed, 30 Oct 2024 10:17:02 +0200 Subject: [PATCH] sdk: replace `whenA` with `if`, `foldMapA` with `foldMapM` --- .../metrics/exemplar/ExemplarReservoir.scala | 10 +++++----- .../exporter/ConsoleMetricExporter.scala | 3 +-- .../internal/CallbackRegistration.scala | 17 ++++++++-------- .../metrics/internal/MeterSharedState.scala | 2 +- .../internal/SdkObservableMeasurement.scala | 14 ++++++------- .../otel4s/sdk/trace/SdkSpanBackend.scala | 20 +++++++++++-------- .../trace/processor/SimpleSpanProcessor.scala | 3 +-- 7 files changed, 34 insertions(+), 35 deletions(-) diff --git a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/exemplar/ExemplarReservoir.scala b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/exemplar/ExemplarReservoir.scala index 7ca46fad3..5d7d341d4 100644 --- a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/exemplar/ExemplarReservoir.scala +++ b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/exemplar/ExemplarReservoir.scala @@ -114,11 +114,11 @@ private[metrics] object ExemplarReservoir { ): ExemplarReservoir[F, A] = new ExemplarReservoir[F, A] { def offer(value: A, attributes: Attributes, context: Context): F[Unit] = - original - .offer(value, attributes, context) - .whenA( - filter.shouldSample(value, attributes, context) - ) + if (filter.shouldSample(value, attributes, context)) { + original.offer(value, attributes, context) + } else { + Applicative[F].unit + } def collectAndReset(attributes: Attributes): F[Vector[Exemplar[A]]] = original.collectAndReset(attributes) diff --git a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/exporter/ConsoleMetricExporter.scala b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/exporter/ConsoleMetricExporter.scala index e89d20e07..89cea7ac1 100644 --- a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/exporter/ConsoleMetricExporter.scala +++ b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/exporter/ConsoleMetricExporter.scala @@ -16,7 +16,6 @@ package org.typelevel.otel4s.sdk.metrics.exporter -import cats.Applicative import cats.Foldable import cats.Monad import cats.effect.std.Console @@ -60,7 +59,7 @@ private final class ConsoleMetricExporter[F[_]: Monad: Console]( doExport.whenA(metrics.nonEmpty) } - def flush: F[Unit] = Applicative[F].unit + def flush: F[Unit] = Monad[F].unit } object ConsoleMetricExporter { diff --git a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/CallbackRegistration.scala b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/CallbackRegistration.scala index f11cac4d8..499340e09 100644 --- a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/CallbackRegistration.scala +++ b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/CallbackRegistration.scala @@ -18,7 +18,6 @@ package org.typelevel.otel4s.sdk.metrics.internal import cats.data.NonEmptyList import cats.effect.MonadCancelThrow -import cats.syntax.applicative._ import cats.syntax.foldable._ import org.typelevel.otel4s.sdk.metrics.data.TimeWindow import org.typelevel.otel4s.sdk.metrics.internal.exporter.RegisteredReader @@ -39,14 +38,14 @@ private[metrics] final class CallbackRegistration[F[_]: MonadCancelThrow]( * @param timeWindow * the time window of the measurement */ - def invokeCallback( - reader: RegisteredReader[F], - timeWindow: TimeWindow - ): F[Unit] = - measurements - .traverse_(_.withActiveReader(reader, timeWindow)) - .surround(callback) - .whenA(hasStorages) + def invokeCallback(reader: RegisteredReader[F], timeWindow: TimeWindow): F[Unit] = + if (hasStorages) { + measurements + .traverse_(_.withActiveReader(reader, timeWindow)) + .surround(callback) + } else { + MonadCancelThrow[F].unit + } override def toString: String = s"CallbackRegistration{instrumentDescriptors=${measurements.map(_.descriptor).mkString_("[", ", ", "]")}" diff --git a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/MeterSharedState.scala b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/MeterSharedState.scala index 8a837289a..323c7f059 100644 --- a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/MeterSharedState.scala +++ b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/MeterSharedState.scala @@ -216,7 +216,7 @@ private[metrics] final class MeterSharedState[ for { _ <- currentCallbacks.traverse_(_.invokeCallback(reader, timeWindow)) - storages <- registries.get(reader).foldMapA(_.storages) + storages <- registries.get(reader).foldMapM(_.storages) result <- storages.traverse { storage => storage.collect(resource, scope, timeWindow) } diff --git a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/SdkObservableMeasurement.scala b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/SdkObservableMeasurement.scala index d7f765ab5..1f008631b 100644 --- a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/SdkObservableMeasurement.scala +++ b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/internal/SdkObservableMeasurement.scala @@ -21,7 +21,6 @@ import cats.effect.Concurrent import cats.effect.Ref import cats.effect.Resource import cats.effect.std.Console -import cats.syntax.applicative._ import cats.syntax.flatMap._ import cats.syntax.foldable._ import cats.syntax.functor._ @@ -117,17 +116,14 @@ private[metrics] object SdkObservableMeasurement { v => !cast(v).isNaN } - def withActiveReader( - reader: RegisteredReader[F], - timeWindow: TimeWindow - ): Resource[F, Unit] = + def withActiveReader(reader: RegisteredReader[F], timeWindow: TimeWindow): Resource[F, Unit] = Resource.make(stateRef.set(State.WithReader(reader, timeWindow))) { _ => stateRef.set(State.Empty()) } def record(value: A, attributes: Attributes): F[Unit] = - stateRef.get - .flatMap { + if (isValid(value)) { + stateRef.get.flatMap { case State.Empty() => Console[F].errorln( "SdkObservableMeasurement: " + @@ -143,7 +139,9 @@ private[metrics] object SdkObservableMeasurement { .filter(_.reader == reader) .traverse_(storage => storage.record(measurement)) } - .whenA(isValid(value)) + } else { + Monad[F].unit + } def hasStorages: Boolean = storages.nonEmpty } diff --git a/sdk/trace/src/main/scala/org/typelevel/otel4s/sdk/trace/SdkSpanBackend.scala b/sdk/trace/src/main/scala/org/typelevel/otel4s/sdk/trace/SdkSpanBackend.scala index 6f7b15823..e12d42b93 100644 --- a/sdk/trace/src/main/scala/org/typelevel/otel4s/sdk/trace/SdkSpanBackend.scala +++ b/sdk/trace/src/main/scala/org/typelevel/otel4s/sdk/trace/SdkSpanBackend.scala @@ -84,9 +84,13 @@ private final class SdkSpanBackend[F[_]: Monad: Clock: Console] private ( updateState("updateName")(_.copy(name = name)).void def addAttributes(attributes: immutable.Iterable[Attribute[_]]): F[Unit] = - updateState("addAttributes") { s => - s.copy(attributes = s.attributes.appendAll(attributes.to(Attributes))) - }.unlessA(attributes.isEmpty) + if (attributes.nonEmpty) { + updateState("addAttributes") { s => + s.copy(attributes = s.attributes.appendAll(attributes.to(Attributes))) + }.void + } else { + Monad[F].unit + } def addEvent( name: String, @@ -193,11 +197,11 @@ private final class SdkSpanBackend[F[_]: Monad: Clock: Console] private ( else (update(state), true) } .flatTap { modified => - Console[F] - .println( - s"SdkSpanBackend: calling [$method] on the ended span $context" - ) - .unlessA(modified) + if (modified) { + Monad[F].unit + } else { + Console[F].println(s"SdkSpanBackend: calling [$method] on the ended span $context") + } } // SpanRef interfaces diff --git a/sdk/trace/src/main/scala/org/typelevel/otel4s/sdk/trace/processor/SimpleSpanProcessor.scala b/sdk/trace/src/main/scala/org/typelevel/otel4s/sdk/trace/processor/SimpleSpanProcessor.scala index e6d998c31..65ca5c653 100644 --- a/sdk/trace/src/main/scala/org/typelevel/otel4s/sdk/trace/processor/SimpleSpanProcessor.scala +++ b/sdk/trace/src/main/scala/org/typelevel/otel4s/sdk/trace/processor/SimpleSpanProcessor.scala @@ -19,7 +19,6 @@ package processor import cats.MonadThrow import cats.effect.std.Console -import cats.syntax.applicative._ import cats.syntax.applicativeError._ import org.typelevel.otel4s.sdk.trace.data.SpanData import org.typelevel.otel4s.sdk.trace.exporter.SpanExporter @@ -53,7 +52,7 @@ private final class SimpleSpanProcessor[F[_]: MonadThrow: Console] private ( def onEnd(span: SpanData): F[Unit] = { val canExport = !exportOnlySampled || span.spanContext.isSampled - doExport(span).whenA(canExport) + if (canExport) doExport(span) else MonadThrow[F].unit } private def doExport(span: SpanData): F[Unit] =