Skip to content

Commit

Permalink
Add missing class name to interop trace (#672)
Browse files Browse the repository at this point in the history
* Add missing class name to interop trace

* Remove redundant interns of pieces of trace string

* Increase cache initial capacity

* fix tracer tests

* Remove redundant 0 column
  • Loading branch information
neko-kai authored Jun 26, 2023
1 parent ca3f501 commit b120f74
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ object InteropTracerSpec extends ZIOSpecDefault {

val result = InteropTracer.newTrace(myLambda)

assertTrue(result == "myLambda(InteropTracerSpec.scala:8:0)".asInstanceOf[Trace])
assertTrue(
result == "zio.internal.stacktracer.InteropTracerSpec$.myLambda(InteropTracerSpec.scala:8)"
.asInstanceOf[Trace]
)
},
test("tracing 'by name' parameter") {

Expand All @@ -23,7 +26,9 @@ object InteropTracerSpec extends ZIOSpecDefault {

val result = check(42)

assertTrue(result == "spec(InteropTracerSpec.scala:24:0)".asInstanceOf[Trace])
assertTrue(
result == "zio.internal.stacktracer.InteropTracerSpec$.spec(InteropTracerSpec.scala:27)".asInstanceOf[Trace]
)
}
).@@(TestAspect.exceptScala3)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import scala.util.matching.Regex

object InteropTracer {
final def newTrace(f: AnyRef): Trace = {
val clazz = f.getClass()
val clazz = f.getClass

val cachedTrace = cache.get(clazz)
if (cachedTrace == null) {
val computedTrace = AkkaLineNumbers(f) match {
Expand All @@ -29,25 +30,26 @@ object InteropTracer {
case AkkaLineNumbers.UnknownSourceFormat(_) => Tracer.instance.empty

case AkkaLineNumbers.SourceFile(filename) =>
createTrace("<unknown>", filename.intern(), 0, 0).asInstanceOf[Trace]
createTrace("<unknown>", filename, 0).asInstanceOf[Trace]

case AkkaLineNumbers.SourceFileLines(filename, from, _, _, methodAnonfun) =>
case AkkaLineNumbers.SourceFileLines(filename, from, _, classNameSlashes, methodAnonfun) =>
val className = classNameSlashes.replace('/', '.')
val methodName = lambdaNamePattern
.findFirstMatchIn(methodAnonfun)
.flatMap(Option apply _.group(1))
.getOrElse(methodAnonfun)

createTrace(methodName.intern(), filename.intern(), from, 0).asInstanceOf[Trace]
createTrace(className + "." + methodName, filename, from).asInstanceOf[Trace]
}
cache.put(clazz, computedTrace)
computedTrace
} else cachedTrace
}

private val cache: ConcurrentMap[Class[?], Trace] = new ConcurrentHashMap[Class[?], Trace]()
private val cache: ConcurrentMap[Class[?], Trace] = new ConcurrentHashMap[Class[?], Trace](10000)

private def createTrace(location: String, file: String, line: Int, column: Int): String =
s"$location($file:$line:$column)".intern
private def createTrace(location: String, file: String, line: Int): String =
s"$location($file:$line)".intern()

private final val lambdaNamePattern: Regex = """\$anonfun\$(.+?)\$\d""".r

Expand Down

0 comments on commit b120f74

Please sign in to comment.