-
Notifications
You must be signed in to change notification settings - Fork 13
Closed
Description
Kotlin coroutines have similar need as InProcessLinkTracer
, but varies just slightly since coroutines are suspended and resumed across threads. This means the start/end mechanism is slightly different.
I would like either:
1- Documentation/guidance of how to interact with the existing SDK in a way that works with coroutines
or
2- Provide helper methods that can be used directly to use with coroutines.
Example coroutine context
class DynatraceCoroutineContext(
private val oneAgentSdk: OneAgentSDK,
private val inProcessLink: InProcessLink,
) : ThreadContextElement<InProcessLinkTracer> {
companion object Key : CoroutineContext.Key<DynatraceCoroutineContext>
override val key: CoroutineContext.Key<DynatraceCoroutineContext> get() = Key
override fun updateThreadContext(context: CoroutineContext): InProcessLinkTracer {
val trace = oneAgentSdk.traceInProcessLink(inProcessLink)
trace.start()
return trace
}
override fun restoreThreadContext(context: CoroutineContext, oldState: InProcessLinkTracer) {
oldState.end()
}
}
Note there is no appropriate place to catch exceptions or end timings.
Perhaps an example extension function could also explain how to leverage this?
public fun <T> CoroutineScope.asyncWithDynatrace(
sdk: OneAgentSDK,
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T
): Deferred<T> {
val inProcessLink = sdk.createInProcessLink()
val inProcessLinkTracer = sdk.traceInProcessLink(inProcessLink)
inProcessLinkTracer.start()
val dynatraceContext = DynatraceCoroutineContext(sdk, inProcessLink)
return try {
async(context+dynatraceContext, start, block)
} catch (e: Exception) {
inProcessLinkTracer.error(e);
throw e
} finally {
inProcessLinkTracer.end();
}
}
Thanks!
Metadata
Metadata
Assignees
Labels
No labels