-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to perform basic custom instrumentation with Transaction and Spans #83
Comments
Please also provide data class SpanContext(val span: Span) : AbstractCoroutineContextElement(SpanContext) {
companion object Key : CoroutineContext.Key<SpanContext>
}
fun CoroutineContext.currentSpan(): Span {
return get(SpanContext)?.span ?: throw IllegalStateException("No active Span in context")
}
suspend fun <T> withTrace(
name: String,
spanName: String,
block: suspend CoroutineScope.() -> T
): T {
val trace = startTrace(name, spanName)
return withSpan(trace, block)
}
suspend fun <T> withSpan(name: String, block: suspend CoroutineScope.() -> T): T {
val span = currentCoroutineContext().currentSpan()
val childSpan = span.startChild(name)
return withSpan(childSpan, block)
}
suspend fun <T> withSpan(span: Span, block: suspend CoroutineScope.() -> T): T {
return try {
withContext(SpanContext(span), block)
} catch (e: Throwable) {
if (e is CancellationException) {
span.setStatus("cancelled")
} else {
span.setThrowable(e)
span.setStatus("error")
}
throw e
} finally {
span.finish()
}
} This allows to add the context to the coroutine context, which ensures it is propagates in suspend code with structured concurrency. This means the child span must no be propagated manually. The extension functions allow to instrument code as follows: suspend fun getData(): String {
return withSpan("getData") {
delay(1000)
"foo"
}
} |
This would allow developers to create custom transactions and spans in their code to capture information about their application's performance and behavior.
At the very least support:
Transaction
Span
startTransaction()
startChild()
getSpan()
tracesSampleRate
optiontracesSampler
optionAlign implementation with:
Blocked by
The text was updated successfully, but these errors were encountered: