8000 Clean tracing by jdegoes · Pull Request #5966 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Clean tracing #5966

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

Merged
merged 2 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
132 changes: 82 additions & 50 deletions core/shared/src/main/scala/zio/Cause.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,48 @@ sealed abstract class Cause[+E] extends Product with Serializable { self =>
}
.reverse

/**
* Finds something and extracts some details from it.
*/
final def find[Z](f: PartialFunction[Cause[E], Z]): Option[Z] = {
@tailrec
def loop(cause: Cause[E], stack: List[Cause[E]]): Option[Z] =
f.lift(cause) match {
case Some(z) => Some(z)
case None =>
cause match {
case Then(left, right) => loop(left, right :: stack)
case Both(left, right) => loop(left, right :: stack)
case Stackless(cause, _) => loop(cause, stack)
case _ =>
stack match {
case hd :: tl => loop(hd, tl)
case Nil => None
}
}
}
loop(self, Nil)
}

/**
* Folds over the cause to statefully compute a value.
*/
final def foldLeft[Z](z: Z)(f: PartialFunction[(Z, Cause[E]), Z]): Z = {
@tailrec
def loop(z: Z, cause: Cause[E], stack: List[Cause[E]]): Z =
(f.applyOrElse[(Z, Cause[E]), Z](z -> cause, _ => z), cause) match {
case (z, Then(left, right)) => loop(z, left, right :: stack)
case (z, Both(left, right)) => loop(z, left, right :: stack)
case (z, Stackless(cause, _)) => loop(z, cause, stack)
case (z, _) =>
stack match {
case hd :: tl => loop(z, hd, tl)
case Nil => z
}
}
loop(z, self, Nil)
}

/**
* Determines if the `Cause` contains an interruption.
*/
Expand Down Expand Up @@ -177,6 +219,16 @@ sealed abstract class Cause[+E] extends Product with Serializable { self =>
case Fail(_, _) => false
}.getOrElse(true)

/**
* Determines if the `Cause` is traced.
*/
final def isTraced: Boolean =
find {
case Die(_, trace) if trace != ZTrace.none => ()
case Fail(_, trace) if trace != ZTrace.none => ()
case Interrupt(_, trace) if trace != ZTrace.none => ()
}.isDefined

final def fold[Z](
empty: => Z,
failCase: (E, ZTrace) => Z,
Expand Down Expand Up @@ -281,22 +333,6 @@ sealed abstract class Cause[+E] extends Product with Serializable { self =>
case Stackless(cause, stackless) => Stackless(cause.map(f), stackless)
}

/**
* Returns a `Cause` that has been stripped of all tracing information.
*/
final def untraced: Cause[E] =
self match {
case Stackless(cause, data) => Stackless(cause.untraced, data)

case Empty => Empty
case c @ Fail(_, _) => c
case c @ Die(_, _) => c
case c @ Interrupt(_, _) => c

case Then(left, right) => Then(left.untraced, right.untraced)
case Both(left, right) => Both(left.untraced, right.untraced)
}

/**
* Returns a `String` with the cause pretty-printed.
*/
Expand Down Expand Up @@ -462,41 +498,37 @@ sealed abstract class Cause[+E] extends Product with Serializable { self =>
}
.reverse

final def find[Z](f: PartialFunction[Cause[E], Z]): Option[Z] = {
@tailrec
def loop(cause: Cause[E], stack: List[Cause[E]]): Option[Z] =
f.lift(cause) match {
case Some(z) => Some(z)
case None =>
cause match {
case Then(left, right) => loop(left, right :: stack)
case Both(left, right) => loop(left, right :: stack)
case Stackless(cause, _) => loop(cause, stack)
case _ =>
stack match {
case hd :: tl => loop(hd, tl)
case Nil => None
}
}
}
loop(self, Nil)
}
/**
* Replaces traces with the specified trace.
*/
final def traced(trace: ZTrace): Cause[E] =
self match {
case s @ Stackless(_, _) => s

final def foldLeft[Z](z: Z)(f: PartialFunction[(Z, Cause[E]), Z]): Z = {
@tailrec
def loop(z: Z, cause: Cause[E], stack: List[Cause[E]]): Z =
(f.applyOrElse[(Z, Cause[E]), Z](z -> cause, _ => z), cause) match {
case (z, Then(left, right)) => loop(z, left, right :: stack)
case (z, Both(left, right)) => loop(z, left, right :: stack)
case (z, Stackless(cause, _)) => loop(z, cause, stack)
case (z, _) =>
stack match {
case hd :: tl => loop(z, hd, tl)
case Nil => z
}
}
loop(z, self, Nil)
}
case Empty => Empty
case Fail(v, _) => Fail(v, trace)
case Die(v, _) => Die(v, trace)
case Interrupt(v, _) => Interrupt(v, trace)

case Then(left, right) => Then(left.traced(trace), right.traced(trace))
case Both(left, right) => Both(left.traced(trace), right.traced(trace))
}

/**
* Returns a `Cause` that has been stripped of all tracing information.
*/
final def untraced: Cause[E] =
self match {
case Stackless(cause, data) => Stackless(cause.untraced, data)

case Empty => Empty
case c @ Fail(_, _) => c
case c @ Die(_, _) => c
case c @ Interrupt(_, _) => c

case Then(left, right) => Then(left.untraced, right.untraced)
case Both(left, right) => Both(left.untraced, right.untraced)
}

private def attachTrace(e: Throwable): Throwable = {
val trace = Cause.FiberTrace(Cause.stackless(this).prettyPrint)
Expand Down
13 changes: 0 additions & 13 deletions core/shared/src/main/scala/zio/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,6 @@ object IO {
def failCause[E](cause: => Cause[E])(implicit trace: ZTraceElement): IO[E, Nothing] =
ZIO.failCause(cause)

/**
* @see See [[zio.ZIO.failCauseWith]]
*/
def failCauseWith[E](function: (() => ZTrace) => Cause[E])(implicit trace: ZTraceElement): IO[E, Nothing] =
ZIO.failCauseWith(function)

/**
* @see [[zio.ZIO.fiberId]]
*/
Expand Down Expand Up @@ -929,13 +923,6 @@ object IO {
def halt[E](cause: => Cause[E])(implicit trace: ZTraceElement): IO[E, Nothing] =
ZIO.halt(cause)

/**
* @see See [[zio.ZIO.haltWith]]
*/
@deprecated("use failCauseWith", "2.0.0")
def haltWith[E](function: (() => ZTrace) => Cause[E])(implicit trace: ZTraceElement): IO[E, Nothing] =
ZIO.haltWith(function)

/**
* @see [[zio.ZIO.ifM]]
*/
Expand Down
13 changes: 0 additions & 13 deletions core/shared/src/main/scala/zio/RIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,6 @@ object RIO {
def failCause(cause: => Cause[Throwable])(implicit trace: ZTraceElement): Task[Nothing] =
ZIO.failCause(cause)

/**
* @see See [[zio.ZIO.failCauseWith]]
*/
def failCauseWith[R](function: (() => ZTrace) => Cause[Throwable])(implicit trace: ZTraceElement): RIO[R, Nothing] =
ZIO.failCauseWith(function)

/**
* @see [[zio.ZIO.fiberId]]
*/
Expand Down Expand Up @@ -961,13 +955,6 @@ object RIO {
def halt(cause: => Cause[Throwable])(implicit trace: ZTraceElement): Task[Nothing] =
ZIO.halt(cause)

/**
* @see See [[zio.ZIO.haltWith]]
*/
@deprecated("use failCauseWith", "2.0.0")
def haltWith[R](function: (() => ZTrace) => Cause[Throwable])(implicit trace: ZTraceElement): RIO[R, Nothing] =
ZIO.haltWith(function)

/**
* @see [[zio.ZIO.ifM]]
*/
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/Runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ trait Runtime[+R] {
case ZIO.Tags.Fail =>
val zio = curZio.asInstanceOf[ZIO.Fail[E]]

done = Exit.failCause(zio.fill(() => null))
done = Exit.failCause(zio.cause())

case ZIO.Tags.Succeed =>
val zio = curZio.asInstanceOf[ZIO.Succeed[A]]
Expand Down
15 changes: 0 additions & 15 deletions core/shared/src/main/scala/zio/Task.scala
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,6 @@ object Task extends TaskPlatformSpecific {
def failCause(cause: => Cause[Throwable])(implicit trace: ZTraceElement): Task[Nothing] =
ZIO.failCause(cause)

/**
* @see See [[zio.ZIO.failCauseWith]]
*/
def failCauseWith[E <: Throwable](function: (() => ZTrace) => Cause[E])(implicit
trace: ZTraceElement
): Task[Nothing] =
ZIO.failCauseWith(function)

/**
* @see [[zio.ZIO.fiberId]]
*/
Expand Down Expand Up @@ -908,13 +900,6 @@ object Task extends TaskPlatformSpecific {
def halt(cause: => Cause[Throwable])(implicit trace: ZTraceElement): Task[Nothing] =
ZIO.halt(cause)

/**
* @see See [[zio.ZIO.haltWith]]
*/
@deprecated("use failCauseWith", "2.0.0")
def haltWith[E <: Throwable](function: (() => ZTrace) => Cause[E])(implicit trace: ZTraceElement): Task[Nothing] =
ZIO.haltWith(function)

/**
* @see [[zio.ZIO.ifM]]
*/
Expand Down
13 changes: 0 additions & 13 deletions core/shared/src/main/scala/zio/UIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,6 @@ object UIO {
def failCause(cause: => Cause[Nothing])(implicit trace: ZTraceElement): UIO[Nothing] =
ZIO.failCause(cause)

/**
* @see [[zio.ZIO.failCauseWith]]
*/
def failCauseWith(function: (() => ZTrace) => Cause[Nothing])(implicit trace: ZTraceElement): UIO[Nothing] =
ZIO.failCauseWith(function)

/**
* @see [[zio.ZIO.fiberId]]
*/
Expand Down Expand Up @@ -790,13 +784,6 @@ object UIO {
def halt(cause: => Cause[Nothing])(implicit trace: ZTraceElement): UIO[Nothing] =
ZIO.halt(cause)

/**
* @see [[zio.ZIO.haltWith]]
*/
@deprecated("use failCauseWith", "2.0.0")
def haltWith(function: (() => ZTrace) => Cause[Nothing])(implicit trace: ZTraceElement): UIO[Nothing] =
ZIO.haltWith(function)

/**
* @see [[zio.ZIO.ifM]]
*/
Expand Down
13 changes: 0 additions & 13 deletions core/shared/src/main/scala/zio/URIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,6 @@ object URIO {
def failCause(cause: => Cause[Nothing])(implicit trace: ZTraceElement): UIO[Nothing] =
ZIO.failCause(cause)

/**
* @see [[zio.ZIO.failCauseWith]]
*/
def failCauseWith[R](function: (() => ZTrace) => Cause[Nothing])(implicit trace: ZTraceElement): URIO[R, Nothing] =
ZIO.failCauseWith(function)

/**
* @see [[zio.ZIO.fiberId]]
*/
Expand Down Expand Up @@ -861,13 +855,6 @@ object URIO {
def halt(cause: => Cause[Nothing])(implicit trace: ZTraceElement): UIO[Nothing] =
ZIO.halt(cause)

/**
* @see [[zio.ZIO.haltWith]]
*/
@deprecated("use failCauseWith", "2.0.0")
def haltWith[R](function: (() => ZTrace) => Cause[Nothing])(implicit trace: ZTraceElement): URIO[R, Nothing] =
ZIO.haltWith(function)

/**
* @see [[zio.ZIO.ifM]]
*/
Expand Down
32 changes: 6 additions & 26 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3423,7 +3423,7 @@ object ZIO extends ZIOCompanionPlatformSpecific {
* detected in the code.
*/
def die(t: => Throwable)(implicit trace: ZTraceElement): UIO[Nothing] =
failCauseWith(trace => Cause.Die(t, trace()))
failCause(Cause.Die(t, ZTrace.none))

/**
* Returns an effect that dies with a [[java.lang.RuntimeException]] having the
Expand Down Expand Up @@ -3655,22 +3655,13 @@ object ZIO extends ZIOCompanionPlatformSpecific {
* The moral equivalent of `throw` for pure code.
*/
def fail[E](error: => E)(implicit trace: ZTraceElement): IO[E, Nothing] =
failCauseWith(trace => Cause.Fail(error, trace()))
failCause(Cause.Fail(error, ZTrace.none))

/**
* Returns an effect that models failure with the specified `Cause`.
*/
def failCause[E](cause: => Cause[E])(implicit trace: ZTraceElement): IO[E, Nothing] =
new ZIO.Fail(_ => cause, trace)

/**
* Returns an effect that models failure with the specified `Cause`.
*
* This version takes in a lazily-evaluated trace that can be attached to the `Cause`
* via `Cause.Traced`.
*/
def failCauseWith[E](function: (() => ZTrace) => Cause[E])(implicit trace: ZTraceElement): IO[E, Nothing] =
new ZIO.Fail(function, trace)
new ZIO.Fail(() => cause, trace)

/**
* Returns the `FiberId` of the fiber executing the effect that calls this method.
Expand Down Expand Up @@ -4333,16 +4324,6 @@ object ZIO extends ZIOCompanionPlatformSpecific {
def halt[E](cause: => Cause[E])(implicit trace: ZTraceElement): IO[E, Nothing] =
failCause(cause)

/**
* Returns an effect that models failure with the specified `Cause`.
*
* This version takes in a lazily-evaluated trace that can be attached to the `Cause`
* via `Cause.Traced`.
*/
@deprecated("use failCauseWith", "2.0.0")
def haltWith[E](function: (() => ZTrace) => Cause[E])(implicit trace: ZTraceElement): IO[E, Nothing] =
failCauseWith(function)

/**
* Runs `onTrue` if the result of `b` is `true` and `onFalse` otherwise.
*/
Expand Down Expand Up @@ -4376,7 +4357,7 @@ object ZIO extends ZIOCompanionPlatformSpecific {
* Returns an effect that is interrupted as if by the specified fiber.
*/
def interruptAs(fiberId: => FiberId)(implicit trace: ZTraceElement): UIO[Nothing] =
failCauseWith(trace => Cause.interrupt(fiberId, trace()))
failCause(Cause.interrupt(fiberId))

/**
* Prefix form of `ZIO#interruptible`.
Expand Down Expand Up @@ -6237,10 +6218,9 @@ object ZIO extends ZIOCompanionPlatformSpecific {
override def tag = Tags.CheckInterrupt
}

private[zio] final class Fail[E](val fill: (() => ZTrace) => Cause[E], val trace: ZTraceElement)
extends IO[E, Nothing] { self =>
private[zio] final class Fail[E](val cause: () => Cause[E], val trace: ZTraceElement) extends IO[E, Nothing] { self =>
def unsafeLog: () => String =
() => s"Fail at ${trace}"
() => s"Fail ${cause()} at ${trace}"

override def tag = Tags.Fail

Expand Down
Loading
0