Skip to content

Conversation

43jay
Copy link
Collaborator

@43jay 43jay commented Oct 9, 2025

NB - NOT READY TO LAND - Request For Feedback!!

📜 Description

Introduce data classes (pkg=io.sentry.util.network), following format in the javascript SDK.

Inject this new data holder object (NetworkRequestData) into the breadcrumb Hint (only for sentry-okhttp for now).

Set up DefaultReplayBreadcrumbConverter as the default BeforeBreadcrumbCallback.
^Anytime replayintegration is enabled, DefaultReplayBreadcrumbConverter will be the BeforeBreadcrumbCallback, and responsible for delegating to any user-defined callback.

Extract any breadcrumb Hint NetworkRequestData into the replay performanceSpan when creating the replay segment.

TODOs

Feedbacks:

  • Not actually seeing the network data in replay dashboard
    • => It should be here,
    • Based on performanceSpan here
  • Align on approach of introducing new Hint datatype and overhead of injecting more data into Hint
  • Align on where/what package to put new classes.

Implementation:

  • Respect networkDetailAllowUrls SDK Options flag
  • handle case where SentryOkHttpEventListener handles http request instrumentation
  • Clean-up breadcrumbsMap entries after data has been added to replay
  • Decide on whether to revert changes to sentry-samples-android

CLEANUP:

  • Code messy to get it working/proof-of concept (readability)
  • Lots of debug statements + hacky code.
  • Not using kotlin idioms (readability)

💡 Motivation and Context

Part of [Mobile Replay] Capture Network request/response bodies

Initially, we were trying to keep SDK changes simple and re-use the existing OKHTTP_REQUEST|RESPONSE hint data.
However, the :sentry-android-replay gradle module doesn't compile against any of the http libs (makes sense).

Because these okhttp3.Request, etc types don't exist in :sentry-android-replay, I switched to this PR where http modules (e.g. :sentry-okhttp) will set the http breadcrumb hints using newly introduced API (under the :sentry build target) which is also available to :sentry-android-replay

💚 How did you test it?

See recording segment data of entire replay payload

See snippet corresponding to http performanceSpans (network request data)
{
      "type": 5,
      "timestamp": 1760123982778,
      "data": {
        "tag": "performanceSpan",
        "payload": {
          "op": "resource.http",
          "description": "https://api.github.com/users/getsentry",
          "startTimestamp": 1760123982.346,
          "endTimestamp": 1760123982.78,
          "data": {
            "request": {
              "size": 94,
              "body": {
                "contentType": "application/json; charset=utf-8",
                "hasBody": true
              },
              "headers": {
                "Accept": "application/json",
                "baggage": "sentry-environment=debug,sentry-public_key=1053864c67cc410aa1ffc9701bd6f93d,sentry-release=io.sentry.samples.android%408.23.0%2B2,sentry-replay_id=65181e834e9b4174a54f3f62a1ba8203,sentry-sample_rand=0.771587471444489,sentry-sample_rate=1,sentry-sampled=true,sentry-trace_id=63be7ce015154c02bc8590e7de39e274,sentry-transaction=TriggerHttpRequestActivity",
                "Content-Type": "application/json",
                "sentry-trace": "63be7ce015154c02bc8590e7de39e274-90d64b1b2750442d-1",
                "User-Agent": "Sentry-Sample-Android"
              }
            },
            "method": "POST",
            "response": {
              "size": 106,
              "body": {
                "contentType": "application/json; charset=utf-8",
                "hasBody": true
              },
              "headers": {
                "access-control-allow-origin": "*",
                "access-control-expose-headers": "[Filtered]",
                "content-length": "106",
                "content-security-policy": "default-src 'none'",
                "content-type": "application/json; charset=utf-8",
                "date": "Fri, 10 Oct 2025 19:19:40 GMT",
                "referrer-policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
                "server": "github.com",
                "strict-transport-security": "max-age=31536000; includeSubdomains; preload",
                "vary": "Accept-Encoding, Accept, X-Requested-With",
                "x-content-type-options": "nosniff",
                "x-frame-options": "deny",
                "x-github-media-type": "github.v3; format=json",
                "x-github-request-id": "AE3A:362FA8:2B591B:BD7083:68E95C4C",
                "x-ratelimit-limit": "60",
                "x-ratelimit-remaining": "56",
                "x-ratelimit-reset": "1760127407",
                "x-ratelimit-resource": "core",
                "x-ratelimit-used": "4",
                "x-xss-protection": "0"
              }
            },
            "responseBodySize": 106,
            "statusCode": 404,
            "requestBodySize": 94
          }
        }
      }
    }

Copy link

linear bot commented Oct 9, 2025

@43jay 43jay marked this pull request as draft October 9, 2025 21:28
scopes.options.logger.log(
io.sentry.SentryLevel.INFO,
"SentryNetwork: Request - Headers count: ${requestHeaders.size}, Body size: $reqBodySize, Body info: $reqBodyInfo"
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Logging Overload and Network Data Loss

Debug logging statements (Android Log.d, println, Sentry logger INFO level) were committed, causing excessive log output. Separately, the DefaultReplayBreadcrumbConverter loses network data for Replay when a user's BeforeBreadcrumbCallback returns a new Breadcrumb instance, as the httpBreadcrumbData map lookup fails due to a key mismatch.

Additional Locations (5)

Fix in Cursor Fix in Web

}

private var lastConnectivityState: String? = null
private val httpBreadcrumbData = mutableMapOf<Breadcrumb, NetworkRequestData>()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Memory Leak in Breadcrumb Data Storage

The httpBreadcrumbData map stores Breadcrumb objects as keys but lacks a cleanup mechanism. This causes breadcrumbs to accumulate indefinitely, leading to a memory leak.

Fix in Cursor Fix in Web

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if you want to use a ring buffer here, but if you do we already have an implementation in the SDK available: https://github.com/getsentry/sentry-java/blob/main/sentry/src/main/java/io/sentry/CircularFifoQueue.java

}

private var lastConnectivityState: String? = null
private val httpBreadcrumbData = mutableMapOf<Breadcrumb, NetworkRequestData>()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential bug: The singleton's httpBreadcrumbData map is not thread-safe and grows indefinitely, leading to concurrency crashes and out-of-memory errors.
  • Description: The DefaultReplayBreadcrumbConverter is a singleton that uses a non-thread-safe mutableMapOf named httpBreadcrumbData. This leads to two issues. First, because OkHttp interceptors can run on concurrent threads, simultaneous writes to the map can cause ConcurrentModificationException crashes. Second, entries are added to the map for every HTTP request but are never removed. This causes unbounded memory growth in the long-lived singleton, eventually leading to an OutOfMemoryError.

  • Suggested fix: Replace mutableMapOf with a thread-safe collection like ConcurrentHashMap. Also, ensure entries are removed from the map after the corresponding breadcrumb data has been processed for the replay segment to prevent unbounded memory growth.
    severity: 0.9, confidence: 0.98

Did we get this right? 👍 / 👎 to inform future reviews.

Comment on lines +404 to +406
options.setBeforeBreadcrumb(
replayBreadcrumbConverter
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential bug: A user-configured BeforeBreadcrumbCallback overwrites the SDK's replay converter during initialization, silently disabling network capture for replays.
  • Description: During SentryAndroid.init(), the DefaultReplayBreadcrumbConverter is set as the BeforeBreadcrumbCallback before the user's configuration callback is executed. If a user provides their own BeforeBreadcrumbCallback in the configuration, it replaces the SDK's converter. This breaks the replay feature's ability to capture network request data, as the logic in DefaultReplayBreadcrumbConverter is never called. The feature fails silently for any user following the common practice of setting a breadcrumb callback.

  • Suggested fix: Modify the initialization logic to chain the callbacks instead of overwriting. The DefaultReplayBreadcrumbConverter should be initialized with the user's callback, which is retrieved after the user's configuration has run. The converter would then execute the user's callback before its own logic.
    severity: 0.7, confidence: 0.99

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is legit, so we probably have to move setting the beforeBreadcrumb callback over to initializeIntegrationsAndProcessors which is called after the user config. You can access the converter via options.getReplayController().getBreadcrumbConverter later on

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one 🙈 thanks

import io.sentry.util.SpanUtils
import io.sentry.util.TracingUtils
import io.sentry.util.UrlUtils
import io.sentry.util.network.NetworkRequestData
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are missing from the PR, but I assume they are just data holders so nothing fancy there :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yikes. adding now - and yes i just copied the naming + types from JS
NetworkRequestData
ReplayNetworkRequestOrResponseData

val networkData = createNetworkRequestData(request, response, requestBodySize, responseBodySize)
it.set("replay:networkDetails", networkData)

// it.set(OKHTTP_REQUEST, request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably still keep these because some of our customer may rely on them being present in the hint


/**
* Extracts body metadata from OkHttp RequestBody or ResponseBody
* Note: We don't consume the actual body stream to avoid interfering with the request/response
Copy link
Member

@romtsn romtsn Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, is it not possible to consume/copy it? I think would be great if we could do that, given that the JS sdk does that too. Otherwise, it's probably not very helpful to just have the headers and some metadata

// First try to get the structured network data from the hint
val networkDetails = breadcrumbHint.get("replay:networkDetails") as? NetworkRequestData
if (networkDetails != null) {
Log.d("SentryNetwork", "SentryNetwork: Found structured NetworkRequestData in hint: $networkDetails")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you're gonna clean these up, but if you wanna keep some logs please use options.logger as it will no-op in production builds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants