Skip to content

Commit 998f52c

Browse files
committed
improve
1 parent d050c7b commit 998f52c

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core",
9898
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
9999
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktorClient" }
100100
ktor-client-java = { module = "io.ktor:ktor-client-java", version.ref = "ktorClient" }
101+
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktorClient" }
101102
log4j-api = { module = "org.apache.logging.log4j:log4j-api", version.ref = "log4j2" }
102103
log4j-core = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j2" }
103104
leakcanary = { module = "com.squareup.leakcanary:leakcanary-android", version = "2.14" }

sentry-ktor-client/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
compileOnly(libs.jetbrains.annotations)
2828
compileOnly(libs.nopen.annotations)
2929
compileOnly(libs.ktor.client.core)
30+
compileOnly(libs.ktor.client.okhttp)
3031
errorprone(libs.errorprone.core)
3132
errorprone(libs.nopen.checker)
3233
errorprone(libs.nullaway)
@@ -37,6 +38,8 @@ dependencies {
3738
testImplementation(libs.mockito.inline)
3839
testImplementation(libs.ktor.client.core)
3940
testImplementation(libs.ktor.client.java)
41+
testImplementation(libs.ktor.client.okhttp)
42+
testImplementation(projects.sentryOkhttp)
4043
testImplementation(libs.okhttp.mockwebserver)
4144
}
4245

sentry-ktor-client/src/main/java/io/sentry/ktorClient/SentryKtorClientPlugin.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sentry.ktorClient
22

33
import io.ktor.client.HttpClient
4+
import io.ktor.client.engine.okhttp.OkHttpConfig
45
import io.ktor.client.plugins.api.*
56
import io.ktor.client.plugins.api.ClientPlugin
67
import io.ktor.client.request.*
@@ -78,6 +79,17 @@ internal const val TRACE_ORIGIN = "auto.http.ktor-client"
7879
*/
7980
public val SentryKtorClientPlugin: ClientPlugin<SentryKtorClientPluginConfig> =
8081
createClientPlugin(SENTRY_KTOR_CLIENT_PLUGIN_KEY, ::SentryKtorClientPluginConfig) {
82+
// If the engine is OkHttp and the OkHttp client is configured to use the SentryOkHttpInterceptor, any functionality of this plugin is disabled
83+
// Without this check, all HTTP requests would be doubly instrumented
84+
if (client.engine.config is OkHttpConfig) {
85+
val config = client.engine.config as OkHttpConfig
86+
var interceptors = config.preconfigured?.interceptors ?: emptyList()
87+
config.config { interceptors += this@config.interceptors() }
88+
if (interceptors.any { it.toString().contains("SentryOkHttpInterceptor") }) {
89+
return@createClientPlugin
90+
}
91+
}
92+
8193
// Init
8294
SentryIntegrationPackageStorage.getInstance()
8395
.addPackage("maven:io.sentry:sentry-ktor-client", BuildConfig.VERSION_NAME)

sentry-ktor-client/src/test/java/io/sentry/ktorClient/SentryKtorClientPluginTest.kt

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package io.sentry.ktorClient
22

33
import io.ktor.client.HttpClient
4+
import io.ktor.client.HttpClientConfig
5+
import io.ktor.client.engine.HttpClientEngine
6+
import io.ktor.client.engine.java.Java
7+
import io.ktor.client.engine.okhttp.OkHttp
8+
import io.ktor.client.engine.okhttp.OkHttpConfig
9+
import io.ktor.client.engine.okhttp.OkHttpEngine
10+
import io.ktor.client.plugins.plugin
411
import io.ktor.client.request.get
512
import io.ktor.client.request.post
613
import io.ktor.client.request.setBody
@@ -24,8 +31,10 @@ import io.sentry.SpanStatus
2431
import io.sentry.TransactionContext
2532
import io.sentry.exception.SentryHttpClientException
2633
import io.sentry.mockServerRequestTimeoutMillis
34+
import io.sentry.okhttp.SentryOkHttpInterceptor
2735
import java.util.concurrent.TimeUnit
2836
import kotlin.test.Test
37+
import okhttp3.OkHttpClient
2938
import kotlin.test.assertEquals
3039
import kotlin.test.assertNotNull
3140
import kotlin.test.assertNull
@@ -68,6 +77,7 @@ class SentryKtorClientPluginTest {
6877
),
6978
sendDefaultPii: Boolean = false,
7079
optionsConfiguration: Sentry.OptionsConfiguration<SentryOptions>? = null,
80+
httpClientEngine: HttpClientEngine = Java.create(),
7181
): HttpClient {
7282
options =
7383
SentryOptions().also {
@@ -102,7 +112,7 @@ class SentryKtorClientPluginTest {
102112
.setResponseCode(httpStatusCode)
103113
)
104114

105-
return HttpClient {
115+
return HttpClient(httpClientEngine) {
106116
install(SentryKtorClientPlugin) {
107117
this.scopes = this@Fixture.scopes
108118
this.captureFailedRequests = captureFailedRequests
@@ -364,9 +374,12 @@ class SentryKtorClientPluginTest {
364374
@Test
365375
fun `does not add sentry-trace header when span origin is ignored`(): Unit = runBlocking {
366376
val sut =
367-
fixture.getSut(isSpanActive = false) { options ->
368-
options.setIgnoredSpanOrigins(listOf("auto.http.ktor-client"))
369-
}
377+
fixture.getSut(
378+
isSpanActive = false,
379+
optionsConfiguration = { options ->
380+
options.setIgnoredSpanOrigins(listOf("auto.http.ktor-client"))
381+
}
382+
)
370383
sut.get(fixture.server.url("/hello").toString())
371384

372385
val recordedRequest =
@@ -419,4 +432,23 @@ class SentryKtorClientPluginTest {
419432
assertTrue(baggageHeaderValues[0].contains("sentry-transaction=name"))
420433
assertTrue(baggageHeaderValues[0].contains("sentry-trace_id"))
421434
}
435+
436+
@Test
437+
fun `is disabled when using OkHttp client with Sentry interceptor added to builder`() {
438+
val okHttpClient = OkHttpClient.Builder()
439+
.addInterceptor(SentryOkHttpInterceptor())
440+
.build()
441+
val engine = OkHttpEngine(OkHttpConfig().apply {
442+
preconfigured = okHttpClient
443+
})
444+
445+
val client = fixture.getSut(httpClientEngine = engine)
446+
val plugin = client.plugin(SentryKtorClientPlugin)
447+
}
448+
449+
@Test
450+
fun `is disabled when using preconfigured OkHttp client with Sentry interceptor`() {
451+
val engine = OkHttpEngine(OkHttpConfig())
452+
val client = fixture.getSut(httpClientEngine = engine)
453+
}
422454
}

0 commit comments

Comments
 (0)