All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sentry.kotlin.SentryContext.kt Maven / Gradle / Ivy

There is a newer version: 8.0.0-alpha.4
Show newest version
package io.sentry.kotlin

import io.sentry.IHub
import io.sentry.Sentry
import kotlinx.coroutines.CopyableThreadContextElement
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext

/**
 * Sentry context element for [CoroutineContext].
 */
public class SentryContext(private val hub: IHub = Sentry.getCurrentHub().clone()) :
    CopyableThreadContextElement, AbstractCoroutineContextElement(Key) {

    private companion object Key : CoroutineContext.Key

    override fun copyForChild(): CopyableThreadContextElement {
        return SentryContext(hub.clone())
    }

    override fun mergeForChild(overwritingElement: CoroutineContext.Element): CoroutineContext {
        return overwritingElement[Key] ?: SentryContext(hub.clone())
    }

    override fun updateThreadContext(context: CoroutineContext): IHub {
        val oldState = Sentry.getCurrentHub()
        Sentry.setCurrentHub(hub)
        return oldState
    }

    override fun restoreThreadContext(context: CoroutineContext, oldState: IHub) {
        Sentry.setCurrentHub(oldState)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy