Skip to content

Commit 47c6829

Browse files
committed
Remove defensive copy when returning request|responseHeaders
getNetworkRequestHeaders / getNetworkResponseHeaders are called on every http request => move the memory operation to the setNetworkRequest|ResponseHeaders path which is called 1x on start-up
1 parent ad09f10 commit 47c6829

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ static void applyMetadata(
555555
options.getSessionReplay().isNetworkCaptureBodies() /* defaultValue */));
556556

557557
if (options.getSessionReplay().getNetworkRequestHeaders().length
558-
== SentryReplayOptions.getNetworkDetailsDefaultHeaders().length) { // Only has defaults
558+
== SentryReplayOptions.getNetworkDetailsDefaultHeaders().size()) { // Only has defaults
559559
final @Nullable List<String> requestHeaders =
560560
readList(metadata, logger, REPLAYS_NETWORK_REQUEST_HEADERS);
561561
if (requestHeaders != null) {
@@ -573,7 +573,7 @@ static void applyMetadata(
573573
}
574574

575575
if (options.getSessionReplay().getNetworkResponseHeaders().length
576-
== SentryReplayOptions.getNetworkDetailsDefaultHeaders().length) { // Only has defaults
576+
== SentryReplayOptions.getNetworkDetailsDefaultHeaders().size()) { // Only has defaults
577577
final @Nullable List<String> responseHeaders =
578578
readList(metadata, logger, REPLAYS_NETWORK_RESPONSE_HEADERS);
579579
if (responseHeaders != null && !responseHeaders.isEmpty()) {

sentry/api/sentry.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3794,7 +3794,7 @@ public final class io/sentry/SentryReplayOptions {
37943794
public fun getMaskViewContainerClass ()Ljava/lang/String;
37953795
public fun getNetworkDetailAllowUrls ()[Ljava/lang/String;
37963796
public fun getNetworkDetailDenyUrls ()[Ljava/lang/String;
3797-
public static fun getNetworkDetailsDefaultHeaders ()[Ljava/lang/String;
3797+
public static fun getNetworkDetailsDefaultHeaders ()Ljava/util/List;
37983798
public fun getNetworkRequestHeaders ()[Ljava/lang/String;
37993799
public fun getNetworkResponseHeaders ()[Ljava/lang/String;
38003800
public fun getOnErrorSampleRate ()Ljava/lang/Double;

sentry/src/main/java/io/sentry/SentryReplayOptions.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import io.sentry.protocol.SdkVersion;
44
import io.sentry.util.SampleRateUtils;
5-
65
import java.util.Arrays;
6+
import java.util.Collections;
77
import java.util.LinkedHashSet;
88
import java.util.List;
99
import java.util.Locale;
@@ -181,30 +181,30 @@ public enum SentryReplayQuality {
181181
private boolean networkCaptureBodies = true;
182182

183183
/** Default headers that are always captured for URLs defined in networkDetailAllowUrls. */
184-
private static final @NotNull String[] DEFAULT_HEADERS =
185-
new String[] {"Content-Type", "Content-Length", "Accept"};
184+
private static final @NotNull List<String> DEFAULT_HEADERS =
185+
Collections.unmodifiableList(Arrays.asList("Content-Type", "Content-Length", "Accept"));
186186

187187
/**
188188
* Gets the default headers that are always captured for URLs defined in networkDetailAllowUrls.
189-
* Returns a defensive copy to prevent modification.
189+
*
190+
* @return an unmodifiable list
190191
*/
191192
@ApiStatus.Internal
192-
public static @NotNull String[] getNetworkDetailsDefaultHeaders() {
193-
return DEFAULT_HEADERS.clone();
193+
public static @NotNull List<String> getNetworkDetailsDefaultHeaders() {
194+
return DEFAULT_HEADERS;
194195
}
195196

196197
/**
197198
* Additional request headers to capture for URLs defined in networkDetailAllowUrls. The default
198199
* headers (Content-Type, Content-Length, Accept) are always included in addition to these.
199200
*/
200-
private @NotNull String[] networkRequestHeaders = DEFAULT_HEADERS.clone();
201+
private @NotNull String[] networkRequestHeaders = DEFAULT_HEADERS.toArray(new String[0]);
201202

202203
/**
203204
* Additional response headers to capture for URLs defined in networkDetailAllowUrls. The default
204205
* headers (Content-Type, Content-Length, Accept) are always included in addition to these.
205206
*/
206-
private @NotNull String[] networkResponseHeaders = DEFAULT_HEADERS.clone();
207-
207+
private @NotNull String[] networkResponseHeaders = DEFAULT_HEADERS.toArray(new String[0]);
208208

209209
public SentryReplayOptions(final boolean empty, final @Nullable SdkVersion sdkVersion) {
210210
if (!empty) {
@@ -493,8 +493,8 @@ public void setNetworkCaptureBodies(final boolean networkCaptureBodies) {
493493
}
494494

495495
/**
496-
* Sets request headers to capture for URLs defined in networkDetailAllowUrls. The
497-
* default headers (Content-Type, Content-Length, Accept) are always included automatically.
496+
* Sets request headers to capture for URLs defined in networkDetailAllowUrls. The default headers
497+
* (Content-Type, Content-Length, Accept) are always included automatically.
498498
*
499499
* @param networkRequestHeaders additional network request headers list
500500
*/
@@ -513,8 +513,8 @@ public void setNetworkRequestHeaders(final @NotNull List<String> networkRequestH
513513
}
514514

515515
/**
516-
* Sets response headers to capture for URLs defined in networkDetailAllowUrls. The
517-
* default headers (Content-Type, Content-Length, Accept) are always included automatically.
516+
* Sets response headers to capture for URLs defined in networkDetailAllowUrls. The default
517+
* headers (Content-Type, Content-Length, Accept) are always included automatically.
518518
*
519519
* @param networkResponseHeaders the additional network response headers list
520520
*/
@@ -529,9 +529,9 @@ public void setNetworkResponseHeaders(final @NotNull List<String> networkRespons
529529
* @param additionalHeaders additional headers to merge
530530
*/
531531
private static @NotNull String[] mergeHeaders(
532-
final @NotNull String[] defaultHeaders, final @NotNull List<String> additionalHeaders) {
532+
final @NotNull List<String> defaultHeaders, final @NotNull List<String> additionalHeaders) {
533533
final Set<String> merged = new LinkedHashSet<>();
534-
merged.addAll(Arrays.asList(defaultHeaders));
534+
merged.addAll(defaultHeaders);
535535
merged.addAll(additionalHeaders);
536536
return merged.toArray(new String[0]);
537537
}

0 commit comments

Comments
 (0)