Skip to content

Instantly share code, notes, and snippets.

@benwaffle
Last active September 19, 2022 13:06
Show Gist options
  • Save benwaffle/aa744910bde340fc136095d53507dd48 to your computer and use it in GitHub Desktop.
Save benwaffle/aa744910bde340fc136095d53507dd48 to your computer and use it in GitHub Desktop.
libraryDependencies ++= Seq(
"com.datadoghq" % "dd-trace-api" % "0.104.0",
"io.opentracing" % "opentracing-util" % "0.33.0",
)
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