-
-
Notifications
You must be signed in to change notification settings - Fork 466
Add coroutines support #1479
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
Merged
Merged
Add coroutines support #1479
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8329110
First attempt to make SDK coroutines-friendly.
maciejwalkowiak ad373f6
WIP
maciejwalkowiak 324f90d
WIP
maciejwalkowiak af37d98
Merge remote-tracking branch 'origin/main' into gh-973
maciejwalkowiak a2e916a
Spotless
maciejwalkowiak 9c5fb4f
WIP
maciejwalkowiak 4bd01a3
Move SentryContext to separate module.
maciejwalkowiak 44155f9
Polish.
maciejwalkowiak 7f299c3
Add API.
maciejwalkowiak 4c4d05c
Polish build.gradle.kts
maciejwalkowiak 5f78d07
Merge remote-tracking branch 'origin/main' into gh-973
maciejwalkowiak 8d55869
Update Coroutines dependency version.
maciejwalkowiak f776e9a
Changelog.
maciejwalkowiak 4d8d7b7
Polish.
maciejwalkowiak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public final class io/sentry/kotlin/SentryContext : kotlin/coroutines/AbstractCoroutineContextElement, kotlinx/coroutines/ThreadContextElement { | ||
| public static final field Key Lio/sentry/kotlin/SentryContext$Key; | ||
| public fun <init> ()V | ||
| public fun fold (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; | ||
| public fun get (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext$Element; | ||
| public fun minusKey (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext; | ||
| public fun plus (Lkotlin/coroutines/CoroutineContext;)Lkotlin/coroutines/CoroutineContext; | ||
| public fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Lio/sentry/IHub;)V | ||
| public synthetic fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Object;)V | ||
| public fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Lio/sentry/IHub; | ||
| public synthetic fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Ljava/lang/Object; | ||
| } | ||
|
|
||
| public final class io/sentry/kotlin/SentryContext$Key : kotlin/coroutines/CoroutineContext$Key { | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import net.ltgt.gradle.errorprone.errorprone | ||
| import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
|
||
| plugins { | ||
| `java-library` | ||
| kotlin("jvm") | ||
| jacoco | ||
| id(Config.QualityPlugins.errorProne) | ||
| id(Config.QualityPlugins.gradleVersions) | ||
| id(Config.QualityPlugins.detektPlugin) | ||
| } | ||
|
|
||
| configure<JavaPluginConvention> { | ||
| sourceCompatibility = JavaVersion.VERSION_1_8 | ||
| targetCompatibility = JavaVersion.VERSION_1_8 | ||
| } | ||
|
|
||
| tasks.withType<KotlinCompile>().configureEach { | ||
| kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
| } | ||
|
|
||
| dependencies { | ||
| api(project(":sentry")) | ||
| implementation(Config.Libs.coroutinesCore) | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| compileOnly(Config.CompileOnly.nopen) | ||
| errorprone(Config.CompileOnly.nopenChecker) | ||
| errorprone(Config.CompileOnly.errorprone) | ||
| errorproneJavac(Config.CompileOnly.errorProneJavac8) | ||
| compileOnly(Config.CompileOnly.jetbrainsAnnotations) | ||
|
|
||
| // tests | ||
| testImplementation(project(":sentry-test-support")) | ||
| testImplementation(kotlin(Config.kotlinStdLib)) | ||
| testImplementation(Config.TestLibs.kotlinTestJunit) | ||
| testImplementation(Config.TestLibs.mockitoKotlin) | ||
| } | ||
|
|
||
| configure<SourceSetContainer> { | ||
| test { | ||
| java.srcDir("src/test/java") | ||
| } | ||
| } | ||
|
|
||
| jacoco { | ||
| toolVersion = Config.QualityPlugins.Jacoco.version | ||
| } | ||
|
|
||
| tasks.jacocoTestReport { | ||
| reports { | ||
| xml.isEnabled = true | ||
| html.isEnabled = false | ||
| } | ||
| } | ||
|
|
||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| tasks { | ||
| jacocoTestCoverageVerification { | ||
| violationRules { | ||
| rule { limit { minimum = Config.QualityPlugins.Jacoco.minimumCoverage } } | ||
| } | ||
| } | ||
| check { | ||
| dependsOn(jacocoTestCoverageVerification) | ||
| dependsOn(jacocoTestReport) | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
sentry-kotlin-extensions/src/main/java/io/sentry/kotlin/SentryContext.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package io.sentry.kotlin | ||
|
|
||
| import io.sentry.IHub | ||
| import io.sentry.Sentry | ||
| import kotlin.coroutines.AbstractCoroutineContextElement | ||
| import kotlin.coroutines.CoroutineContext | ||
| import kotlinx.coroutines.ThreadContextElement | ||
|
|
||
| /** | ||
| * Sentry context element for [CoroutineContext]. | ||
| */ | ||
| class SentryContext : ThreadContextElement<IHub>, AbstractCoroutineContextElement(Key) { | ||
|
|
||
| companion object Key : CoroutineContext.Key<SentryContext> | ||
|
|
||
| private val hub: IHub = Sentry.getCurrentHub().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) | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package io.sentry.kotlin | ||
|
|
||
| import io.sentry.Sentry | ||
| import kotlin.test.AfterTest | ||
| import kotlin.test.BeforeTest | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNull | ||
| import kotlinx.coroutines.joinAll | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.runBlocking | ||
|
|
||
| class SentryContextTest { | ||
|
|
||
| @BeforeTest | ||
| fun init() { | ||
| Sentry.init("https://key@sentry.io/123") | ||
| } | ||
|
|
||
| @AfterTest | ||
| fun close() { | ||
| Sentry.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun testContextIsNotPassedByDefaultBetweenCoroutines() = runBlocking { | ||
| Sentry.setTag("parent", "parentValue") | ||
| val c1 = launch(SentryContext()) { | ||
| Sentry.setTag("c1", "c1value") | ||
| assertEquals("c1value", getTag("c1")) | ||
| assertEquals("parentValue", getTag("parent")) | ||
| assertNull(getTag("c2")) | ||
| } | ||
| val c2 = launch(SentryContext()) { | ||
| Sentry.setTag("c2", "c2value") | ||
| assertEquals("c2value", getTag("c2")) | ||
| assertEquals("parentValue", getTag("parent")) | ||
| assertNull(getTag("c1")) | ||
| } | ||
| listOf(c1, c2).joinAll() | ||
| assertNull(getTag("c1")) | ||
| assertNull(getTag("c2")) | ||
| } | ||
|
|
||
| @Test | ||
| fun testContextIsPassedToChildCoroutines() = runBlocking { | ||
| val c1 = launch(SentryContext()) { | ||
| Sentry.setTag("c1", "c1value") | ||
| assertEquals("c1value", getTag("c1")) | ||
| assertNull(getTag("c2")) | ||
| launch() { | ||
| Sentry.setTag("c2", "c2value") | ||
| assertEquals("c2value", getTag("c2")) | ||
| assertEquals("c1value", getTag("c1")) | ||
| }.join() | ||
| } | ||
| c1.join() | ||
| } | ||
|
|
||
| @Test | ||
| fun testContextPassedWhileOnMainThread() { | ||
| Sentry.setTag("myKey", "myValue") | ||
| runBlocking { | ||
| assertEquals("myValue", getTag("myKey")) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun testContextCanBePassedWhileOnMainThread() { | ||
| Sentry.setTag("myKey", "myValue") | ||
| runBlocking(SentryContext()) { | ||
| assertEquals("myValue", getTag("myKey")) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun testContextMayBeEmpty() { | ||
| runBlocking(SentryContext()) { | ||
| assertNull(getTag("myKey")) | ||
| } | ||
| } | ||
|
|
||
| private fun getTag(tag: String): String? { | ||
| var value: String? = null | ||
| Sentry.configureScope { | ||
| value = it.tags[tag] | ||
| } | ||
| return value | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.