Skip to content

Commit 6ed966f

Browse files
Refactor BraveTracer and clean up compilation trace
1 parent 07edfd2 commit 6ed966f

10 files changed

Lines changed: 337 additions & 255 deletions

File tree

backend/src/main/scala/bloop/tracing/BraveTracer.scala

Lines changed: 56 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package bloop.tracing
22

33
import java.util.concurrent.ConcurrentHashMap
4+
import java.nio.file.{Paths => NioPaths, Files}
5+
import java.net.URI
46

57
import scala.util.control.NonFatal
68

79
import bloop.task.Task
10+
import bloop.io.AbsolutePath
811

912
import brave.Span
1013
import brave.Tracer
@@ -92,31 +95,25 @@ object BraveTracer {
9295
ctx: Option[TraceContext],
9396
tags: (String, String)*
9497
): BraveTracer = {
95-
val braveTracer = if (properties.enabled) {
98+
if (properties.enabled) {
9699
BraveTracerInternal(name, properties, ctx, tags: _*)
97100
} else {
98-
NoopTracer
99-
}
100-
101-
if (properties.compilationTrace) {
102-
val traceFile = bloop.io.AbsolutePath(
103-
java.nio.file.Paths
104-
.get(name.stripPrefix("compile ").stripSuffix(" (transitively)"))
105-
.getParent
106-
.resolve(".bloop")
107-
.resolve("compilation-trace.json")
108-
)
109-
val projectName = tags
110-
.collectFirst { case ("compile.target", value) => value }
111-
.getOrElse(name)
112-
CompositeTracer(
113-
braveTracer,
114-
new CompilationTraceTracer(projectName, traceFile, System.currentTimeMillis())
115-
)
116-
} else {
117-
braveTracer
101+
val buildUri = tags.collectFirst { case ("workspace.dir", value) => value }
102+
buildUri match {
103+
case Some(uri) =>
104+
val workspaceDir = AbsolutePath(NioPaths.get(uri))
105+
val traceFile = workspaceDir.resolve("compilation-trace.json")
106+
if (traceFile.exists) {
107+
val projectName = tags
108+
.collectFirst { case ("compile.target", value) => value }
109+
.getOrElse(name)
110+
new CompilationTraceTracer(projectName, traceFile, System.currentTimeMillis())
111+
} else {
112+
NoopTracer
113+
}
114+
case None => NoopTracer
115+
}
118116
}
119-
120117
}
121118
}
122119

@@ -324,38 +321,45 @@ final class CompilationTraceTracer(
324321

325322
override def terminate(): Unit = {
326323
val durationMs = System.currentTimeMillis() - startTime
327-
// We only want to write the trace if we have collected the necessary tags
328-
// The "success" tag is used as a signal that the compilation finished
329-
if (tags.containsKey("success")) {
330-
val isNoOp = tags.getOrDefault("noop", "false").toBoolean
331-
val analysisOut = tags.getOrDefault("analysis", "")
332-
val classesDir = tags.getOrDefault("classesDir", "")
333-
val artifacts = TraceArtifacts(classesDir, analysisOut)
334-
val fileCount = tags.getOrDefault("fileCount", "0").toInt
335-
val files = (0 until fileCount).map(i => tags.get(s"file.$i")).filter(_ != null)
336-
337-
val trace = CompilationTrace(
338-
project,
339-
files,
340-
Seq.empty,
341-
artifacts,
342-
isNoOp,
343-
durationMs
344-
)
324+
val isNoOp = tags.getOrDefault("isNoOp", "false").toBoolean
325+
val analysisOut = tags.getOrDefault("analysis", "")
326+
val classesDir = tags.getOrDefault("classesDir", "")
327+
val artifacts = TraceArtifacts(classesDir, analysisOut)
328+
val fileCount = tags.getOrDefault("fileCount", "0").toInt
329+
val files = (0 until fileCount).map(i => tags.get(s"file.$i")).filter(_ != null)
330+
331+
import com.github.plokhotnyuk.jsoniter_scala.core.readFromString
332+
val diagnosticCount = tags.getOrDefault("diagnostics.count", "0").toInt
333+
val diagnostics = (0 until diagnosticCount).flatMap { i =>
334+
val json = tags.get(s"diagnostic.$i")
335+
if (json != null) {
336+
try {
337+
Some(readFromString[TraceDiagnostic](json)(CompilationTrace.diagnosticCodec))
338+
} catch { case NonFatal(_) => None }
339+
} else None
340+
}
345341

346-
try {
347-
if (!java.nio.file.Files.exists(traceFile.getParent.underlying)) {
348-
java.nio.file.Files.createDirectories(traceFile.getParent.underlying)
349-
}
350-
traceFile.getParent.underlying.synchronized {
351-
val projectTraceFile = traceFile.getParent.resolve(s"compilation-trace-${project}.json")
352-
val bytes = writeToArray(trace, WriterConfig.withIndentionStep(4))(CompilationTrace.codec)
353-
java.nio.file.Files.write(projectTraceFile.underlying, bytes)
354-
()
355-
}
356-
} catch {
357-
case NonFatal(e) => e.printStackTrace()
342+
val trace = CompilationTrace(
343+
project,
344+
files,
345+
diagnostics,
346+
artifacts,
347+
isNoOp,
348+
durationMs
349+
)
350+
351+
try {
352+
if (!java.nio.file.Files.exists(traceFile.getParent.underlying)) {
353+
java.nio.file.Files.createDirectories(traceFile.getParent.underlying)
358354
}
355+
traceFile.getParent.underlying.synchronized {
356+
val projectTraceFile = traceFile.getParent.resolve(s"compilation-trace-${project}.json")
357+
val bytes = writeToArray(trace, WriterConfig.withIndentionStep(4))(CompilationTrace.codec)
358+
java.nio.file.Files.write(projectTraceFile.underlying, bytes)
359+
()
360+
}
361+
} catch {
362+
case NonFatal(e) => e.printStackTrace()
359363
}
360364
}
361365

@@ -367,43 +371,3 @@ final class CompilationTraceTracer(
367371
): BraveTracer =
368372
this
369373
}
370-
371-
final case class CompositeTracer(tracers: BraveTracer*) extends BraveTracer {
372-
override def startNewChildTracer(name: String, tags: (String, String)*): BraveTracer =
373-
CompositeTracer(tracers.map(_.startNewChildTracer(name, tags: _*)): _*)
374-
375-
override def tag(key: String, value: String): Unit =
376-
tracers.foreach(_.tag(key, value))
377-
378-
override def trace[T](name: String, tags: (String, String)*)(thunk: BraveTracer => T): T = {
379-
val children = tracers.map(_.startNewChildTracer(name, tags: _*))
380-
val compositeChild = CompositeTracer(children: _*)
381-
try thunk(compositeChild)
382-
finally children.foreach(_.terminate())
383-
}
384-
385-
override def traceVerbose[T](name: String, tags: (String, String)*)(thunk: BraveTracer => T): T =
386-
trace(name, tags: _*)(thunk)
387-
388-
override def traceTask[T](name: String, tags: (String, String)*)(
389-
thunk: BraveTracer => Task[T]
390-
): Task[T] = {
391-
val children = tracers.map(_.startNewChildTracer(name, tags: _*))
392-
val compositeChild = CompositeTracer(children: _*)
393-
thunk(compositeChild).doOnFinish(_ => Task.eval(children.foreach(_.terminate())))
394-
}
395-
396-
override def traceTaskVerbose[T](name: String, tags: (String, String)*)(
397-
thunk: BraveTracer => Task[T]
398-
): Task[T] =
399-
traceTask(name, tags: _*)(thunk)
400-
401-
override def terminate(): Unit = tracers.foreach(_.terminate())
402-
override def currentSpan: Option[Span] = tracers.flatMap(_.currentSpan).headOption
403-
override def toIndependentTracer(
404-
name: String,
405-
traceProperties: TraceProperties,
406-
tags: (String, String)*
407-
): BraveTracer =
408-
CompositeTracer(tracers.map(_.toIndependentTracer(name, traceProperties, tags: _*)): _*)
409-
}

backend/src/main/scala/bloop/tracing/CompilationTrace.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ object CompilationTrace {
1717
JsonCodecMaker.make[CompilationTrace]
1818
implicit val listCodec: JsonValueCodec[List[CompilationTrace]] =
1919
JsonCodecMaker.make[List[CompilationTrace]]
20+
implicit val diagnosticCodec: JsonValueCodec[TraceDiagnostic] =
21+
JsonCodecMaker.make[TraceDiagnostic]
2022
}
2123

2224
case class TraceDiagnostic(

compile.log

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[info] welcome to sbt 1.11.7 (Oracle Corporation Java 20.0.2)
2+
[info] loading settings for project bloop-build from build.sbt...
3+
[info] loading project definition from /Users/krrishbiswas/Desktop/GSoC/bloop/project
4+
[info] loading settings for project bloop from build.sbt...
5+
[info] loading settings for project benchmark-bridge-build from plugins.sbt...
6+
[info] loading project definition from /Users/krrishbiswas/Desktop/GSoC/bloop/benchmark-bridge/project
7+
[info] loading settings for project benchmark-bridge from build.sbt...
8+
[info] resolving key references (22997 settings) ...
9+
[info] _____ __ ______ __
10+
[info] / ___/_________ _/ /___ _ / ____/__ ____ / /____ _____
11+
[info] \__ \/ ___/ __ `/ / __ `/ / / / _ \/ __ \/ __/ _ \/ ___/
12+
[info] ___/ / /__/ /_/ / / /_/ / / /___/ /__/ / / / /_/ /__/ /
13+
[info] /____/\___/\__,_/_/\__,_/ \____/\___/_/ /_/\__/\___/_/
14+
[info]
15+
[info] ***********************************************************
16+
[info] *** Welcome to the build of `loooooooooop` ***
17+
[info] *** An effort funded by the Scala Center ***
18+
[info] ***********************************************************
19+
[info]
20+
[warn] there are 3 keys that are not used by any other settings/tasks:
21+
[warn]
22+
[warn] * frontend / Test / libraryDependencies
23+
[warn] +- /Users/krrishbiswas/Desktop/GSoC/bloop/build.sbt:166
24+
[warn] * frontend / bloopName
25+
[warn] +- /Users/krrishbiswas/Desktop/GSoC/bloop/build.sbt:131
26+
[warn] * sbtBloop / sbtVersion
27+
[warn] +- /Users/krrishbiswas/Desktop/GSoC/bloop/build.sbt:322
28+
[warn]
29+
[warn] note: a setting might still be used by a command; to exclude a key from this `lintUnused` check
30+
[warn] either append it to `Global / excludeLintKeys` or call .withRank(KeyRanks.Invisible) on the key
31+
[warn] sbt 0.13 shell syntax is deprecated; use slash syntax instead: frontend / Test / compile
32+
[info] compiling 1 Scala source to /Users/krrishbiswas/Desktop/GSoC/bloop/frontend/target/scala-2.12/classes ...
33+
[info] done compiling
34+
[info] compiling 4 Scala sources to /Users/krrishbiswas/Desktop/GSoC/bloop/frontend/target/scala-2.12/test-classes ...
35+
[warn] /Users/krrishbiswas/Desktop/GSoC/bloop/frontend/src/test/scala/bloop/CompilationTraceSpec.scala:3:19: Unused import
36+
[warn] import bloop.task.Task
37+
[warn] ^
38+
[error] /Users/krrishbiswas/Desktop/GSoC/bloop/frontend/src/test/scala/bloop/io/ResourceMapperSpec.scala:25:18: not found: value ResourceMapper
39+
[error] val task = ResourceMapper.copyMappedResources(mappings, targetDir, logger)
40+
[error] ^
41+
[error] /Users/krrishbiswas/Desktop/GSoC/bloop/frontend/src/test/scala/bloop/io/ResourceMapperSpec.scala:60:18: not found: value ResourceMapper
42+
[error] val task = ResourceMapper.copyMappedResources(mappings, targetDir, logger)
43+
[error] ^
44+
[error] /Users/krrishbiswas/Desktop/GSoC/bloop/frontend/src/test/scala/bloop/io/ResourceMapperSpec.scala:85:18: not found: value ResourceMapper
45+
[error] val task = ResourceMapper.copyMappedResources(List.empty, targetDir, logger)
46+
[error] ^
47+
[error] /Users/krrishbiswas/Desktop/GSoC/bloop/frontend/src/test/scala/bloop/io/ResourceMapperSpec.scala:103:18: not found: value ResourceMapper
48+
[error] val task = ResourceMapper.copyMappedResources(mappings, targetDir, logger)
49+
[error] ^
50+
[error] /Users/krrishbiswas/Desktop/GSoC/bloop/frontend/src/test/scala/bloop/io/ResourceMapperSpec.scala:124:21: not found: value ResourceMapper
51+
[error] val changed = ResourceMapper.hasMappingsChanged(mappings, oldTimestamp)
52+
[error] ^
53+
[error] /Users/krrishbiswas/Desktop/GSoC/bloop/frontend/src/test/scala/bloop/io/ResourceMapperSpec.scala:129:24: not found: value ResourceMapper
54+
[error] val notChanged = ResourceMapper.hasMappingsChanged(mappings, futureTimestamp)
55+
[error] ^
56+
[error] /Users/krrishbiswas/Desktop/GSoC/bloop/frontend/src/test/scala/bloop/io/ResourceMapperSpec.scala:146:21: not found: value ResourceMapper
57+
[error] val changed = ResourceMapper.hasMappingsChanged(mappings, oldTimestamp)
58+
[error] ^
59+
[warn] one warning found
60+
[error] 7 errors found
61+
[info] Wrote /Users/krrishbiswas/Desktop/GSoC/bloop/integrations/sbt-bloop/target/1.9.0/scala-2.12/sbt-1.0/sbt-bloop-0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT.pom
62+
[info] Wrote /Users/krrishbiswas/Desktop/GSoC/bloop/bridges/scalajs-1/target/scala-2.12/bloop-js-bridge-1_2.12-0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT.pom
63+
[info] Created a proper POM file: /Users/krrishbiswas/Desktop/GSoC/bloop/integrations/sbt-bloop/target/1.9.0/scala-2.12/sbt-1.0/sbt-bloop_2.12_1.0.pom
64+
[info] Wrote /Users/krrishbiswas/Desktop/GSoC/bloop/bridges/scala-native-0.4/target/scala-2.12/bloop-native-bridge-0-4_2.12-0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT.pom
65+
[info] Wrote /Users/krrishbiswas/Desktop/GSoC/bloop/bridges/scala-native-0.5/target/scala-2.12/bloop-native-bridge-0-5_2.12-0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT.pom
66+
[info] Wrote /Users/krrishbiswas/Desktop/GSoC/bloop/integrations/sbt-bloop/target/1.9.0/scala-2.12/sbt-1.0/sbt-bloop_2.12_1.0-0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT.pom
67+
[info] :: delivering :: ch.epfl.scala#sbt-bloop;0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT :: 0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT :: integration :: Wed Jan 14 12:32:19 IST 2026
68+
[info] delivering ivy file to /Users/krrishbiswas/Desktop/GSoC/bloop/integrations/sbt-bloop/target/1.9.0/scala-2.12/sbt-1.0/ivy-0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT.xml
69+
[info] published sbt-bloop_2.12_1.0 to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/sbt-bloop/scala_2.12/sbt_1.0/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/poms/sbt-bloop_2.12_1.0.pom
70+
[info] published sbt-bloop to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/sbt-bloop/scala_2.12/sbt_1.0/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/jars/sbt-bloop.jar
71+
[info] published sbt-bloop_2.12_1.0 to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/sbt-bloop/scala_2.12/sbt_1.0/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/docs/sbt-bloop_2.12_1.0-javadoc.jar
72+
[info] published sbt-bloop_2.12_1.0 to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/sbt-bloop/scala_2.12/sbt_1.0/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/jars/sbt-bloop_2.12_1.0.jar
73+
[info] published sbt-bloop to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/sbt-bloop/scala_2.12/sbt_1.0/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/poms/sbt-bloop.pom
74+
[info] published sbt-bloop_2.12_1.0 to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/sbt-bloop/scala_2.12/sbt_1.0/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/srcs/sbt-bloop_2.12_1.0-sources.jar
75+
[info] published ivy to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/sbt-bloop/scala_2.12/sbt_1.0/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/ivys/ivy.xml
76+
[info] :: delivering :: ch.epfl.scala#bloop-native-bridge-0-4_2.12;0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT :: 0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT :: integration :: Wed Jan 14 12:32:19 IST 2026
77+
[info] delivering ivy file to /Users/krrishbiswas/Desktop/GSoC/bloop/bridges/scala-native-0.4/target/scala-2.12/ivy-0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT.xml
78+
[info] published bloop-native-bridge-0-4_2.12 to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/bloop-native-bridge-0-4_2.12/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/poms/bloop-native-bridge-0-4_2.12.pom
79+
[info] published bloop-native-bridge-0-4_2.12 to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/bloop-native-bridge-0-4_2.12/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/jars/bloop-native-bridge-0-4_2.12.jar
80+
[info] published ivy to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/bloop-native-bridge-0-4_2.12/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/ivys/ivy.xml
81+
[info] :: delivering :: ch.epfl.scala#bloop-native-bridge-0-5_2.12;0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT :: 0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT :: integration :: Wed Jan 14 12:32:20 IST 2026
82+
[info] delivering ivy file to /Users/krrishbiswas/Desktop/GSoC/bloop/bridges/scala-native-0.5/target/scala-2.12/ivy-0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT.xml
83+
[info] published bloop-native-bridge-0-5_2.12 to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/bloop-native-bridge-0-5_2.12/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/poms/bloop-native-bridge-0-5_2.12.pom
84+
[info] published bloop-native-bridge-0-5_2.12 to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/bloop-native-bridge-0-5_2.12/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/jars/bloop-native-bridge-0-5_2.12.jar
85+
[info] published ivy to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/bloop-native-bridge-0-5_2.12/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/ivys/ivy.xml
86+
[info] :: delivering :: ch.epfl.scala#bloop-js-bridge-1_2.12;0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT :: 0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT :: integration :: Wed Jan 14 12:32:20 IST 2026
87+
[info] delivering ivy file to /Users/krrishbiswas/Desktop/GSoC/bloop/bridges/scalajs-1/target/scala-2.12/ivy-0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT.xml
88+
[info] published bloop-js-bridge-1_2.12 to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/bloop-js-bridge-1_2.12/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/poms/bloop-js-bridge-1_2.12.pom
89+
[info] published bloop-js-bridge-1_2.12 to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/bloop-js-bridge-1_2.12/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/jars/bloop-js-bridge-1_2.12.jar
90+
[info] published ivy to /Users/krrishbiswas/.ivy2/local/ch.epfl.scala/bloop-js-bridge-1_2.12/0.0.0-6265-07edfd2a-20260114-1231-SNAPSHOT/ivys/ivy.xml
91+
[error] (frontend / Test / compileIncremental) Compilation failed
92+
[error] Total time: 25 s, completed Jan 14, 2026, 12:32:22 PM

0 commit comments

Comments
 (0)