From 9e60fc1fe6e17eaf16620e62eff24459aeec2505 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Thu, 17 Aug 2023 16:35:15 +0200 Subject: [PATCH] Add an option for opting out of pretty print (#2871) --- CHANGELOG.md | 1 + sentry/api/sentry.api | 4 ++++ .../main/java/io/sentry/ExternalOptions.java | 13 ++++++++++ .../main/java/io/sentry/JsonSerializer.java | 2 +- .../main/java/io/sentry/SentryOptions.java | 24 +++++++++++++++++++ .../test/java/io/sentry/SentryOptionsTest.kt | 7 ++++++ 6 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc75708ecb..7427a063c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 43ca4d5fad..67cbd93d9a 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -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 @@ -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 @@ -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 diff --git a/sentry/src/main/java/io/sentry/ExternalOptions.java b/sentry/src/main/java/io/sentry/ExternalOptions.java index 1d5f71431e..0bdd89f247 100644 --- a/sentry/src/main/java/io/sentry/ExternalOptions.java +++ b/sentry/src/main/java/io/sentry/ExternalOptions.java @@ -43,6 +43,7 @@ public final class ExternalOptions { private @Nullable Boolean sendClientReports; private @NotNull Set bundleIds = new CopyOnWriteArraySet<>(); private @Nullable Boolean enabled; + private @Nullable Boolean enablePrettySerializationOutput; @SuppressWarnings("unchecked") public static @NotNull ExternalOptions from( @@ -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 { @@ -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; + } } diff --git a/sentry/src/main/java/io/sentry/JsonSerializer.java b/sentry/src/main/java/io/sentry/JsonSerializer.java index 8018211d56..a7d0bb10c3 100644 --- a/sentry/src/main/java/io/sentry/JsonSerializer.java +++ b/sentry/src/main/java/io/sentry/JsonSerializer.java @@ -183,7 +183,7 @@ public 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()); diff --git a/sentry/src/main/java/io/sentry/SentryOptions.java b/sentry/src/main/java/io/sentry/SentryOptions.java index 319dd8118c..7a0b0daab7 100644 --- a/sentry/src/main/java/io/sentry/SentryOptions.java +++ b/sentry/src/main/java/io/sentry/SentryOptions.java @@ -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 * @@ -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() { @@ -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() { diff --git a/sentry/src/test/java/io/sentry/SentryOptionsTest.kt b/sentry/src/test/java/io/sentry/SentryOptionsTest.kt index a5304d7de3..3e34435ac6 100644 --- a/sentry/src/test/java/io/sentry/SentryOptionsTest.kt +++ b/sentry/src/test/java/io/sentry/SentryOptionsTest.kt @@ -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) @@ -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 @@ -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) + } }