Skip to content

Instantly share code, notes, and snippets.

@kory33
Created September 6, 2024 09:45
Show Gist options
  • Select an option

  • Save kory33/14cfbd2f738c649afdc96eefd06b2afd to your computer and use it in GitHub Desktop.

Select an option

Save kory33/14cfbd2f738c649afdc96eefd06b2afd to your computer and use it in GitHub Desktop.
Scala わいわい勉強会 #3 でのサンプルコード
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