From 6376f990cbdd10ded072f862e7e249fa57ced854 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Thu, 24 Jun 2021 10:17:20 +0200 Subject: [PATCH 1/4] Add data field to SentrySpan. Fixes #1538 --- CHANGELOG.md | 1 + sentry/api/sentry.api | 2 ++ .../test/java/io/sentry/GsonSerializerTest.kt | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64497b8058..0be79e67a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Fix: Handling missing Spring Security on classpath on Java 8 (#1552) * Feat: Support transaction waiting for children to finish. (#1535) * Feat: Capture logged marker in log4j2 and logback appenders (#1551) +* Fix: Add data field to SentrySpan () ## 5.1.0-beta.1 diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 15945664cc..1e25bf9548 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -1664,6 +1664,8 @@ public final class io/sentry/protocol/SentryRuntime : io/sentry/IUnknownProperti public final class io/sentry/protocol/SentrySpan { public fun (Lio/sentry/Span;)V + public fun (Lio/sentry/Span;Ljava/util/Map;)V + public fun getData ()Ljava/util/Map; public fun getDescription ()Ljava/lang/String; public fun getOp ()Ljava/lang/String; public fun getParentSpanId ()Lio/sentry/SpanId; diff --git a/sentry/src/test/java/io/sentry/GsonSerializerTest.kt b/sentry/src/test/java/io/sentry/GsonSerializerTest.kt index 8ab2a95b29..9865f330fe 100644 --- a/sentry/src/test/java/io/sentry/GsonSerializerTest.kt +++ b/sentry/src/test/java/io/sentry/GsonSerializerTest.kt @@ -523,6 +523,9 @@ class GsonSerializerTest { "status": "aborted", "tags": { "name": "value" + }, + "data": { + "key": "value" } } ] @@ -547,6 +550,9 @@ class GsonSerializerTest { val span = transaction.spans[0] assertNotNull(span.startTimestamp) assertNotNull(span.timestamp) + assertNotNull(span.data) { + assertEquals("value", it["key"]) + } assertEquals("2b099185293344a5bfdd7ad89ebf9416", span.traceId.toString()) assertEquals("5b95c29a5ded4281", span.spanId.toString()) assertEquals("a3b2d1d58b344b07", span.parentSpanId.toString()) @@ -696,4 +702,17 @@ class GsonSerializerTest { comments = "comment" } } + + private fun createSpan(): ISpan { + val trace = TransactionContext("transaction-name", "http").apply { + description = "some request" + status = SpanStatus.OK + setTag("myTag", "myValue") + } + val tracer = SentryTracer(trace, fixture.hub) + val span = tracer.startChild("child") + span.finish(SpanStatus.OK) + tracer.finish() + return span + } } From fd66f4692ec945d2f18afdd99306632254c11528 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Thu, 24 Jun 2021 10:18:42 +0200 Subject: [PATCH 2/4] Add data field to SentrySpan. Fixes #1538 --- .../main/java/io/sentry/protocol/SentrySpan.java | 11 +++++++++++ .../test/java/io/sentry/GsonSerializerTest.kt | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/sentry/src/main/java/io/sentry/protocol/SentrySpan.java b/sentry/src/main/java/io/sentry/protocol/SentrySpan.java index 147f2e3bd0..fbcd743ba5 100644 --- a/sentry/src/main/java/io/sentry/protocol/SentrySpan.java +++ b/sentry/src/main/java/io/sentry/protocol/SentrySpan.java @@ -23,8 +23,14 @@ public final class SentrySpan { private final @Nullable String description; private final @Nullable SpanStatus status; private final @NotNull Map tags; + private final @Nullable Map data; public SentrySpan(final @NotNull Span span) { + this(span, null); + } + + @ApiStatus.Internal + public SentrySpan(final @NotNull Span span, final @Nullable Map data) { Objects.requireNonNull(span, "span is required"); this.description = span.getDescription(); this.op = span.getOperation(); @@ -36,6 +42,7 @@ public SentrySpan(final @NotNull Span span) { this.tags = tagsCopy != null ? tagsCopy : new ConcurrentHashMap<>(); this.timestamp = span.getTimestamp(); this.startTimestamp = span.getStartTimestamp(); + this.data = data; } public boolean isFinished() { @@ -77,4 +84,8 @@ public boolean isFinished() { public @NotNull Map getTags() { return tags; } + + public @Nullable Map getData() { + return data; + } } diff --git a/sentry/src/test/java/io/sentry/GsonSerializerTest.kt b/sentry/src/test/java/io/sentry/GsonSerializerTest.kt index 9865f330fe..d61da79116 100644 --- a/sentry/src/test/java/io/sentry/GsonSerializerTest.kt +++ b/sentry/src/test/java/io/sentry/GsonSerializerTest.kt @@ -13,6 +13,7 @@ import io.sentry.exception.SentryEnvelopeException import io.sentry.protocol.Device import io.sentry.protocol.SdkVersion import io.sentry.protocol.SentryId +import io.sentry.protocol.SentrySpan import io.sentry.protocol.SentryTransaction import java.io.BufferedWriter import java.io.ByteArrayInputStream @@ -562,6 +563,21 @@ class GsonSerializerTest { assertEquals(mapOf("name" to "value"), span.tags) } + @Test + fun `serializes span data`() { + val sentrySpan = SentrySpan(createSpan() as Span, mapOf("data1" to "value1")) + + val stringWriter = StringWriter() + fixture.serializer.serialize(sentrySpan, stringWriter) + + val element = JsonParser().parse(stringWriter.toString()).asJsonObject + assertNotNull(element["data"]) { + assertNotNull(it.asJsonObject["data1"]) { + assertEquals("value1", it.asString) + } + } + } + @Test fun `serializing user feedback`() { val actual = serializeToString(userFeedback) From 6ad35590e6a8ce84ffe81fd0814f0d79dfed4666 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Thu, 24 Jun 2021 10:19:30 +0200 Subject: [PATCH 3/4] Changelog. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0be79e67a4..850368e427 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ * Fix: Handling missing Spring Security on classpath on Java 8 (#1552) * Feat: Support transaction waiting for children to finish. (#1535) * Feat: Capture logged marker in log4j2 and logback appenders (#1551) -* Fix: Add data field to SentrySpan () +* Fix: Add data field to SentrySpan (#1555) ## 5.1.0-beta.1 From 0fc2338362cc2b3353c388b3bc4d44a0b6d5a834 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Thu, 24 Jun 2021 13:22:49 +0200 Subject: [PATCH 4/4] Refactor. --- .../test/java/io/sentry/GsonSerializerTest.kt | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/sentry/src/test/java/io/sentry/GsonSerializerTest.kt b/sentry/src/test/java/io/sentry/GsonSerializerTest.kt index d61da79116..80f7a34c5d 100644 --- a/sentry/src/test/java/io/sentry/GsonSerializerTest.kt +++ b/sentry/src/test/java/io/sentry/GsonSerializerTest.kt @@ -62,16 +62,8 @@ class GsonSerializerTest { fixture = Fixture() } - private fun serializeToString(ev: SentryEvent): String { - return this.serializeToString { wrt -> fixture.serializer.serialize(ev, wrt) } - } - - private fun serializeToString(session: Session): String { - return this.serializeToString { wrt -> fixture.serializer.serialize(session, wrt) } - } - - private fun serializeToString(userFeedback: UserFeedback): String { - return this.serializeToString { wrt -> fixture.serializer.serialize(userFeedback, wrt) } + private fun serializeToString(ev: T): String { + return this.serializeToString { wrt -> fixture.serializer.serialize(ev!!, wrt) } } private fun serializeToString(serialize: (StringWriter) -> Unit): String { @@ -567,10 +559,7 @@ class GsonSerializerTest { fun `serializes span data`() { val sentrySpan = SentrySpan(createSpan() as Span, mapOf("data1" to "value1")) - val stringWriter = StringWriter() - fixture.serializer.serialize(sentrySpan, stringWriter) - - val element = JsonParser().parse(stringWriter.toString()).asJsonObject + val element = JsonParser().parse(serializeToString(sentrySpan)).asJsonObject assertNotNull(element["data"]) { assertNotNull(it.asJsonObject["data1"]) { assertEquals("value1", it.asString)