Skip to content

Commit a126274

Browse files
authored
Merge 91dbdcd into 654c2dc
2 parents 654c2dc + 91dbdcd commit a126274

File tree

8 files changed

+32
-7
lines changed

8 files changed

+32
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Fixes
1010

11+
- Cache parsed Dsn ([#3796](https://github.com/getsentry/sentry-java/pull/3796))
1112
- fix invalid profiles when the transaction name is empty ([#3747](https://github.com/getsentry/sentry-java/pull/3747))
1213
- Deprecate `enableTracing` option ([#3777](https://github.com/getsentry/sentry-java/pull/3777))
1314
- Vendor `java.util.Random` and replace `java.security.SecureRandom` usages ([#3783](https://github.com/getsentry/sentry-java/pull/3783))

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5704,6 +5704,7 @@ public final class io/sentry/util/JsonSerializationUtils {
57045704
public final class io/sentry/util/LazyEvaluator {
57055705
public fun <init> (Lio/sentry/util/LazyEvaluator$Evaluator;)V
57065706
public fun getValue ()Ljava/lang/Object;
5707+
public fun resetValue ()V
57075708
public fun setValue (Ljava/lang/Object;)V
57085709
}
57095710

sentry/src/main/java/io/sentry/Baggage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static Baggage fromEvent(
134134
final Baggage baggage = new Baggage(options.getLogger());
135135
final SpanContext trace = event.getContexts().getTrace();
136136
baggage.setTraceId(trace != null ? trace.getTraceId().toString() : null);
137-
baggage.setPublicKey(new Dsn(options.getDsn()).getPublicKey());
137+
baggage.setPublicKey(options.getParsedDsn().getPublicKey());
138138
baggage.setRelease(event.getRelease());
139139
baggage.setEnvironment(event.getEnvironment());
140140
final User user = event.getUser();
@@ -405,7 +405,7 @@ public void setValuesFromTransaction(
405405
final @NotNull SentryOptions sentryOptions,
406406
final @Nullable TracesSamplingDecision samplingDecision) {
407407
setTraceId(transaction.getSpanContext().getTraceId().toString());
408-
setPublicKey(new Dsn(sentryOptions.getDsn()).getPublicKey());
408+
setPublicKey(sentryOptions.getParsedDsn().getPublicKey());
409409
setRelease(sentryOptions.getRelease());
410410
setEnvironment(sentryOptions.getEnvironment());
411411
setUserSegment(user != null ? getSegment(user) : null);
@@ -427,7 +427,7 @@ public void setValuesFromScope(
427427
final @Nullable User user = scope.getUser();
428428
final @NotNull SentryId replayId = scope.getReplayId();
429429
setTraceId(propagationContext.getTraceId().toString());
430-
setPublicKey(new Dsn(options.getDsn()).getPublicKey());
430+
setPublicKey(options.getParsedDsn().getPublicKey());
431431
setRelease(options.getRelease());
432432
setEnvironment(options.getEnvironment());
433433
if (!SentryId.EMPTY_ID.equals(replayId)) {

sentry/src/main/java/io/sentry/DsnUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static boolean urlContainsDsnHost(@Nullable SentryOptions options, @Nulla
2323
return false;
2424
}
2525

26-
final @NotNull Dsn dsn = new Dsn(dsnString);
26+
final @NotNull Dsn dsn = options.getParsedDsn();
2727
final @NotNull URI sentryUri = dsn.getSentryUri();
2828
final @Nullable String dsnHost = sentryUri.getHost();
2929

sentry/src/main/java/io/sentry/RequestDetailsResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public RequestDetailsResolver(final @NotNull SentryOptions options) {
2121

2222
@NotNull
2323
RequestDetails resolve() {
24-
final Dsn dsn = new Dsn(options.getDsn());
24+
final Dsn dsn = options.getParsedDsn();
2525
final URI sentryUri = dsn.getSentryUri();
2626
final String envelopeUrl = sentryUri.resolve(sentryUri.getPath() + "/envelope/").toString();
2727

sentry/src/main/java/io/sentry/Sentry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ private static boolean initConfigurations(final @NotNull SentryOptions options)
381381
"DSN is required. Use empty string or set enabled to false in SentryOptions to disable SDK.");
382382
}
383383

384-
@SuppressWarnings("unused")
385-
final Dsn parsedDsn = new Dsn(dsn);
384+
// This creates the DSN object and performs some checks
385+
options.getParsedDsn();
386386

387387
ILogger logger = options.getLogger();
388388

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public class SentryOptions {
8282
*/
8383
private @Nullable String dsn;
8484

85+
/** Parsed DSN to avoid parsing it every time. */
86+
private final @NotNull LazyEvaluator<Dsn> parsedDsn = new LazyEvaluator<>(() -> new Dsn(dsn));
87+
8588
/** dsnHash is used as a subfolder of cacheDirPath to isolate events when rotating DSNs */
8689
private @Nullable String dsnHash;
8790

@@ -537,13 +540,27 @@ public void addIntegration(@NotNull Integration integration) {
537540
return dsn;
538541
}
539542

543+
/**
544+
* Returns the DSN
545+
*
546+
* @return the DSN or null if not set
547+
*/
548+
@ApiStatus.Internal
549+
@NotNull
550+
Dsn getParsedDsn() {
551+
return parsedDsn.getValue();
552+
}
553+
540554
/**
541555
* Sets the DSN
542556
*
543557
* @param dsn the DSN
544558
*/
545559
public void setDsn(final @Nullable String dsn) {
546560
this.dsn = dsn;
561+
if (!isEnabled() || (dsn != null && dsn.isEmpty())) {
562+
this.parsedDsn.resetValue();
563+
}
547564

548565
dsnHash = StringUtils.calculateStringHash(this.dsn, logger);
549566
}

sentry/src/main/java/io/sentry/util/LazyEvaluator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public void setValue(final @Nullable T value) {
4848
}
4949
}
5050

51+
public void resetValue() {
52+
synchronized (this) {
53+
this.value = null;
54+
}
55+
}
56+
5157
public interface Evaluator<T> {
5258
@NotNull
5359
T evaluate();

0 commit comments

Comments
 (0)