Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Session Replay: Improve network body parsing and truncation handling ([#4958](https://github.com/getsentry/sentry-java/pull/4958))

### Internal

- Support `metric` envelope item type ([#4956](https://github.com/getsentry/sentry-java/pull/4956))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ public open class DefaultReplayBreadcrumbConverter() : ReplayBreadcrumbConverter
networkData.request?.let { request ->
val requestData = mutableMapOf<String, Any?>()
request.size?.let { requestData["size"] = it }
request.body?.let { requestData["body"] = it.value }
request.body?.let {
requestData["body"] = it.body
it.warnings?.let { warnings ->
requestData["warnings"] = warnings.map { warning -> warning.value }
}
}

if (request.headers.isNotEmpty()) {
requestData["headers"] = request.headers
Expand All @@ -255,7 +260,12 @@ public open class DefaultReplayBreadcrumbConverter() : ReplayBreadcrumbConverter
networkData.response?.let { response ->
val responseData = mutableMapOf<String, Any?>()
response.size?.let { responseData["size"] = it }
response.body?.let { responseData["body"] = it.value }
response.body?.let {
responseData["body"] = it.body
it.warnings?.let { warnings ->
responseData["warnings"] = warnings.map { warning -> warning.value }
}
}

if (response.headers.isNotEmpty()) {
responseData["headers"] = response.headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,15 @@ class DefaultReplayBreadcrumbConverterTest {
fakeOkHttpNetworkDetails.setRequestDetails(
ReplayNetworkRequestOrResponse(
100L,
NetworkBody.fromString("request body content"),
NetworkBody("request body content"),
mapOf("Content-Type" to "application/json"),
)
)
fakeOkHttpNetworkDetails.setResponseDetails(
200,
ReplayNetworkRequestOrResponse(
500L,
NetworkBody.fromJsonObject(mapOf("status" to "success", "message" to "OK")),
NetworkBody(mapOf("status" to "success", "message" to "OK")),
mapOf("Content-Type" to "text/plain"),
),
)
Expand Down Expand Up @@ -485,15 +485,15 @@ class DefaultReplayBreadcrumbConverterTest {
fakeOkHttpNetworkDetails.setRequestDetails(
ReplayNetworkRequestOrResponse(
150L,
NetworkBody.fromJsonArray(listOf("item1", "item2", "item3")),
NetworkBody(listOf("item1", "item2", "item3")),
mapOf("Content-Type" to "application/json"),
)
)
fakeOkHttpNetworkDetails.setResponseDetails(
404,
ReplayNetworkRequestOrResponse(
550L,
NetworkBody.fromJsonObject(mapOf("status" to "success", "message" to "OK")),
NetworkBody(mapOf("status" to "success", "message" to "OK")),
mapOf("Content-Type" to "text/plain"),
),
)
Expand Down Expand Up @@ -540,15 +540,15 @@ class DefaultReplayBreadcrumbConverterTest {
networkRequestData.setRequestDetails(
ReplayNetworkRequestOrResponse(
100L,
NetworkBody.fromString("request body content"),
NetworkBody("request body content"),
mapOf("Content-Type" to "application/json"),
)
)
networkRequestData.setResponseDetails(
200,
ReplayNetworkRequestOrResponse(
100L,
NetworkBody.fromString("respnse body content"),
NetworkBody("response body content"),
mapOf("Content-Type" to "application/json"),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,10 @@ public open class SentryOkHttpInterceptor(
val contentTypeString = contentType?.toString()
val maxBodySize = SentryReplayOptions.MAX_NETWORK_BODY_SIZE

val contentLength = responseBody.contentLength()
if (contentLength > maxBodySize * 2) {
return NetworkBody.fromString("[Response body too large: $contentLength bytes]")
}

// Peek at the body (doesn't consume it)
val peekBody = peekBody(maxBodySize.toLong())
// We +1 here in order to properly truncate within NetworkBodyParser.fromBytes
// and be able to distinguish from an oversized request and a request matching maxBodySize
val peekBody = peekBody(maxBodySize.toLong() + 1)
val bodyBytes = peekBody.bytes()

val charset = contentType?.charset(Charsets.UTF_8)?.name() ?: "UTF-8"
Expand Down
31 changes: 12 additions & 19 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -7481,29 +7481,22 @@ public final class io/sentry/util/UrlUtils$UrlDetails {
public fun getUrlOrFallback ()Ljava/lang/String;
}

public abstract interface class io/sentry/util/network/NetworkBody {
public static fun fromJsonArray (Ljava/util/List;)Lio/sentry/util/network/NetworkBody;
public static fun fromJsonObject (Ljava/util/Map;)Lio/sentry/util/network/NetworkBody;
public static fun fromString (Ljava/lang/String;)Lio/sentry/util/network/NetworkBody;
public abstract fun getValue ()Ljava/lang/Object;
}

public final class io/sentry/util/network/NetworkBody$JsonArrayImpl : io/sentry/util/network/NetworkBody {
public synthetic fun getValue ()Ljava/lang/Object;
public fun getValue ()Ljava/util/List;
public fun toString ()Ljava/lang/String;
}

public final class io/sentry/util/network/NetworkBody$JsonObjectImpl : io/sentry/util/network/NetworkBody {
public synthetic fun getValue ()Ljava/lang/Object;
public fun getValue ()Ljava/util/Map;
public final class io/sentry/util/network/NetworkBody {
public fun <init> (Ljava/lang/Object;)V
public fun <init> (Ljava/lang/Object;Ljava/util/List;)V
public fun getBody ()Ljava/lang/Object;
public fun getWarnings ()Ljava/util/List;
public fun toString ()Ljava/lang/String;
}

public final class io/sentry/util/network/NetworkBody$StringBodyImpl : io/sentry/util/network/NetworkBody {
public synthetic fun getValue ()Ljava/lang/Object;
public final class io/sentry/util/network/NetworkBody$NetworkBodyWarning : java/lang/Enum {
public static final field BODY_PARSE_ERROR Lio/sentry/util/network/NetworkBody$NetworkBodyWarning;
public static final field INVALID_JSON Lio/sentry/util/network/NetworkBody$NetworkBodyWarning;
public static final field JSON_TRUNCATED Lio/sentry/util/network/NetworkBody$NetworkBodyWarning;
public static final field TEXT_TRUNCATED Lio/sentry/util/network/NetworkBody$NetworkBodyWarning;
public fun getValue ()Ljava/lang/String;
public fun toString ()Ljava/lang/String;
public static fun valueOf (Ljava/lang/String;)Lio/sentry/util/network/NetworkBody$NetworkBodyWarning;
public static fun values ()[Lio/sentry/util/network/NetworkBody$NetworkBodyWarning;
}

public final class io/sentry/util/network/NetworkBodyParser {
Expand Down
116 changes: 32 additions & 84 deletions sentry/src/main/java/io/sentry/util/network/NetworkBody.java
Original file line number Diff line number Diff line change
@@ -1,113 +1,61 @@
package io.sentry.util.network;

import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

/**
* Represents the body content of a network request or response. Can be one of: JSON object, JSON
* array, or string.
* Represents the body content of a network request or response.
*
* <p>See <a
* href="https://github.com/getsentry/sentry-javascript/blob/develop/packages/replay-internal/src/types/request.ts">Javascript
* types</a>
*/
public interface NetworkBody {
@ApiStatus.Internal
public final class NetworkBody {

/**
* Creates a NetworkBody from a JSON object.
*
* @param value The map representing the JSON object
* @return A NetworkBody instance for the JSON object
*/
static @NotNull NetworkBody fromJsonObject(@NotNull final Map<String, Object> value) {
return new JsonObjectImpl(value);
}
private final @Nullable Object body;
private final @Nullable List<NetworkBodyWarning> warnings;

/**
* Creates a NetworkBody from a JSON array.
*
* @param value The list representing the JSON array
* @return A NetworkBody instance for the JSON array
*/
static @NotNull NetworkBody fromJsonArray(@NotNull final List<Object> value) {
return new JsonArrayImpl(value);
public NetworkBody(final @Nullable Object body) {
this(body, null);
}

/**
* Creates a NetworkBody from string content.
*
* @param value The string content
* @return A NetworkBody instance for the string
*/
static @NotNull NetworkBody fromString(@NotNull final String value) {
return new StringBodyImpl(value);
public NetworkBody(
final @Nullable Object body, final @Nullable List<NetworkBodyWarning> warnings) {
this.body = body;
this.warnings = warnings;
}

/**
* Gets the underlying value of this NetworkBody.
*
* @return The value as an Object (could be Map, List, or String)
*/
@NotNull
Object getValue();

// Private implementation classes

/** Implementation for JSON object bodies */
final class JsonObjectImpl implements NetworkBody {
private final @NotNull Map<String, Object> value;

JsonObjectImpl(@NotNull final Map<String, Object> value) {
this.value = value;
}

@Override
public @NotNull Map<String, Object> getValue() {
return value;
}

@Override
public String toString() {
return "NetworkBody.JsonObject{" + value + '}';
}
public @Nullable Object getBody() {
return body;
}

/** Implementation for JSON array bodies */
final class JsonArrayImpl implements NetworkBody {
private final @NotNull List<Object> value;

JsonArrayImpl(@NotNull final List<Object> value) {
this.value = value;
}

@Override
public @NotNull List<Object> getValue() {
return value;
}

@Override
public String toString() {
return "NetworkBody.JsonArray{" + value + '}';
}
public @Nullable List<NetworkBodyWarning> getWarnings() {
return warnings;
}

/** Implementation for string bodies */
final class StringBodyImpl implements NetworkBody {
private final @NotNull String value;
// Based on
// https://github.com/getsentry/sentry/blob/ccb61aa9b0f33e1333830093a5ce3bd5db88ef33/static/app/utils/replays/replay.tsx#L5-L12
public enum NetworkBodyWarning {
Copy link
Member

Choose a reason for hiding this comment

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

so we can't exactly match all of the reasons on Android?

Copy link
Member Author

Choose a reason for hiding this comment

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

Some of them simply don't make sense on mobile, like BODY_PARSE_TIMEOUT. So I only added those which we actually use

JSON_TRUNCATED("JSON_TRUNCATED"),
TEXT_TRUNCATED("TEXT_TRUNCATED"),
INVALID_JSON("INVALID_JSON"),
BODY_PARSE_ERROR("BODY_PARSE_ERROR");

private final String value;

StringBodyImpl(@NotNull final String value) {
NetworkBodyWarning(String value) {
this.value = value;
}

@Override
public @NotNull String getValue() {
public String getValue() {
return value;
}
}

@Override
public String toString() {
return "NetworkBody.StringBody{" + value + '}';
}
@Override
public String toString() {
return "NetworkBody{" + "body=" + body + ", warnings=" + warnings + '}';
}
}
Loading
Loading