Eivind
07/18/2024, 9:26 AMOs
07/19/2024, 5:10 PMEivind
07/20/2024, 8:45 AMtracer.buildSpan("aspan"){
setAttribute("foo","bar")
}.wrap {
//normal code goes here.
}
Eivind
07/20/2024, 8:46 AMEivind
07/20/2024, 8:46 AMsuspend inline fun Tracer.buildSpan(name: String, block: SpanBuilder.() -> Unit = {}): Span {
// val name = "${this::class.simpleName}.$name"
return spanBuilder(name).run{
setParent(coroutineContext.getOpenTelemetryContext())
block()
coroutineContext[CoroutineName]?.let {
setAttribute("coroutine.name", it.name)
}
startSpan()
}
}
suspend inline fun <R> Span.wrap(crossinline block: (span: Span) -> R): R {
val tSpan = this
return withContext(this.asContextElement()) {
try {
val v = block(tSpan)
tSpan.setStatus(StatusCode.OK)
v
} catch (e: CancellationException) {
tSpan.addEvent("Coroutine cancelled")
throw e
} catch (@Suppress("TooGenericExceptionCaught") throwable: Throwable) {
tSpan.setStatus(StatusCode.ERROR)
tSpan.recordException(throwable)
throw throwable
} finally {
tSpan.end()
}
}
}
Eivind
07/20/2024, 8:47 AM