10BC0 RFF(replay): Adding OkHttp Request/Response bodies for sentry-java by 43jay · Pull Request #4796 · getsentry/sentry-java · GitHub
[go: up one dir, main page]

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:

  • Actually see the replay data in dashboard => It should be here, but maybe there is some feature flag server side?
  • 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:

  • 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 gist of entire replay payload

See snippet corresponding to http performanceSpans (network request data)
Serializing object: {
	"segment_id": 4
}
[
	...
	{
		"type": 3,
		"timestamp": 1760041988900,
		"data": {
			"source": 2,
			"type": 9,
			"id": 0,
			"x": 95.52592468261719,
			"y": 259.65582275390625,
			"pointerType": 2,
			"pointerId": 0
		}
	},
	{
		"type": 5,
		"timestamp": 1760041989261,
		"data": {
			"tag": "performanceSpan",
			"payload": {
				"op": "resource.http",
				"description": "https://api.github.com/users/getsentry",
				"startTimestamp": 1760041988.909,
				"endTimestamp": 1760041989.262,
				"data": {
					"request": {
						"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=a54a50c783174c12bf92efccd53d5ab5,sentry-sample_rand=0.5273439858953587,sentry-sample_rate=1,sentry-sampled=true,sentry-trace_id=0e272865c4b84ce28e8351e936671822,sentry-transaction=TriggerHttpRequestActivity",
							"sentry-trace": "0e272865c4b84ce28e8351e936671822-7406a84640b546ea-1",
							"User-Agent": "Sentry-Sample-Android"
						}
					},
					"method": "GET",
					"response": {
						"body": {
							"contentType": "application/json; charset=utf-8",
							"hasBody": true
						},
						"headers": {
							"accept-ranges": "bytes",
							"access-control-allow-origin": "*",
							"access-control-expose-headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
							"cache-control": "public, max-age=60, s-maxage=60",
							"content-security-policy": "default-src 'none'",
							"content-type": "application/json; charset=utf-8",
							"date": "Thu, 09 Oct 2025 20:32:58 GMT",


							"etag": "W/\"9d9812fc3321956eca38b1c94bb05d89f19e816beea53c584d46f8819b45fc9b\"",
							"last-modified": "Fri, 18 Jul 2025 21:10:27 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,Accept-Encoding, Accept, X-Requested-With",
							"x-content-type-options": "nosniff",
							"x-frame-options": "deny",
							"x-github-api-version-selected": "2022-11-28",
							"x-github-media-type": "github.v3",
							"x-github-request-id": "BB54:3653:2F076E2:CDF7237:68E81C04",
							"x-ratelimit-limit": "60",
							"x-ratelimit-remaining": "57",
							"x-ratelimit-reset": "1760045577",
							"x-ratelimit-resource": "core",
							"x-ratelimit-used": "3",
							"x-xss-protection": "0"
						}
					},
					"statusCode": 200
				}
			}
		}
	},
	...
	{
		"type": 5,
		"timestamp": 1760041991319,
		"data": {
			"tag": "performanceSpan",
			"payload": {
				"op": "resource.http",
				"description": "https://api.github.com/users/getsentry",
				"startTimestamp": 1760041991.226,
				"endTimestamp": 1760041991.321,
				"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=a54a50c783174c12bf92efccd53d5ab5,sentry-sample_rand=0.5273439858953587,sentry-sample_rate=1,sentry-sampled=true,sentry-trace_id=0e272865c4b84ce28e8351e936671822,sentry-transaction=TriggerHttpRequestActivity",
							"Content-Type": "application/json",
							"sentry-trace": "0e272865c4b84ce28e8351e936671822-890b5f0ec10b479f-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": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
							"content-length": "106",
							"content-security-policy": "default-src 'none'",
							"content-type": "application/json; charset=utf-8",
							"date": "Thu, 09 Oct 2025 20:33:10 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": "BB54:3653:2F0817D:CDFA110:68E81C04",
							"x-ratelimit-limit": "60",
							"x-ratelimit-remaining": "56",
							"x-ratelimit-reset": "1760045577",
							"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

}

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.

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.

1 participant
0