Are there any good projects ongoing to make it eas...
# ktor
e
Are there any good projects ongoing to make it easy use OpenTelemetry in Kotlin. I find trying to juse the java library add allot of boiler plate kode, to get it passed along and setup. Or any good excamples of pepole using otel?
o
I’m also after this. Have you found anything useful so far @Eivind?
e
Currently no, I started playing whit some helper methods So i can write some thing like this
Copy code
tracer.buildSpan("aspan"){
  setAttribute("foo","bar")
}.wrap {
  //normal code goes here.
}
I made som extention functions for that, but im still lokking for something nicer.
Copy code
suspend 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()
        }
    }
}
Not 100% all the stuff i put there is actually needed