Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data field to SentrySpan. #1555

Merged
merged 6 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (#1555)

## 5.1.0-beta.1

Expand Down
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,8 @@ public final class io/sentry/protocol/SentryRuntime : io/sentry/IUnknownProperti

public final class io/sentry/protocol/SentrySpan {
public fun <init> (Lio/sentry/Span;)V
public fun <init> (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;
Expand Down
11 changes: 11 additions & 0 deletions sentry/src/main/java/io/sentry/protocol/SentrySpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ public final class SentrySpan {
private final @Nullable String description;
private final @Nullable SpanStatus status;
private final @NotNull Map<String, String> tags;
private final @Nullable Map<String, Object> data;

public SentrySpan(final @NotNull Span span) {
this(span, null);
}

@ApiStatus.Internal
public SentrySpan(final @NotNull Span span, final @Nullable Map<String, Object> data) {
Objects.requireNonNull(span, "span is required");
this.description = span.getDescription();
this.op = span.getOperation();
Expand All @@ -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() {
Expand Down Expand Up @@ -77,4 +84,8 @@ public boolean isFinished() {
public @NotNull Map<String, String> getTags() {
return tags;
}

public @Nullable Map<String, Object> getData() {
return data;
}
}
35 changes: 35 additions & 0 deletions sentry/src/test/java/io/sentry/GsonSerializerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -523,6 +524,9 @@ class GsonSerializerTest {
"status": "aborted",
"tags": {
"name": "value"
},
"data": {
"key": "value"
}
}
]
Expand All @@ -547,6 +551,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())
Expand All @@ -556,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)
maciejwalkowiak marked this conversation as resolved.
Show resolved Hide resolved

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)
Expand Down Expand Up @@ -696,4 +718,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
}
}