Coverage Summary for Class: OTelKt (dev.suresh.plugins)
Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
OTelKt |
0%
(0/8)
|
0%
(0/14)
|
0%
(0/19)
|
0%
(0/152)
|
OTelKt$openTelemetrySdk$2$ins$1 |
0%
(0/1)
|
|
0%
(0/1)
|
0%
(0/2)
|
Total |
0%
(0/9)
|
0%
(0/14)
|
0%
(0/20)
|
0%
(0/154)
|
package dev.suresh.plugins
import dev.suresh.http.log
import io.ktor.http.HttpMethod
import io.ktor.server.application.*
import io.ktor.server.request.httpMethod
import io.opentelemetry.api.*
import io.opentelemetry.api.trace.SpanKind
import io.opentelemetry.instrumentation.ktor.v3_0.*
import io.opentelemetry.sdk.extension.incubator.fileconfig.*
import kotlin.io.path.*
import kotlin.time.Clock
fun Application.configureOTel() {
install(KtorServerTelemetry) {
setOpenTelemetry(openTelemetrySdk)
spanKindExtractor {
if (httpMethod == HttpMethod.Post) {
SpanKind.PRODUCER
} else {
SpanKind.CLIENT
}
}
attributesExtractor {
onStart { attributes.put("start-time", Clock.System.now().toEpochMilliseconds()) }
onEnd { attributes.put("end-time", Clock.System.now().toEpochMilliseconds()) }
}
}
// install(OTelExtnPlugin) { enabled = true }
// openTelemetrySdk.shutdown().join(10, TimeUnit.SECONDS);
}
/** See [Configure the SDK](https://opentelemetry.io/docs/languages/java/configuration/) */
val openTelemetrySdk: OpenTelemetry by lazy {
val sdkConfPath =
System.getenv("OTEL_EXPERIMENTAL_CONFIG_FILE")?.takeIf { it.isNotBlank() }?.let { Path(it) }
val ins =
when (sdkConfPath) {
null -> object {}::class.java.getResourceAsStream("/otel/sdk-config.yaml")
else -> sdkConfPath.inputStream()
}
runCatching { DeclarativeConfiguration.parseAndCreate(ins) }
.onFailure { log.error(it) {} }
.getOrElse { GlobalOpenTelemetry.get() }
}