Skip to content

Commit

Permalink
Add an option for opting out of pretty print (#2871)
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer authored Aug 17, 2023
1 parent 3f6f636 commit 9e60fc1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Send `db.system` and `db.name` in span data ([#2894](https://github.com/getsentry/sentry-java/pull/2894))
- Send `http.request.method` in span data ([#2896](https://github.com/getsentry/sentry-java/pull/2896))
- Add `enablePrettySerializationOutput` option for opting out of pretty print ([#2871](https://github.com/getsentry/sentry-java/pull/2871))

## 6.28.0

Expand Down
4 changes: 4 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,13 @@ public final class io/sentry/ExternalOptions {
public fun getTracePropagationTargets ()Ljava/util/List;
public fun getTracesSampleRate ()Ljava/lang/Double;
public fun getTracingOrigins ()Ljava/util/List;
public fun isEnablePrettySerializationOutput ()Ljava/lang/Boolean;
public fun isEnabled ()Ljava/lang/Boolean;
public fun setDebug (Ljava/lang/Boolean;)V
public fun setDist (Ljava/lang/String;)V
public fun setDsn (Ljava/lang/String;)V
public fun setEnableDeduplication (Ljava/lang/Boolean;)V
public fun setEnablePrettySerializationOutput (Ljava/lang/Boolean;)V
public fun setEnableTracing (Ljava/lang/Boolean;)V
public fun setEnableUncaughtExceptionHandler (Ljava/lang/Boolean;)V
public fun setEnabled (Ljava/lang/Boolean;)V
Expand Down Expand Up @@ -1825,6 +1827,7 @@ public class io/sentry/SentryOptions {
public fun isEnableDeduplication ()Z
public fun isEnableExternalConfiguration ()Z
public fun isEnableNdk ()Z
public fun isEnablePrettySerializationOutput ()Z
public fun isEnableScopeSync ()Z
public fun isEnableShutdownHook ()Z
public fun isEnableTimeToFullDisplayTracing ()Z
Expand Down Expand Up @@ -1859,6 +1862,7 @@ public class io/sentry/SentryOptions {
public fun setEnableDeduplication (Z)V
public fun setEnableExternalConfiguration (Z)V
public fun setEnableNdk (Z)V
public fun setEnablePrettySerializationOutput (Z)V
public fun setEnableScopeSync (Z)V
public fun setEnableShutdownHook (Z)V
public fun setEnableTimeToFullDisplayTracing (Z)V
Expand Down
13 changes: 13 additions & 0 deletions sentry/src/main/java/io/sentry/ExternalOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public final class ExternalOptions {
private @Nullable Boolean sendClientReports;
private @NotNull Set<String> bundleIds = new CopyOnWriteArraySet<>();
private @Nullable Boolean enabled;
private @Nullable Boolean enablePrettySerializationOutput;

@SuppressWarnings("unchecked")
public static @NotNull ExternalOptions from(
Expand Down Expand Up @@ -118,6 +119,9 @@ public final class ExternalOptions {

options.setEnabled(propertiesProvider.getBooleanProperty("enabled"));

options.setEnablePrettySerializationOutput(
propertiesProvider.getBooleanProperty("enable-pretty-serialization-output"));

for (final String ignoredExceptionType :
propertiesProvider.getList("ignored-exceptions-for-type")) {
try {
Expand Down Expand Up @@ -358,4 +362,13 @@ public void addBundleId(final @NotNull String bundleId) {
public void setEnabled(final @Nullable Boolean enabled) {
this.enabled = enabled;
}

public @Nullable Boolean isEnablePrettySerializationOutput() {
return enablePrettySerializationOutput;
}

public void setEnablePrettySerializationOutput(
final @Nullable Boolean enablePrettySerializationOutput) {
this.enablePrettySerializationOutput = enablePrettySerializationOutput;
}
}
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/JsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public <T> void serialize(@NotNull T entity, @NotNull Writer writer) throws IOEx
Objects.requireNonNull(writer, "The Writer object is required.");

if (options.getLogger().isEnabled(SentryLevel.DEBUG)) {
String serialized = serializeToString(entity, true);
String serialized = serializeToString(entity, options.isEnablePrettySerializationOutput());
options.getLogger().log(SentryLevel.DEBUG, "Serializing object: %s", serialized);
}
JsonObjectWriter jsonObjectWriter = new JsonObjectWriter(writer, options.getMaxDepth());
Expand Down
24 changes: 24 additions & 0 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ public class SentryOptions {
/** Whether Sentry should be enabled */
private boolean enabled = true;

/** Whether to format serialized data, e.g. events logged to console in debug mode */
private boolean enablePrettySerializationOutput = true;

/**
* Adds an event processor
*
Expand Down Expand Up @@ -2117,6 +2120,24 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

/**
* Whether to format serialized data, e.g. events logged to console in debug mode
*
* @return true if data should be pretty printed
*/
public boolean isEnablePrettySerializationOutput() {
return enablePrettySerializationOutput;
}

/**
* Whether to format serialized data, e.g. events logged to console in debug mode
*
* @param enablePrettySerializationOutput true if output should be pretty printed
*/
public void setEnablePrettySerializationOutput(boolean enablePrettySerializationOutput) {
this.enablePrettySerializationOutput = enablePrettySerializationOutput;
}

/** Returns the current {@link SentryDateProvider} that is used to retrieve the current date. */
@ApiStatus.Internal
public @NotNull SentryDateProvider getDateProvider() {
Expand Down Expand Up @@ -2358,6 +2379,9 @@ public void merge(final @NotNull ExternalOptions options) {
if (options.isEnabled() != null) {
setEnabled(options.isEnabled());
}
if (options.isEnablePrettySerializationOutput() != null) {
setEnablePrettySerializationOutput(options.isEnablePrettySerializationOutput());
}
}

private @NotNull SdkVersion createSdkVersion() {
Expand Down
7 changes: 7 additions & 0 deletions sentry/src/test/java/io/sentry/SentryOptionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ class SentryOptionsTest {
externalOptions.idleTimeout = 1500L
externalOptions.bundleIds.addAll(listOf("12ea7a02-46ac-44c0-a5bb-6d1fd9586411 ", " faa3ab42-b1bd-4659-af8e-1682324aa744"))
externalOptions.isEnabled = false
externalOptions.isEnablePrettySerializationOutput = false
val options = SentryOptions()

options.merge(externalOptions)
Expand All @@ -394,6 +395,7 @@ class SentryOptionsTest {
assertEquals(1500L, options.idleTimeout)
assertEquals(setOf("12ea7a02-46ac-44c0-a5bb-6d1fd9586411", "faa3ab42-b1bd-4659-af8e-1682324aa744"), options.bundleIds)
assertFalse(options.isEnabled)
assertFalse(options.isEnablePrettySerializationOutput)
}

@Test
Expand Down Expand Up @@ -490,4 +492,9 @@ class SentryOptionsTest {
fun `when options are initialized, enabled is set to true by default`() {
assertTrue(SentryOptions().isEnabled)
}

@Test
fun `when options are initialized, enablePrettySerializationOutput is set to true by default`() {
assertTrue(SentryOptions().isEnablePrettySerializationOutput)
}
}

0 comments on commit 9e60fc1

Please sign in to comment.