Skip to content

Commit e5882fa

Browse files
authored
Merge 79e8ce1 into b5b093e
2 parents b5b093e + 79e8ce1 commit e5882fa

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

sentry/src/main/java/io/sentry/ExperimentalOptions.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
* <p>Beware that experimental options can change at any time.
1010
*/
1111
public final class ExperimentalOptions {
12-
private @NotNull SentryReplayOptions sessionReplay = new SentryReplayOptions();
12+
private @NotNull SentryReplayOptions sessionReplay;
13+
14+
public ExperimentalOptions(final boolean empty) {
15+
this.sessionReplay = new SentryReplayOptions(empty);
16+
}
1317

1418
@NotNull
1519
public SentryReplayOptions getSessionReplay() {

sentry/src/main/java/io/sentry/SentryOptions.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
import io.sentry.transport.ITransportGate;
2020
import io.sentry.transport.NoOpEnvelopeCache;
2121
import io.sentry.transport.NoOpTransportGate;
22+
import io.sentry.util.Objects;
2223
import io.sentry.util.Platform;
2324
import io.sentry.util.SampleRateUtils;
2425
import io.sentry.util.StringUtils;
2526
import io.sentry.util.thread.IMainThreadChecker;
2627
import io.sentry.util.thread.NoOpMainThreadChecker;
2728
import java.io.File;
28-
import java.net.Proxy;
2929
import java.util.ArrayList;
3030
import java.util.Collections;
3131
import java.util.HashMap;
@@ -119,10 +119,14 @@ public class SentryOptions {
119119
private @NotNull SentryLevel diagnosticLevel = DEFAULT_DIAGNOSTIC_LEVEL;
120120

121121
/** Envelope reader interface */
122-
private @NotNull IEnvelopeReader envelopeReader = new EnvelopeReader(new JsonSerializer(this));
122+
private volatile @Nullable IEnvelopeReader envelopeReader;
123+
124+
private final @NotNull Object envelopeReaderLock = new Object();
123125

124126
/** Serializer interface to serialize/deserialize json events */
125-
private @NotNull ISerializer serializer = new JsonSerializer(this);
127+
private volatile @Nullable ISerializer serializer;
128+
129+
private final @NotNull Object serializerLock = new Object();
126130

127131
/** Max depth when serializing object graphs with reflection. * */
128132
private int maxDepth = 100;
@@ -416,7 +420,9 @@ public class SentryOptions {
416420

417421
/** Date provider to retrieve the current date from. */
418422
@ApiStatus.Internal
419-
private @NotNull SentryDateProvider dateProvider = new SentryAutoDateProvider();
423+
private volatile @Nullable SentryDateProvider dateProvider;
424+
425+
private final @NotNull Object dateProviderLock = new Object();
420426

421427
private final @NotNull List<IPerformanceCollector> performanceCollectors = new ArrayList<>();
422428

@@ -479,7 +485,7 @@ public class SentryOptions {
479485

480486
@ApiStatus.Experimental private @Nullable Cron cron = null;
481487

482-
private final @NotNull ExperimentalOptions experimental = new ExperimentalOptions();
488+
private final @NotNull ExperimentalOptions experimental;
483489

484490
private @NotNull ReplayController replayController = NoOpReplayController.getInstance();
485491

@@ -605,7 +611,14 @@ public void setDiagnosticLevel(@Nullable final SentryLevel diagnosticLevel) {
605611
* @return the serializer
606612
*/
607613
public @NotNull ISerializer getSerializer() {
608-
return serializer;
614+
if (serializer == null) {
615+
synchronized (serializerLock) {
616+
if (serializer == null) {
617+
serializer = new JsonSerializer(this);
618+
}
619+
}
620+
}
621+
return Objects.requireNonNull(serializer, "Serializer was null");
609622
}
610623

611624
/**
@@ -636,7 +649,14 @@ public void setMaxDepth(int maxDepth) {
636649
}
637650

638651
public @NotNull IEnvelopeReader getEnvelopeReader() {
639-
return envelopeReader;
652+
if (envelopeReader == null) {
653+
synchronized (envelopeReaderLock) {
654+
if (envelopeReader == null) {
655+
envelopeReader = new EnvelopeReader(getSerializer());
656+
}
657+
}
658+
}
659+
return Objects.requireNonNull(envelopeReader, "EnvelopeReader was null");
640660
}
641661

642662
public void setEnvelopeReader(final @Nullable IEnvelopeReader envelopeReader) {
@@ -2212,7 +2232,14 @@ public void setIgnoredCheckIns(final @Nullable List<String> ignoredCheckIns) {
22122232
/** Returns the current {@link SentryDateProvider} that is used to retrieve the current date. */
22132233
@ApiStatus.Internal
22142234
public @NotNull SentryDateProvider getDateProvider() {
2215-
return dateProvider;
2235+
if (dateProvider == null) {
2236+
synchronized (dateProviderLock) {
2237+
if (dateProvider == null) {
2238+
dateProvider = new SentryAutoDateProvider();
2239+
}
2240+
}
2241+
}
2242+
return Objects.requireNonNull(dateProvider, "DateProvider is not set");
22162243
}
22172244

22182245
/**
@@ -2540,6 +2567,7 @@ public SentryOptions() {
25402567
* @param empty if options should be empty.
25412568
*/
25422569
private SentryOptions(final boolean empty) {
2570+
experimental = new ExperimentalOptions(empty);
25432571
if (!empty) {
25442572
// SentryExecutorService should be initialized before any
25452573
// SendCachedEventFireAndForgetIntegration

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,16 @@ public enum SentryReplayQuality {
9696
/** The maximum duration of a full session replay, defaults to 1h. */
9797
private long sessionDuration = 60 * 60 * 1000L;
9898

99-
public SentryReplayOptions() {
100-
setRedactAllText(true);
101-
setRedactAllImages(true);
99+
public SentryReplayOptions(final boolean empty) {
100+
if (!empty) {
101+
setRedactAllText(true);
102+
setRedactAllImages(true);
103+
}
102104
}
103105

104106
public SentryReplayOptions(
105107
final @Nullable Double sessionSampleRate, final @Nullable Double onErrorSampleRate) {
106-
this();
108+
this(false);
107109
this.sessionSampleRate = sessionSampleRate;
108110
this.onErrorSampleRate = onErrorSampleRate;
109111
}

sentry/src/main/java/io/sentry/clientreport/AtomicClientReportStorage.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package io.sentry.clientreport;
22

33
import io.sentry.DataCategory;
4+
import io.sentry.util.LazyEvaluator;
45
import java.util.ArrayList;
56
import java.util.Collections;
67
import java.util.List;
78
import java.util.Map;
9+
import java.util.Set;
810
import java.util.concurrent.ConcurrentHashMap;
911
import java.util.concurrent.atomic.AtomicLong;
1012
import org.jetbrains.annotations.ApiStatus;
@@ -14,25 +16,25 @@
1416
@ApiStatus.Internal
1517
final class AtomicClientReportStorage implements IClientReportStorage {
1618

17-
private final @NotNull Map<ClientReportKey, AtomicLong> lostEventCounts;
18-
19-
public AtomicClientReportStorage() {
19+
private final @NotNull LazyEvaluator<Map<ClientReportKey, AtomicLong>> lostEventCounts = new LazyEvaluator<>(() -> {
2020
final Map<ClientReportKey, AtomicLong> modifyableEventCountsForInit = new ConcurrentHashMap<>();
2121

2222
for (final DiscardReason discardReason : DiscardReason.values()) {
2323
for (final DataCategory category : DataCategory.values()) {
2424
modifyableEventCountsForInit.put(
25-
new ClientReportKey(discardReason.getReason(), category.getCategory()),
26-
new AtomicLong(0));
25+
new ClientReportKey(discardReason.getReason(), category.getCategory()),
26+
new AtomicLong(0));
2727
}
2828
}
2929

30-
lostEventCounts = Collections.unmodifiableMap(modifyableEventCountsForInit);
31-
}
30+
return Collections.unmodifiableMap(modifyableEventCountsForInit);
31+
});
32+
33+
public AtomicClientReportStorage() {}
3234

3335
@Override
3436
public void addCount(ClientReportKey key, Long count) {
35-
final @Nullable AtomicLong quantity = lostEventCounts.get(key);
37+
final @Nullable AtomicLong quantity = lostEventCounts.getValue().get(key);
3638

3739
if (quantity != null) {
3840
quantity.addAndGet(count);
@@ -43,7 +45,8 @@ public void addCount(ClientReportKey key, Long count) {
4345
public List<DiscardedEvent> resetCountsAndGet() {
4446
final List<DiscardedEvent> discardedEvents = new ArrayList<>();
4547

46-
for (final Map.Entry<ClientReportKey, AtomicLong> entry : lostEventCounts.entrySet()) {
48+
Set<Map.Entry<ClientReportKey, AtomicLong>> entrySet = lostEventCounts.getValue().entrySet();
49+
for (final Map.Entry<ClientReportKey, AtomicLong> entry : entrySet) {
4750
final Long quantity = entry.getValue().getAndSet(0);
4851
if (quantity > 0) {
4952
discardedEvents.add(

0 commit comments

Comments
 (0)