Skip to content

Commit 2e9b796

Browse files
committed
Extract network details when using SentryOkHttpEventListener
Reuse existing logic that retrieves optional SentryOkHttpEvent for the okhttp3.Call, and optionally provide NetworkRequestData for adding to Breadcrumb Hint in SentryOkHttpEvent#finish
1 parent 0aac31b commit 2e9b796

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.sentry.TypeCheckHint
1010
import io.sentry.transport.CurrentDateProvider
1111
import io.sentry.util.Platform
1212
import io.sentry.util.UrlUtils
13+
import io.sentry.util.network.NetworkRequestData
1314
import java.util.concurrent.ConcurrentHashMap
1415
import java.util.concurrent.TimeUnit
1516
import java.util.concurrent.atomic.AtomicBoolean
@@ -27,6 +28,7 @@ internal class SentryOkHttpEvent(private val scopes: IScopes, private val reques
2728
internal val callSpan: ISpan?
2829
private var response: Response? = null
2930
private var clientErrorResponse: Response? = null
31+
private var networkDetails: NetworkRequestData? = null
3032
internal val isEventFinished = AtomicBoolean(false)
3133
private var url: String
3234
private var method: String
@@ -135,6 +137,11 @@ internal class SentryOkHttpEvent(private val scopes: IScopes, private val reques
135137
}
136138
}
137139

140+
/** Sets the [NetworkRequestData] for network detail capture. */
141+
fun setNetworkDetails(networkRequestData: NetworkRequestData?) {
142+
this.networkDetails = networkRequestData
143+
}
144+
138145
/** Record event start if the callRootSpan is not null. */
139146
fun onEventStart(event: String) {
140147
callSpan ?: return
@@ -163,6 +170,9 @@ internal class SentryOkHttpEvent(private val scopes: IScopes, private val reques
163170
hint.set(TypeCheckHint.OKHTTP_REQUEST, request)
164171
response?.let { hint.set(TypeCheckHint.OKHTTP_RESPONSE, it) }
165172

173+
// Include network details in the hint for session replay
174+
networkDetails?.let { hint.set(TypeCheckHint.SENTRY_REPLAY_NETWORK_DETAILS, it) }
175+
166176
// needs this as unix timestamp for rrweb
167177
breadcrumb.setData(
168178
SpanDataConvention.HTTP_END_TIMESTAMP,

sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ public open class SentryOkHttpInterceptor(
209209
)
210210
}
211211

212+
// Set network details on the OkHttpEvent so it can include them in the breadcrumb hint
213+
okHttpEvent?.setNetworkDetails(networkDetailData)
214+
212215
finishSpan(span, request, response, isFromEventListener, okHttpEvent)
213216

214217
// The SentryOkHttpEventListener will send the breadcrumb itself if used for this call

sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/TriggerHttpRequestActivity.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.widget.Toast;
1111
import androidx.appcompat.app.AppCompatActivity;
1212
import io.sentry.Sentry;
13+
import io.sentry.okhttp.SentryOkHttpEventListener;
1314
import io.sentry.okhttp.SentryOkHttpInterceptor;
1415
import java.io.ByteArrayInputStream;
1516
import java.io.IOException;
@@ -80,14 +81,14 @@ private void initializeViews() {
8081

8182
private void setupOkHttpClient() {
8283
// OkHttpClient with Sentry integration for monitoring HTTP requests
84+
// Both SentryOkHttpEventListener and SentryOkHttpInterceptor are enabled to test
85+
// network detail capture when both components are used together
8386
okHttpClient =
8487
new OkHttpClient.Builder()
8588
.connectTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
8689
.readTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
8790
.writeTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
88-
// performance monitoring
89-
// .eventListener(new SentryOkHttpEventListener())
90-
// breadcrumbs and failed request capture
91+
.eventListener(new SentryOkHttpEventListener())
9192
.addInterceptor(new SentryOkHttpInterceptor())
9293
.build();
9394
}

0 commit comments

Comments
 (0)