1
1
package io.sentry.ktorClient
2
2
3
3
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
4
11
import io.ktor.client.request.get
5
12
import io.ktor.client.request.post
6
13
import io.ktor.client.request.setBody
@@ -24,8 +31,10 @@ import io.sentry.SpanStatus
24
31
import io.sentry.TransactionContext
25
32
import io.sentry.exception.SentryHttpClientException
26
33
import io.sentry.mockServerRequestTimeoutMillis
34
+ import io.sentry.okhttp.SentryOkHttpInterceptor
27
35
import java.util.concurrent.TimeUnit
28
36
import kotlin.test.Test
37
+ import okhttp3.OkHttpClient
29
38
import kotlin.test.assertEquals
30
39
import kotlin.test.assertNotNull
31
40
import kotlin.test.assertNull
@@ -68,6 +77,7 @@ class SentryKtorClientPluginTest {
68
77
),
69
78
sendDefaultPii : Boolean = false,
70
79
optionsConfiguration : Sentry .OptionsConfiguration <SentryOptions >? = null,
80
+ httpClientEngine : HttpClientEngine = Java .create(),
71
81
): HttpClient {
72
82
options =
73
83
SentryOptions ().also {
@@ -102,7 +112,7 @@ class SentryKtorClientPluginTest {
102
112
.setResponseCode(httpStatusCode)
103
113
)
104
114
105
- return HttpClient {
115
+ return HttpClient (httpClientEngine) {
106
116
install(SentryKtorClientPlugin ) {
107
117
this .scopes = this @Fixture.scopes
108
118
this .captureFailedRequests = captureFailedRequests
@@ -364,9 +374,12 @@ class SentryKtorClientPluginTest {
364
374
@Test
365
375
fun `does not add sentry-trace header when span origin is ignored` (): Unit = runBlocking {
366
376
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
+ )
370
383
sut.get(fixture.server.url(" /hello" ).toString())
371
384
372
385
val recordedRequest =
@@ -419,4 +432,23 @@ class SentryKtorClientPluginTest {
419
432
assertTrue(baggageHeaderValues[0 ].contains(" sentry-transaction=name" ))
420
433
assertTrue(baggageHeaderValues[0 ].contains(" sentry-trace_id" ))
421
434
}
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
+ }
422
454
}
0 commit comments