Skip to content

Fiber Failure Metric Double Counts Failures #9742

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

Open
khajavi opened this issue Apr 1, 2025 · 1 comment
Open

Fiber Failure Metric Double Counts Failures #9742

khajavi opened this issue Apr 1, 2025 · 1 comment

Comments

@khajavi
Copy link
Member

khajavi commented Apr 1, 2025

I expect this example to print 5 for String causes and 2 for null pointer exception causes.

import zio._

object RuntimeMetricsExample extends ZIOAppDefault {
  override val bootstrap =
    Runtime.enableRuntimeMetrics

  val nullErrors   =
    ZIO.foreachParDiscard(1 to 2)(_ => ZIO.attempt(null.toString))

  val customErrors =
    ZIO.foreachParDiscard(1 to 5)(_ => ZIO.fail("Custom application error"))

  def run = {
    nullErrors <&> customErrors
  }.onError { _ =>
    Metric.runtime.fiberFailureCauses.value
      .flatMap(failureCauses =>
        ZIO
          .foreachDiscard(failureCauses.occurrences) { case (cause, count) =>
            Console.printLine(s"Failure cause: $cause, Count: $count")
          }
          .orDie
      )
  }.ignore
}

But the output is:

Failure cause: java.lang.String, Count: 10
Failure cause: java.lang.NullPointerException, Count: 4
@kyri-petrou
Copy link
Contributor

The issue here is that when we join the results of the two fibers created by <&> we're essentially creating a new Cause that concats all the failures and we count them again.

One way to fix this would be to filter on the fiberId in the Cause's traces, but that would only work with ZIO.fail / ZIO.attempt etc, but not with Exit.fail. In the case of Exit.fail we would have to either double-count or not include it in metrics at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants