Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement lazy MDC #246

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions core/src/main/scala/com/evolutiongaming/catshelper/Log.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,66 @@ object Log {
object Mdc {

private object Empty extends Mdc
private final case class Context(values: NonEmptyMap[String, String]) extends Mdc {
override def toString: String = s"MDC(${values.toSortedMap.mkString(", ")})"
}
private final case class Context(getValues: () => Map[String, String]) extends Mdc {
dfakhritdinov marked this conversation as resolved.
Show resolved Hide resolved
lazy val values: SortedMap[String, String] = SortedMap(getValues().toSeq: _*)

val empty: Mdc = Empty
override def toString: String = s"MDC(${values.mkString(", ")})"

def apply(head: (String, String), tail: (String, String)*): Mdc = Context(NonEmptyMap.of(head, tail: _*))
override def hashCode(): Int = values.hashCode()

def fromSeq(seq: Seq[(String, String)]): Mdc =
NonEmptyMap.fromMap(SortedMap(seq: _*)).fold(empty){ nem => Context(nem) }
override def equals(obj: Any): Boolean = obj match {
case that: Context => this.values.equals(that.values)
case _ => false
}
}

def fromMap(map: Map[String, String]): Mdc = fromSeq(map.toSeq)
val empty: Mdc = Empty

type Record = (String, String)

@deprecated("Use Mdc.Lazy.apply. If it's not enough - Mdc.Lazy.fromMap", "3.9.0")
def apply(head: Record, tail: Record*): Mdc = Lazy.fromMap( (head +: tail).toMap)
dfakhritdinov marked this conversation as resolved.
Show resolved Hide resolved

@deprecated("Use Mdc.Lazy.fromSeq", "3.9.0")
def fromSeq(seq: Seq[Record]): Mdc = Lazy.fromSeq(seq)

@deprecated("Use Mdc.Lazy.fromMap", "3.9.0")
def fromMap(map: Map[String, String]): Mdc = Lazy.fromMap(map)

object Lazy {
def apply(v1: => Record): Mdc = fromMap(Map(v1))
def apply(v1: => Record, v2: => Record): Mdc = fromMap(Map(v1, v2))
def apply(v1: => Record, v2: => Record, v3: => Record): Mdc = fromMap(Map(v1, v2, v3))
def apply(v1: => Record, v2: => Record, v3: => Record, v4: => Record): Mdc = fromMap(Map(v1, v2, v3, v4))
def apply(v1: => Record, v2: => Record, v3: => Record, v4: => Record, v5: => Record): Mdc =
fromMap(Map(v1, v2, v3, v4, v5))
def apply(v1: => Record, v2: => Record, v3: => Record, v4: => Record, v5: => Record, v6: => Record): Mdc =
fromMap(Map(v1, v2, v3, v4, v5, v6))
def apply(v1: => Record, v2: => Record, v3: => Record, v4: => Record, v5: => Record, v6: => Record, v7: => Record): Mdc =
fromMap(Map(v1, v2, v3, v4, v5, v6, v7))
def apply(v1: => Record, v2: => Record, v3: => Record, v4: => Record, v5: => Record, v6: => Record, v7: => Record, v8: => Record): Mdc =
fromMap(Map(v1, v2, v3, v4, v5, v6, v7, v8))
def apply(v1: => Record, v2: => Record, v3: => Record, v4: => Record, v5: => Record, v6: => Record, v7: => Record, v8: => Record, v9: => Record): Mdc =
fromMap(Map(v1, v2, v3, v4, v5, v6, v7, v8, v9))
def apply(v1: => Record, v2: => Record, v3: => Record, v4: => Record, v5: => Record, v6: => Record, v7: => Record, v8: => Record, v9: => Record, v10: => Record): Mdc =
fromMap(Map(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10))

def fromSeq(seq: => Seq[Record]): Mdc = fromMap(seq.toMap)

def fromMap(map: => Map[String, String]): Mdc = Context(() => map)
}

implicit final val mdcSemigroup: Semigroup[Mdc] = Semigroup.instance {
case (Empty, right) => right
case (left, Empty) => left
case (Context(v1), Context(v2)) => Context(v1 ++ v2)
case (Context(v1), Context(v2)) => Context(() => v1() ++ v2())
}

implicit final class MdcOps(val mdc: Mdc) extends AnyVal {

def context: Option[NonEmptyMap[String, String]] = mdc match {
case Empty => None
case Context(values) => Some(values)
case c: Context => NonEmptyMap.fromMap(c.values)
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions core/src/test/scala/com/evolutiongaming/catshelper/LogSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,25 @@ class LogSpec extends AnyFunSuite with Matchers {
val stateT = for {
log0 <- logOf("source")
log = log0.prefixed(">").mapK(FunctionK.id)
_ <- log.trace("trace", Log.Mdc(mdc))
_ <- log.debug("debug", Log.Mdc(mdc))
_ <- log.info("info", Log.Mdc(mdc))
_ <- log.warn("warn", Log.Mdc(mdc))
_ <- log.warn("warn", Error, Log.Mdc(mdc))
_ <- log.error("error", Log.Mdc(mdc))
_ <- log.error("error", Error, Log.Mdc(mdc))
_ <- log.trace("trace", Log.Mdc.Lazy(mdc))
_ <- log.debug("debug", Log.Mdc.Lazy(mdc))
_ <- log.info("info", Log.Mdc.Lazy(mdc))
_ <- log.warn("warn", Log.Mdc.Lazy(mdc))
_ <- log.warn("warn", Error, Log.Mdc.Lazy(mdc))
_ <- log.error("error", Log.Mdc.Lazy(mdc))
_ <- log.error("error", Error, Log.Mdc.Lazy(mdc))
} yield {}


val (state, _) = stateT.run(State(Nil))
state shouldEqual State(List(
Action.Error1("> error", Error, Log.Mdc(mdc)),
Action.Error0("> error", Log.Mdc(mdc)),
Action.Warn1("> warn", Error, Log.Mdc(mdc)),
Action.Warn0("> warn", Log.Mdc(mdc)),
Action.Info("> info", Log.Mdc(mdc)),
Action.Debug("> debug", Log.Mdc(mdc)),
Action.Trace("> trace", Log.Mdc(mdc)),
Action.Error1("> error", Error, Log.Mdc.Lazy(mdc)),
Action.Error0("> error", Log.Mdc.Lazy(mdc)),
Action.Warn1("> warn", Error, Log.Mdc.Lazy(mdc)),
Action.Warn0("> warn", Log.Mdc.Lazy(mdc)),
Action.Info("> info", Log.Mdc.Lazy(mdc)),
Action.Debug("> debug", Log.Mdc.Lazy(mdc)),
Action.Trace("> trace", Log.Mdc.Lazy(mdc)),
Action.OfStr("source")))
}

Expand All @@ -74,7 +74,7 @@ class LogSpec extends AnyFunSuite with Matchers {
val io = for {
logOf <- LogOf.slf4j[IO]
log <- logOf(getClass)
_ <- log.info("whatever", Log.Mdc("k" -> "v"))
_ <- log.info("whatever", Log.Mdc.Lazy("k" -> "v"))
} yield org.slf4j.MDC.getCopyOfContextMap

io.unsafeRunSync() shouldEqual null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.evolutiongaming.catshelper
import cats.effect.std.CountDownLatch
import cats.effect.syntax.all.*
import cats.effect.unsafe.IORuntime
import cats.effect.{Deferred, IO, Ref, Resource}
import cats.effect.{IO, Ref, Resource}
import com.evolutiongaming.catshelper.testkit.PureTest.ioTest
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LogOfFromLogbackSpec extends AnyFunSuite with Matchers {
val io = for {
logOf <- LogOfFromLogback[IO]
log <- logOf(getClass)
_ <- log.info("hello from logback", Log.Mdc("k" -> "test value for K"))
_ <- log.info("hello from logback", Log.Mdc.Lazy("k" -> "test value for K"))
} yield ()

io.unsafeRunSync()
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ThisBuild / version := "3.8.1-SNAPSHOT"
ThisBuild / version := "3.9.0-SNAPSHOT"
Loading