Skip to content

Commit 7074cdd

Browse files
committed
forward early cancel events and skip re-wrapping Sentry OkHttp listeners
1 parent 4e3cff3 commit 7074cdd

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Features
66

7-
- Add OkHttp autoconfiguration for Spring Boot 4 ([#5797](https://github.com/getsentry/sentry-java/pull/5797))
7+
- Add OkHttp autoconfiguration for Spring Boot ([#5797](https://github.com/getsentry/sentry-java/pull/5797))
88
- Add `LocalSentrySpan` to `sentry-compose` so apps can provide a parent `ISpan` to a composable subtree and have nested `SentryTraced` spans attach to it ([#6112]https://github.com/getsentry/sentry-java/pull/6112)
99
- Add `dataCollection`, a fine-grained replacement for `sendDefaultPii`, for controlling data collected automatically by SDK integrations ([#5759](https://github.com/getsentry/sentry-java/pull/5759))
1010
- `sendDefaultPii` remains supported for backwards compatibility. When `dataCollection` is not configured, the SDK preserves the existing `sendDefaultPii` behavior.

‎sentry-spring-boot-4/src/main/java/io/sentry/spring/boot4/SentryOkHttpClientBeanPostProcessor.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ private SentryEventListenerFactory(final @NotNull EventListener.Factory delegate
7878

7979
@Override
8080
public @NotNull EventListener create(final @NotNull Call call) {
81-
return new SentryOkHttpEventListener(ScopesAdapter.getInstance(), delegate);
81+
final @NotNull EventListener original = delegate.create(call);
82+
if (original instanceof SentryOkHttpEventListener) {
83+
return original;
84+
}
85+
return new SentryOkHttpEventListener(ScopesAdapter.getInstance(), original);
8286
}
8387
}
8488
}

‎sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryOkHttpAutoConfigurationTest.kt‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,39 @@ class SentryOkHttpAutoConfigurationTest {
118118
}
119119
}
120120

121+
@Test
122+
fun `forwards canceled before callStart to the original event listener`() {
123+
noOtelContextRunner
124+
.withPropertyValues("sentry.dsn=http://key@localhost/proj")
125+
.withUserConfiguration(OkHttpClientConfiguration::class.java)
126+
.run { context ->
127+
val client = context.getBean(OkHttpClient::class.java)
128+
129+
// OkHttp creates the listener in the Call constructor and reports canceled() even if
130+
// the call is never executed, i.e. before callStart() ever happens
131+
client.newCall(Request.Builder().url("https://example.com").build()).cancel()
132+
133+
assertThat(context.getBean(RecordingEventListener::class.java).callCanceled.get()).isTrue()
134+
}
135+
}
136+
137+
@Test
138+
fun `does not wrap a Sentry listener created by the original factory`() {
139+
val sentryListener = SentryOkHttpEventListener()
140+
val client = OkHttpClient.Builder().eventListenerFactory { sentryListener }.build()
141+
142+
noOtelContextRunner
143+
.withPropertyValues("sentry.dsn=http://key@localhost/proj")
144+
.withBean("okHttpClient", OkHttpClient::class.java, { client })
145+
.run { context ->
146+
val instrumented = context.getBean(OkHttpClient::class.java)
147+
148+
assertThat(instrumented.eventListenerFactory.create(mock()))
149+
.isSameInstanceAs(sentryListener)
150+
assertThat(instrumented.interceptors.filterIsInstance<SentryOkHttpInterceptor>()).hasSize(1)
151+
}
152+
}
153+
121154
@Test
122155
fun `does not duplicate an existing Sentry interceptor`() {
123156
noOtelContextRunner
@@ -285,9 +318,14 @@ class SentryOkHttpAutoConfigurationTest {
285318

286319
class RecordingEventListener : EventListener() {
287320
val callStarted = AtomicBoolean(false)
321+
val callCanceled = AtomicBoolean(false)
288322

289323
override fun callStart(call: Call) {
290324
callStarted.set(true)
291325
}
326+
327+
override fun canceled(call: Call) {
328+
callCanceled.set(true)
329+
}
292330
}
293331
}

0 commit comments

Comments
 (0)