From 53217faa5aee88cdd792dabd7edd78be3506c44b Mon Sep 17 00:00:00 2001 From: alexcardell <29524087+alexcardell@users.noreply.github.com> Date: Sat, 5 Oct 2024 12:38:07 +0100 Subject: [PATCH] Replace TraceHooks with TracedProvider --- build.sbt | 3 +- .../openfeature/otel4s/TraceHook.scala | 91 ------------- .../openfeature/otel4s/TracedProvider.scala | 128 ++++++++++++++++++ ...ookTest.scala => TracedProviderTest.scala} | 58 +------- project/Versions.scala | 2 +- 5 files changed, 134 insertions(+), 148 deletions(-) delete mode 100644 openfeature/sdk-otel4s/src/main/scala/io/cardell/openfeature/otel4s/TraceHook.scala create mode 100644 openfeature/sdk-otel4s/src/main/scala/io/cardell/openfeature/otel4s/TracedProvider.scala rename openfeature/sdk-otel4s/src/test/scala/io/cardell/openfeature/otel4s/{TraceHookTest.scala => TracedProviderTest.scala} (55%) diff --git a/build.sbt b/build.sbt index 1877ba7..c7773ea 100644 --- a/build.sbt +++ b/build.sbt @@ -2,8 +2,7 @@ import build.V Global / onChangedBuildSource := ReloadOnSourceChanges -// https://typelevel.org/sbt-typelevel/faq.html#what-is-a-base-version-anyway -ThisBuild / tlBaseVersion := "0.4" // your current series x.y +ThisBuild / tlBaseVersion := "0.5" ThisBuild / organization := "io.cardell" ThisBuild / organizationName := "Alex Cardell" diff --git a/openfeature/sdk-otel4s/src/main/scala/io/cardell/openfeature/otel4s/TraceHook.scala b/openfeature/sdk-otel4s/src/main/scala/io/cardell/openfeature/otel4s/TraceHook.scala deleted file mode 100644 index 6c0e90a..0000000 --- a/openfeature/sdk-otel4s/src/main/scala/io/cardell/openfeature/otel4s/TraceHook.scala +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2023 Alex Cardell - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.cardell.openfeature.otel4s - -import cats.data.OptionT -import cats.effect.IO -import cats.effect.IOLocal -import cats.syntax.all._ -import org.typelevel.otel4s.Attributes -import org.typelevel.otel4s.trace.Span -import org.typelevel.otel4s.trace.StatusCode -import org.typelevel.otel4s.trace.Tracer - -import io.cardell.openfeature.AfterHook -import io.cardell.openfeature.BeforeHook -import io.cardell.openfeature.ErrorHook -import io.cardell.openfeature.FinallyHook -import io.cardell.openfeature.Hook - -object TraceHooks { - - def ioLocal(implicit T: Tracer[IO]): IO[List[Hook[IO]]] = IOLocal( - Option.empty[Span[IO]] - ).map(fromIOLocal) - - private def fromIOLocal( - local: IOLocal[Option[Span[IO]]] - )(implicit T: Tracer[IO]): List[Hook[IO]] = { - import FeatureFlagAttributes._ - - val before = BeforeHook[IO] { case (context, _) => - val attributes = Attributes( - FeatureFlagKey(context.flagKey) - ) - - Tracer[IO] - .span("resolve-flag", attributes) - .startUnmanaged - .flatMap(s => local.update(_ => s.some)) - .as(None) - } - - val after = AfterHook[IO] { case _ => - OptionT(local.get) - .semiflatMap { span => - for { - _ <- span.setStatus(StatusCode.Ok) - _ <- span.end - } yield () - } - .value - .void - } - - val error = ErrorHook[IO] { case (_, _, error) => - OptionT(local.get) - .semiflatMap { span => - for { - _ <- span.setStatus(StatusCode.Error) - _ <- span.recordException(error) - } yield () - } - .value - .void - } - - val finally_ = FinallyHook[IO] { case _ => - OptionT(local.get) - .semiflatMap(_.end) - .value - .void - } - - List(before, after, error, finally_) - } - -} diff --git a/openfeature/sdk-otel4s/src/main/scala/io/cardell/openfeature/otel4s/TracedProvider.scala b/openfeature/sdk-otel4s/src/main/scala/io/cardell/openfeature/otel4s/TracedProvider.scala new file mode 100644 index 0000000..845bdb7 --- /dev/null +++ b/openfeature/sdk-otel4s/src/main/scala/io/cardell/openfeature/otel4s/TracedProvider.scala @@ -0,0 +1,128 @@ +/* + * Copyright 2023 Alex Cardell + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.cardell.openfeature.otel4s + +import cats.Applicative +import cats.Monad +import cats.syntax.all._ +import org.typelevel.otel4s.Attribute +import org.typelevel.otel4s.AttributeKey +import org.typelevel.otel4s.Attributes +import org.typelevel.otel4s.trace.StatusCode +import org.typelevel.otel4s.trace.Tracer + +import io.cardell.openfeature.EvaluationContext +import io.cardell.openfeature.StructureCodec +import io.cardell.openfeature.otel4s.FeatureFlagAttributes.FeatureFlagKey +import io.cardell.openfeature.otel4s.FeatureFlagAttributes.FeatureFlagProviderName +import io.cardell.openfeature.otel4s.FeatureFlagAttributes.FeatureFlagVariant +import io.cardell.openfeature.provider.EvaluationProvider +import io.cardell.openfeature.provider.Provider +import io.cardell.openfeature.provider.ProviderMetadata +import io.cardell.openfeature.provider.ResolutionDetails +import cats.MonadThrow + +class TracedProvider[F[_]: Tracer: MonadThrow]( + provider: Provider[F] +) extends EvaluationProvider[F] { + + override def metadata: ProviderMetadata = provider.metadata + + override def resolveBooleanValue( + flagKey: String, + defaultValue: Boolean, + context: EvaluationContext + ): F[ResolutionDetails[Boolean]] = + trace("boolean", flagKey)( + provider.resolveBooleanValue( + flagKey, + defaultValue, + context + ) + ) + + override def resolveStringValue( + flagKey: String, + defaultValue: String, + context: EvaluationContext + ): F[ResolutionDetails[String]] = + trace("string", flagKey)( + provider.resolveStringValue( + flagKey, + defaultValue, + context + ) + ) + + override def resolveIntValue( + flagKey: String, + defaultValue: Int, + context: EvaluationContext + ): F[ResolutionDetails[Int]] = + trace("int", flagKey)( + provider.resolveIntValue( + flagKey, + defaultValue, + context + ) + ) + + override def resolveDoubleValue( + flagKey: String, + defaultValue: Double, + context: EvaluationContext + ): F[ResolutionDetails[Double]] = + trace("double", flagKey)( + provider.resolveDoubleValue( + flagKey, + defaultValue, + context + ) + ) + + override def resolveStructureValue[A: StructureCodec]( + flagKey: String, + defaultValue: A, + context: EvaluationContext + ): F[ResolutionDetails[A]] = + trace("structure", flagKey)( + provider.resolveStructureValue( + flagKey, + defaultValue, + context + ) + ) + + private def flagAttributes(flagKey: String): Attributes = + Attributes(FeatureFlagKey(flagKey), FeatureFlagProviderName(metadata.name)) + + private def variantAttributes(maybeVariant: Option[String]): Attributes = + Attributes.empty.concat(FeatureFlagVariant.maybe(maybeVariant)) + + private def trace[A](flagType: String, flagKey: String)( + fa: F[ResolutionDetails[A]] + ): F[ResolutionDetails[A]] = + Tracer[F].span(s"evaluate-${flagType}-flag").use { span => + for { + _ <- span.addAttributes(flagAttributes(flagKey)) + res <- fa.onError(span.recordException(_)) + _ <- span.addAttributes(variantAttributes(res.variant)) + _ <- span.setStatus(StatusCode.Ok) + } yield res + } + +} diff --git a/openfeature/sdk-otel4s/src/test/scala/io/cardell/openfeature/otel4s/TraceHookTest.scala b/openfeature/sdk-otel4s/src/test/scala/io/cardell/openfeature/otel4s/TracedProviderTest.scala similarity index 55% rename from openfeature/sdk-otel4s/src/test/scala/io/cardell/openfeature/otel4s/TraceHookTest.scala rename to openfeature/sdk-otel4s/src/test/scala/io/cardell/openfeature/otel4s/TracedProviderTest.scala index 99e38a2..a40944e 100644 --- a/openfeature/sdk-otel4s/src/test/scala/io/cardell/openfeature/otel4s/TraceHookTest.scala +++ b/openfeature/sdk-otel4s/src/test/scala/io/cardell/openfeature/otel4s/TracedProviderTest.scala @@ -27,7 +27,7 @@ import io.cardell.openfeature.FlagValue import io.cardell.openfeature.provider.ProviderImpl import io.cardell.openfeature.provider.memory.MemoryProvider -class TraceHookTest extends CatsEffectSuite { +class TracedProviderTest extends CatsEffectSuite { val setupProvider = MemoryProvider[IO]( Map("boolean-flag" -> FlagValue.BooleanValue(true)) @@ -37,7 +37,7 @@ class TraceHookTest extends CatsEffectSuite { test("span is applied") { val expectedFlagResult = true - val expectedSpanName = "resolve-flag" + val expectedSpanName = "evaluate-boolean-flag" val expectedSpanCount = 1 val expectedSpanStatus = StatusCode.Ok @@ -46,10 +46,9 @@ class TraceHookTest extends CatsEffectSuite { setupTracer.flatMap { implicit tracer => for { - hooks <- TraceHooks.ioLocal provider <- setupProvider.map(ProviderImpl[IO]) - hookedProvider = hooks.foldLeft(provider)(_ withHook _) - flagResolution <- hookedProvider.resolveBooleanValue( + tracedProvider = new TracedProvider[IO](provider) + flagResolution <- tracedProvider.resolveBooleanValue( "boolean-flag", false, EvaluationContext.empty @@ -79,53 +78,4 @@ class TraceHookTest extends CatsEffectSuite { } - test("span is recorded as exception when exception thrown in after hook") { - val expectedSpanName = "resolve-flag" - val expectedSpanCount = 1 - val expectedSpanStatus = StatusCode.Error - - val throwingHook = AfterHook[IO] { case _ => - IO.raiseError(new Throwable("throwing hook")) - } - - setupTestkit.use { kit => - val setupTracer = kit.tracerProvider.tracer("name").get - - setupTracer.flatMap { implicit tracer => - for { - traceHooks <- TraceHooks.ioLocal - (before, others) = (traceHooks.head, traceHooks.tail) - hooks = List(before, throwingHook) ++ others - provider <- setupProvider.map(ProviderImpl[IO]) - hookedProvider = hooks.foldLeft(provider)(_ withHook _) - _ <- - hookedProvider - .resolveBooleanValue( - "boolean-flag", - false, - EvaluationContext.empty - ) - .attempt - spans <- kit.finishedSpans - spanCount = spans.size - headSpan = spans.headOption - spanName = headSpan.map(_.name) - spanStatus = headSpan.map(_.status.status) - spanEnded = headSpan.map(_.hasEnded) - spanAttrs = headSpan.map(_.attributes.elements) - flagKeyAttrExists = spanAttrs.map( - _.exists(_ == FeatureFlagAttributes.FeatureFlagKey("boolean-flag")) - ) - } yield { - assertEquals(spanCount, expectedSpanCount) - assertEquals(spanName, Some(expectedSpanName)) - assertEquals(spanStatus, Some(expectedSpanStatus)) - assertEquals(spanEnded, Some(true)) - assertEquals(flagKeyAttrExists, Some(true)) - } - } - } - - } - } diff --git a/project/Versions.scala b/project/Versions.scala index f728f7a..4c1498b 100644 --- a/project/Versions.scala +++ b/project/Versions.scala @@ -5,7 +5,7 @@ object V { val catsEffect = "3.5.4" val http4s = "0.23.26" val circe = "0.14.7" - val otel4s = "0.9.0" + val otel4s = "0.10.0" val munit = "1.0.0-RC1" val munitCatsEffect = "2.0.0-M5"