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

improvement: Disable tracing by default #2165

Merged
merged 1 commit into from
Sep 27, 2023
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
110 changes: 96 additions & 14 deletions backend/src/main/scala/bloop/tracing/BraveTracer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,100 @@ import brave.propagation.TraceContextOrSamplingFlags
import zipkin2.codec.SpanBytesEncoder.JSON_V1
import zipkin2.codec.SpanBytesEncoder.JSON_V2

final class BraveTracer private (
sealed trait BraveTracer {
def startNewChildTracer(name: String, tags: (String, String)*): BraveTracer

def trace[T](name: String, tags: (String, String)*)(
thunk: BraveTracer => T
): T

def traceVerbose[T](name: String, tags: (String, String)*)(
thunk: BraveTracer => T
): T

def traceTask[T](name: String, tags: (String, String)*)(
thunk: BraveTracer => Task[T]
): Task[T]

def traceTaskVerbose[T](name: String, tags: (String, String)*)(
thunk: BraveTracer => Task[T]
): Task[T]

def terminate(): Unit

def currentSpan: Option[Span]

def toIndependentTracer(
name: String,
traceProperties: TraceProperties,
tags: (String, String)*
): BraveTracer

}

object NoopTracer extends BraveTracer {

override def startNewChildTracer(name: String, tags: (String, String)*): BraveTracer = this

override def trace[T](name: String, tags: (String, String)*)(thunk: BraveTracer => T): T = thunk(
this
)

override def traceVerbose[T](name: String, tags: (String, String)*)(thunk: BraveTracer => T): T =
thunk(this)

override def traceTask[T](name: String, tags: (String, String)*)(
thunk: BraveTracer => Task[T]
): Task[T] = thunk(this)

override def traceTaskVerbose[T](name: String, tags: (String, String)*)(
thunk: BraveTracer => Task[T]
): Task[T] = thunk(this)

override def terminate(): Unit = ()

override def currentSpan: Option[Span] = None

def toIndependentTracer(
name: String,
traceProperties: TraceProperties,
tags: (String, String)*
): BraveTracer = this

}

object BraveTracer {

def apply(name: String, properties: TraceProperties, tags: (String, String)*): BraveTracer = {
BraveTracer(name, properties, None, tags: _*)
}

def apply(
name: String,
properties: TraceProperties,
ctx: Option[TraceContext],
tags: (String, String)*
): BraveTracer = {
if (properties.enabled) {
BraveTracerInternal(name, properties, ctx, tags: _*)
} else {
NoopTracer
}

}
}

final class BraveTracerInternal private (
tracer: Tracer,
val currentSpan: Span,
val _currentSpan: Span,
closeCurrentSpan: () => Unit,
properties: TraceProperties
) {
) extends BraveTracer {

def currentSpan = Some(_currentSpan)

def startNewChildTracer(name: String, tags: (String, String)*): BraveTracer = {
val span = tags.foldLeft(tracer.newChild(currentSpan.context).name(name)) {
val span = tags.foldLeft(tracer.newChild(_currentSpan.context).name(name)) {
case (span, (tagKey, tagValue)) => span.tag(tagKey, tagValue)
}

Expand All @@ -32,7 +118,7 @@ final class BraveTracer private (
span.finish()
}

new BraveTracer(tracer, span, closeHandler, properties)
new BraveTracerInternal(tracer, span, closeHandler, properties)
}

def trace[T](name: String, tags: (String, String)*)(
Expand Down Expand Up @@ -67,7 +153,7 @@ final class BraveTracer private (
try thunk(newTracer) // Don't catch and report errors in spans
catch {
case NonFatal(t) =>
newTracer.currentSpan.error(t)
newTracer.currentSpan.foreach(_.error(t))
throw t
} finally {
try newTracer.terminate()
Expand All @@ -91,7 +177,7 @@ final class BraveTracer private (
case None => Task.eval(newTracer.terminate())
case Some(value) =>
Task.eval {
newTracer.currentSpan.error(value)
newTracer.currentSpan.foreach(_.error(value))
newTracer.terminate()
}
}
Expand All @@ -116,10 +202,10 @@ final class BraveTracer private (
traceProperties: TraceProperties,
tags: (String, String)*
): BraveTracer =
BraveTracer(name, traceProperties, Some(currentSpan.context), tags: _*)
BraveTracer(name, traceProperties, Some(_currentSpan.context), tags: _*)
}

object BraveTracer {
object BraveTracerInternal {
import brave._
import zipkin2.reporter.AsyncReporter
import zipkin2.reporter.urlconnection.URLConnectionSender
Expand All @@ -134,10 +220,6 @@ object BraveTracer {
reporterCache.computeIfAbsent(url, newReporter)
}

def apply(name: String, properties: TraceProperties, tags: (String, String)*): BraveTracer = {
BraveTracer(name, properties, None, tags: _*)
}

def apply(
name: String,
properties: TraceProperties,
Expand Down Expand Up @@ -178,6 +260,6 @@ object BraveTracer {
spanReporter.flush()
}

new BraveTracer(tracer, rootSpan, closeEverything, properties)
new BraveTracerInternal(tracer, rootSpan, closeEverything, properties)
}
}
7 changes: 5 additions & 2 deletions backend/src/main/scala/bloop/tracing/TraceProperties.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ case class TraceProperties(
verbose: Boolean,
localServiceName: String,
traceStartAnnotation: Option[String],
traceEndAnnotation: Option[String]
traceEndAnnotation: Option[String],
enabled: Boolean
)

object TraceProperties {
val default: TraceProperties = {
val verbose = Properties.propOrFalse("bloop.tracing.verbose")
val enabled = Properties.propOrFalse("bloop.tracing.enabled")
val debugTracing = Properties.propOrFalse("bloop.tracing.debugTracing")
val localServiceName = Properties.propOrElse("bloop.tracing.localServiceName", "bloop")
val traceStartAnnotation = Properties.propOrNone("bloop.tracing.traceStartAnnotation")
Expand All @@ -30,7 +32,8 @@ object TraceProperties {
verbose,
localServiceName,
traceStartAnnotation,
traceEndAnnotation
traceEndAnnotation,
enabled
)
}
}
Loading