Created
September 6, 2024 09:45
-
-
Save kory33/14cfbd2f738c649afdc96eefd06b2afd to your computer and use it in GitHub Desktop.
Scala わいわい勉強会 #3 でのサンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package io.github.kory33.tracing_instrument.example | |
| import io.github.kory33.tracing_instrument.otel4s.methodSpan | |
| import io.github.kory33.tracing_instrument.experimental.otel4s.instrumentF | |
| import scala.annotation.experimental | |
| object example { | |
| import cats.Monad | |
| import cats.effect.Ref | |
| import cats.syntax.flatMap._ | |
| import cats.syntax.functor._ | |
| import org.typelevel.otel4s.Attribute | |
| import org.typelevel.otel4s.trace.Tracer | |
| case class User(email: String) | |
| object beforeMethodSpanExpansion { | |
| class UserRepository[F[_]: Monad: Tracer]( | |
| storage: Ref[F, Map[Long, User]] | |
| ) { | |
| def findUser(userId: Long): F[Option[User]] = | |
| methodSpan[F, Option[User]] { _ => | |
| for { | |
| current <- storage.get | |
| user <- Monad[F].pure(current.get(userId)) | |
| } yield user | |
| } | |
| } | |
| } | |
| object afterMethodSpanExpansion { | |
| class UserRepository[F[_]: Monad: Tracer]( | |
| storage: Ref[F, Map[Long, User]] | |
| ) { | |
| def findUser(userId: Long): F[Option[User]] = Tracer[F] | |
| .span(name = "findUser", attributes = List(Attribute("userId", userId))) | |
| .use { _ => | |
| for { | |
| current <- storage.get | |
| user <- Monad[F].pure(current.get(userId)) | |
| } yield user | |
| } | |
| } | |
| } | |
| @experimental object beforeInstrumentFExpansion { | |
| class UserRepository[F[_]: Monad: Tracer]( | |
| storage: Ref[F, Map[Long, User]] | |
| ) { | |
| @instrumentF | |
| def findUser(userId: Long): F[Option[User]] = | |
| for { | |
| current <- storage.get | |
| user <- Monad[F].pure(current.get(userId)) | |
| } yield user | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment