Last active
September 19, 2022 13:06
-
-
Save benwaffle/aa744910bde340fc136095d53507dd48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libraryDependencies ++= Seq( | |
"com.datadoghq" % "dd-trace-api" % "0.104.0", | |
"io.opentracing" % "opentracing-util" % "0.33.0", | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util | |
import io.opentracing.util.GlobalTracer | |
import datadog.trace.api.DDTags | |
object Tracing { | |
private val tracer = GlobalTracer.get | |
private val serviceName = sys.env.getOrElse("DD_SERVICE", "missing DD_SERVICE env var") | |
def trace[T](name: String)(tags: (String, String)*)(f: => T) = { | |
val builder = tracer.buildSpan(name) | |
.withTag(DDTags.SERVICE_NAME, serviceName) | |
.withTag(DDTags.RESOURCE_NAME, name) | |
val taggedBuilder = tags.foldLeft(builder) { case (builder, (key, value)) => | |
builder.withTag(key, value) | |
} | |
val span = taggedBuilder.start() | |
val scope = tracer.activateSpan(span) | |
try { | |
f | |
scope.close() | |
span.finish() | |
} catch { | |
case e: Throwable => | |
span.setTag("error", e.getMessage) | |
scope.close() | |
span.finish() | |
throw e | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment