Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@

- Report the cellular network technology generation in `device.connection_effective_type`, for example `4g` or `5g` ([#6146](https://github.com/getsentry/sentry-java/pull/6146))

### Fixes

- Avoid duplicate serialization debug logs when calculating serialized object sizes ([#6255](https://github.com/getsentry/sentry-java/pull/6255))

### Dependencies

- Bump Native SDK from v0.17.1 to v0.17.2 ([#6230](https://github.com/getsentry/sentry-java/pull/6230))
Expand Down
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,7 @@ public abstract interface class io/sentry/ISerializer {
public abstract fun deserializeEnvelope (Ljava/io/InputStream;)Lio/sentry/SentryEnvelope;
public abstract fun serialize (Lio/sentry/SentryEnvelope;Ljava/io/OutputStream;)V
public abstract fun serialize (Ljava/lang/Object;Ljava/io/Writer;)V
public fun serialize (Ljava/lang/Object;Ljava/io/Writer;Z)V
public abstract fun serialize (Ljava/util/Map;)Ljava/lang/String;
}

Expand Down Expand Up @@ -1450,6 +1451,7 @@ public final class io/sentry/JsonSerializer : io/sentry/ISerializer {
public fun deserializeEnvelope (Ljava/io/InputStream;)Lio/sentry/SentryEnvelope;
public fun serialize (Lio/sentry/SentryEnvelope;Ljava/io/OutputStream;)V
public fun serialize (Ljava/lang/Object;Ljava/io/Writer;)V
public fun serialize (Ljava/lang/Object;Ljava/io/Writer;Z)V
public fun serialize (Ljava/util/Map;)Ljava/lang/String;
}

Expand Down
14 changes: 14 additions & 0 deletions sentry/src/main/java/io/sentry/ISerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ public interface ISerializer {

<T> void serialize(@NotNull T entity, @NotNull Writer writer) throws IOException;

/**
* Serializes an entity to a writer with optional logging control.
*
* @param entity the entity to serialize
* @param writer the writer to serialize to
* @param enableLogging whether serializer logging is enabled, if supported
* @param <T> the type of entity
* @throws IOException if serialization fails
*/
default <T> void serialize(@NotNull T entity, @NotNull Writer writer, boolean enableLogging)
throws IOException {
serialize(entity, writer);
}

/**
* Serializes an envelope
*
Expand Down
8 changes: 7 additions & 1 deletion sentry/src/main/java/io/sentry/JsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,16 @@ public JsonSerializer(@NotNull SentryOptions options) {

@Override
public <T> void serialize(@NotNull T entity, @NotNull Writer writer) throws IOException {
serialize(entity, writer, true);
}

@Override
public <T> void serialize(@NotNull T entity, @NotNull Writer writer, boolean enableLogging)
throws IOException {
Objects.requireNonNull(entity, "The entity is required.");
Objects.requireNonNull(writer, "The Writer object is required.");

if (options.getLogger().isEnabled(SentryLevel.DEBUG)) {
if (enableLogging && options.getLogger().isEnabled(SentryLevel.DEBUG)) {
String serialized = serializeToString(entity, options.isEnablePrettySerializationOutput());
options.getLogger().log(SentryLevel.DEBUG, "Serializing object: %s", serialized);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static long byteSizeOf(
}
try {
final ByteCountingWriter writer = new ByteCountingWriter();
serializer.serialize(serializable, writer);
serializer.serialize(serializable, writer, false);
return writer.getByteCount();
} catch (Throwable t) {
logger.log(SentryLevel.ERROR, "Could not calculate size of serializable", t);
Expand Down
14 changes: 14 additions & 0 deletions sentry/src/test/java/io/sentry/JsonSerializerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,20 @@ class JsonSerializerTest {
.log(any(), check { assertTrue(it.startsWith("Serializing object:")) }, any<Any>())
}

@Test
fun `json serializer can disable serialization logging`() {
val logger = mock<ILogger>()
val options = SentryOptions()
options.setLogger(logger)
options.setDebug(true)
whenever(logger.isEnabled(any())).thenReturn(true)

(options.serializer as JsonSerializer).serialize(mapOf("key" to "val"), StringWriter(), false)

verify(logger, never())
.log(any(), check { assertTrue(it.startsWith("Serializing object:")) }, any<Any>())
}

@Test
fun `json serializer does not close the stream that is passed in`() {
val stream = mock<OutputStream>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry.util

import io.sentry.ILogger
import io.sentry.ISerializer
import io.sentry.JsonSerializable
import io.sentry.JsonSerializer
import io.sentry.ObjectWriter
Expand All @@ -18,7 +19,9 @@ import kotlin.test.assertNull
import kotlin.test.assertTrue
import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify

class JsonSerializationUtilsTest {

Expand Down Expand Up @@ -117,6 +120,16 @@ class JsonSerializationUtilsTest {
assertTrue(byteSize > 0, "byteSize should be positive")
}

@Test
fun `byteSizeOf disables serialization logging`() {
val mockSerializer: ISerializer = mock()
val serializable: JsonSerializable = mock()

JsonSerializationUtils.byteSizeOf(mockSerializer, logger, serializable)

verify(mockSerializer).serialize(eq(serializable), any(), eq(false))
}

@Test
fun `byteSizeOf returns 0 on serialization error`() {
val serializable =
Expand Down
Loading