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

Filtering stack trace #144

Merged
merged 3 commits into from
May 20, 2020
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
18 changes: 16 additions & 2 deletions benchmarks/src/main/scala/io/odin/Benchmarks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,25 @@ class FormatterBenchmarks extends OdinBenchmarks {
private val noThrowable: LoggerMessage = noCtx.copy(exception = None)

private val formatterDepth = Formatter.create(
ThrowableFormat(ThrowableFormat.Depth.Fixed(2), ThrowableFormat.Indent.NoIndent),
ThrowableFormat(ThrowableFormat.Depth.Fixed(2), ThrowableFormat.Indent.NoIndent, ThrowableFormat.Filter.NoFilter),
PositionFormat.Full,
colorful = false,
printCtx = true
)

private val formatterDepthIndent = Formatter.create(
ThrowableFormat(ThrowableFormat.Depth.Fixed(2), ThrowableFormat.Indent.Fixed(4)),
ThrowableFormat(ThrowableFormat.Depth.Fixed(2), ThrowableFormat.Indent.Fixed(4), ThrowableFormat.Filter.NoFilter),
PositionFormat.Full,
colorful = false,
printCtx = true
)

private val formatterDepthIndentFilter = Formatter.create(
ThrowableFormat(
ThrowableFormat.Depth.Fixed(2),
ThrowableFormat.Indent.Fixed(4),
ThrowableFormat.Filter.Excluding("cats.effect.IOApp", "io.odin.OdinBenchmarks", "io.odin.FormatterBenchmarks")
),
PositionFormat.Full,
colorful = false,
printCtx = true
Expand Down Expand Up @@ -248,6 +259,9 @@ class FormatterBenchmarks extends OdinBenchmarks {
@Benchmark
def depthIndentFormatter(): Unit = formatterDepthIndent.format(loggerMessage)

@Benchmark
def depthIndentFilterFormatter(): Unit = formatterDepthIndentFilter.format(loggerMessage)

@Benchmark
def abbreviatedPositionFormatter(): Unit = abbreviated.format(loggerMessage)

Expand Down
11 changes: 10 additions & 1 deletion core/src/main/scala/io/odin/formatter/Formatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ object Formatter {
case ThrowableFormat.Depth.Fixed(size) => Some(size)
}

val filter: Option[Set[String]] = format.filter match {
case ThrowableFormat.Filter.NoFilter => None
case ThrowableFormat.Filter.Excluding(prefixes) => Some(prefixes)
}

@tailrec
def loop(t: Throwable, builder: StringBuilder): StringBuilder = {
builder.append("Caused by: ")
Expand All @@ -134,7 +139,11 @@ object Formatter {
builder.append(t.getLocalizedMessage)
}
builder.append(System.lineSeparator())
writeStackTrace(builder, depth.fold(t.getStackTrace)(t.getStackTrace.take), indent)

val filteredStackTrace = filter.fold(t.getStackTrace)(set =>
t.getStackTrace.filterNot(row => set.contains(row.getClassName.stripSuffix("$")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be faster to move this check into writeStackTrace loop and save few allocations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then we'll need to move the t.getStackTrace.take part into writeStackTrace loop too, because we could take(5) and then filter them out, having empty list of rows.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it's possible to just count amount of actual lines printed and stop whenever depth is achieved

no worries, it's a matter of optimization in separate PR then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I make this PR for optimization?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lightning95 sorry haven't noticed it earlier

yes, feel free to

)
writeStackTrace(builder, depth.fold(filteredStackTrace)(filteredStackTrace.take), indent)
if (Option(t.getCause).isEmpty) {
builder
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.odin.formatter.options

final case class ThrowableFormat(depth: ThrowableFormat.Depth, indent: ThrowableFormat.Indent)
final case class ThrowableFormat(
depth: ThrowableFormat.Depth,
indent: ThrowableFormat.Indent,
filter: ThrowableFormat.Filter
)

object ThrowableFormat {

val Default: ThrowableFormat = ThrowableFormat(Depth.Full, Indent.NoIndent)
val Default: ThrowableFormat = ThrowableFormat(Depth.Full, Indent.NoIndent, Filter.NoFilter)

sealed trait Depth
object Depth {
Expand All @@ -18,4 +22,14 @@ object ThrowableFormat {
final case class Fixed(size: Int) extends Indent
}

sealed trait Filter
object Filter {
final case object NoFilter extends Filter
final case class Excluding(prefixes: Set[String]) extends Filter

object Excluding {
def apply(prefixes: String*): Excluding = new Excluding(prefixes.toSet)
}
}

}
54 changes: 49 additions & 5 deletions core/src/test/scala/io/odin/formatter/FormatterSpec.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.odin.formatter

import io.odin.{LoggerMessage, OdinSpec}
import io.odin.formatter.options.{PositionFormat, ThrowableFormat}
import io.odin.formatter.options.ThrowableFormat.{Depth, Indent}
import io.odin.formatter.FormatterSpec.TestException
import io.odin.formatter.options.ThrowableFormat.{Depth, Filter, Indent}
import io.odin.formatter.options.{PositionFormat, ThrowableFormat}
import io.odin.meta.Position
import io.odin.{LoggerMessage, OdinSpec}
import org.scalacheck.Gen

import scala.util.control.NoStackTrace
Expand All @@ -22,7 +22,7 @@ class FormatterSpec extends OdinSpec {
val error1 = TestException("Exception 1")
val error2 = new RuntimeException("Exception 2", error1)

val format = ThrowableFormat(Depth.Full, indent)
val format = ThrowableFormat(Depth.Full, indent, Filter.NoFilter)
val result = Formatter.formatThrowable(error2, format)
val lines = result.split(System.lineSeparator()).toList

Expand All @@ -48,7 +48,7 @@ class FormatterSpec extends OdinSpec {
val error1 = TestException("Exception 1")
val error2 = new RuntimeException("Exception 2", error1)

val format = ThrowableFormat(depth, indent)
val format = ThrowableFormat(depth, indent, Filter.NoFilter)
val result = Formatter.formatThrowable(error2, format)
val lines = result.split(System.lineSeparator()).toList

Expand All @@ -68,6 +68,42 @@ class FormatterSpec extends OdinSpec {
trace.length shouldBe expectedLength
}

it should "support Filter" in forAll(filterGen) { filter =>
val excluding = filter match {
case Filter.NoFilter => Set.empty[String]
case Filter.Excluding(prefixes) => prefixes
}

val stackTraceElements = Array(
new StackTraceElement("class1", "method1", "fileName1", 1),
new StackTraceElement("class2", "method1", "fileName2", 1),
new StackTraceElement("class2$", "method2", "fileName2", 2),
new StackTraceElement("class3", "method1", "fileName3", 1)
)
val error1 = TestException("Exception 1")
error1.setStackTrace(stackTraceElements)
val error2 = new RuntimeException("Exception 2", error1)
error2.setStackTrace(stackTraceElements)

val format = ThrowableFormat(Depth.Full, Indent.Fixed(2), filter)
val result = Formatter.formatThrowable(error2, format)
val lines = result.split(System.lineSeparator()).toList

val (cause, trace) = lines.partition(_.startsWith("Caused by"))
val expectedCausedBy = List(
"Caused by: java.lang.RuntimeException: Exception 2",
s"Caused by: io.odin.formatter.FormatterSpec$$TestException: Exception 1"
)
val expectedLength = error1.getStackTrace
.++(error1.getStackTrace)
.map(_.getClassName.replace("$", ""))
.filterNot(excluding.contains)
.length

cause shouldBe expectedCausedBy
trace.length shouldBe expectedLength
}

behavior of "Formatter.formatPosition with PositionFormat"

it should "support PositionFormat" in forAll(positionFormatGen, Gen.posNum[Int]) { (format, line) =>
Expand Down Expand Up @@ -133,6 +169,14 @@ class FormatterSpec extends OdinSpec {
Gen.posNum[Int].map(size => Depth.Fixed(size))
)

private lazy val filterGen: Gen[Filter] =
Gen.oneOf(
Gen.const(Filter.NoFilter),
Gen.someOf("class1", "class3", "class2", "notIncludedClass")
.map(_.toSet)
.map(Filter.Excluding.apply)
)

private lazy val positionFormatGen: Gen[PositionFormat] =
Gen.oneOf(
Gen.const(PositionFormat.Full),
Expand Down
40 changes: 40 additions & 0 deletions examples/src/main/scala/io/odin/examples/FilteringStackTrace.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.odin.examples

import cats.effect.{ExitCode, IO, IOApp}
import io.odin._
import io.odin.formatter.Formatter
import io.odin.formatter.options.ThrowableFormat

/**
* Filters stack trace of the thrown exception.
* Prints:
* ERROR io.odin.examples.FilteringStackTrace.run:39 - This is an exception
* Caused by: java.lang.RuntimeException: here
* io.odin.examples.FilteringStackTrace$.run(FilteringStackTrace.scala:39)
* io.odin.examples.FilteringStackTrace$.main(FilteringStackTrace.scala:30)
* io.odin.examples.FilteringStackTrace.main(FilteringStackTrace.scala)
* "
*
* Instead of:
* ERROR io.odin.examples.FilteringStackTrace.run:39 - This is an exception
* Caused by: java.lang.RuntimeException: here
* io.odin.examples.FilteringStackTrace$.run(FilteringStackTrace.scala:39)
* cats.effect.IOApp.$anonfun$main$3(IOApp.scala:68)
* cats.effect.internals.IOAppPlatform$.mainFiber(IOAppPlatform.scala:40)
* cats.effect.internals.IOAppPlatform$.main(IOAppPlatform.scala:25)
* cats.effect.IOApp.main(IOApp.scala:68)
* cats.effect.IOApp.main$(IOApp.scala:67)
* io.odin.examples.FilteringStackTrace$.main(FilteringStackTrace.scala:30)
* io.odin.examples.FilteringStackTrace.main(FilteringStackTrace.scala)
*/
object FilteringStackTrace extends IOApp {
val throwableFormat: ThrowableFormat = ThrowableFormat(
ThrowableFormat.Depth.Fixed(3),
ThrowableFormat.Indent.NoIndent,
ThrowableFormat.Filter.Excluding("cats.effect.IOApp", "cats.effect.internals.IOAppPlatform")
)
val logger: Logger[IO] = consoleLogger(formatter = Formatter.create(throwableFormat, colorful = true))

def run(args: List[String]): IO[ExitCode] =
logger.error("This is an exception", new RuntimeException("here")).as(ExitCode.Success)
}