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

Feat: Add "data" to spans #1717

Merged
merged 6 commits into from
Sep 15, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* Feat: Add "data" to spans (#1717)
* Fix: Should not capture unfinished transaction (#1719)

## 5.2.0-beta.2
Expand Down
12 changes: 12 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,15 @@ public abstract interface class io/sentry/ISerializer {
public abstract interface class io/sentry/ISpan {
public abstract fun finish ()V
public abstract fun finish (Lio/sentry/SpanStatus;)V
public abstract fun getData (Ljava/lang/String;)Ljava/lang/Object;
public abstract fun getDescription ()Ljava/lang/String;
public abstract fun getOperation ()Ljava/lang/String;
public abstract fun getSpanContext ()Lio/sentry/SpanContext;
public abstract fun getStatus ()Lio/sentry/SpanStatus;
public abstract fun getTag (Ljava/lang/String;)Ljava/lang/String;
public abstract fun getThrowable ()Ljava/lang/Throwable;
public abstract fun isFinished ()Z
public abstract fun setData (Ljava/lang/String;Ljava/lang/Object;)V
public abstract fun setDescription (Ljava/lang/String;)V
public abstract fun setOperation (Ljava/lang/String;)V
public abstract fun setStatus (Lio/sentry/SpanStatus;)V
Expand Down Expand Up @@ -414,6 +416,7 @@ public final class io/sentry/NoOpLogger : io/sentry/ILogger {
public final class io/sentry/NoOpSpan : io/sentry/ISpan {
public fun finish ()V
public fun finish (Lio/sentry/SpanStatus;)V
public fun getData (Ljava/lang/String;)Ljava/lang/Object;
public fun getDescription ()Ljava/lang/String;
public static fun getInstance ()Lio/sentry/NoOpSpan;
public fun getOperation ()Ljava/lang/String;
Expand All @@ -422,6 +425,7 @@ public final class io/sentry/NoOpSpan : io/sentry/ISpan {
public fun getTag (Ljava/lang/String;)Ljava/lang/String;
public fun getThrowable ()Ljava/lang/Throwable;
public fun isFinished ()Z
public fun setData (Ljava/lang/String;Ljava/lang/Object;)V
public fun setDescription (Ljava/lang/String;)V
public fun setOperation (Ljava/lang/String;)V
public fun setStatus (Lio/sentry/SpanStatus;)V
Expand All @@ -439,6 +443,7 @@ public final class io/sentry/NoOpTransaction : io/sentry/ITransaction {
public fun finish ()V
public fun finish (Lio/sentry/SpanStatus;)V
public fun getContexts ()Lio/sentry/protocol/Contexts;
public fun getData (Ljava/lang/String;)Ljava/lang/Object;
public fun getDescription ()Ljava/lang/String;
public fun getEventId ()Lio/sentry/protocol/SentryId;
public static fun getInstance ()Lio/sentry/NoOpTransaction;
Expand All @@ -453,6 +458,7 @@ public final class io/sentry/NoOpTransaction : io/sentry/ITransaction {
public fun getThrowable ()Ljava/lang/Throwable;
public fun isFinished ()Z
public fun isSampled ()Ljava/lang/Boolean;
public fun setData (Ljava/lang/String;Ljava/lang/Object;)V
public fun setDescription (Ljava/lang/String;)V
public fun setName (Ljava/lang/String;)V
public fun setOperation (Ljava/lang/String;)V
Expand Down Expand Up @@ -971,6 +977,8 @@ public final class io/sentry/SentryTracer : io/sentry/ITransaction {
public fun finish (Lio/sentry/SpanStatus;)V
public fun getChildren ()Ljava/util/List;
public fun getContexts ()Lio/sentry/protocol/Contexts;
public fun getData ()Ljava/util/Map;
public fun getData (Ljava/lang/String;)Ljava/lang/Object;
public fun getDescription ()Ljava/lang/String;
public fun getEventId ()Lio/sentry/protocol/SentryId;
public fun getLatestActiveSpan ()Lio/sentry/Span;
Expand All @@ -986,6 +994,7 @@ public final class io/sentry/SentryTracer : io/sentry/ITransaction {
public fun getTimestamp ()Ljava/util/Date;
public fun isFinished ()Z
public fun isSampled ()Ljava/lang/Boolean;
public fun setData (Ljava/lang/String;Ljava/lang/Object;)V
public fun setDescription (Ljava/lang/String;)V
public fun setName (Ljava/lang/String;)V
public fun setOperation (Ljava/lang/String;)V
Expand Down Expand Up @@ -1053,6 +1062,8 @@ public final class io/sentry/Span : io/sentry/ISpan {
public fun <init> (Lio/sentry/TransactionContext;Lio/sentry/SentryTracer;Lio/sentry/IHub;Ljava/util/Date;)V
public fun finish ()V
public fun finish (Lio/sentry/SpanStatus;)V
public fun getData ()Ljava/util/Map;
public fun getData (Ljava/lang/String;)Ljava/lang/Object;
public fun getDescription ()Ljava/lang/String;
public fun getOperation ()Ljava/lang/String;
public fun getParentSpanId ()Lio/sentry/SpanId;
Expand All @@ -1067,6 +1078,7 @@ public final class io/sentry/Span : io/sentry/ISpan {
public fun getTraceId ()Lio/sentry/protocol/SentryId;
public fun isFinished ()Z
public fun isSampled ()Ljava/lang/Boolean;
public fun setData (Ljava/lang/String;Ljava/lang/Object;)V
public fun setDescription (Ljava/lang/String;)V
public fun setOperation (Ljava/lang/String;)V
public fun setStatus (Lio/sentry/SpanStatus;)V
Expand Down
16 changes: 16 additions & 0 deletions sentry/src/main/java/io/sentry/ISpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,20 @@ ISpan startChild(
* @return if span has finished.
*/
boolean isFinished();

/**
* Sets extra data on span or transaction.
*
* @param key the data key
* @param value the data value
*/
void setData(@NotNull String key, @NotNull Object value);

/**
* Returns extra data from span or transaction.
*
* @return the data
*/
@Nullable
Object getData(@NotNull String key);
}
8 changes: 8 additions & 0 deletions sentry/src/main/java/io/sentry/NoOpSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,12 @@ public void setTag(@NotNull String key, @NotNull String value) {}
public boolean isFinished() {
return false;
}

@Override
public void setData(@NotNull String key, @NotNull Object value) {}

@Override
public @Nullable Object getData(@NotNull String key) {
return null;
}
}
8 changes: 8 additions & 0 deletions sentry/src/main/java/io/sentry/NoOpTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,12 @@ public void setTag(@NotNull String key, @NotNull String value) {}
public @Nullable Boolean isSampled() {
return null;
}

@Override
public void setData(@NotNull String key, @NotNull Object value) {}

@Override
public @Nullable Object getData(@NotNull String key) {
return null;
}
}
15 changes: 15 additions & 0 deletions sentry/src/main/java/io/sentry/SentryTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -345,6 +346,20 @@ public boolean isFinished() {
return this.root.isFinished();
}

@Override
public void setData(@NotNull String key, @NotNull Object value) {
this.root.setData(key, value);
}

@Override
public @Nullable Object getData(@NotNull String key) {
return this.root.getData(key);
}

public @Nullable Map<String, Object> getData() {
return this.root.getData();
}

@Override
public @Nullable Boolean isSampled() {
return this.root.isSampled();
Expand Down
17 changes: 17 additions & 0 deletions sentry/src/main/java/io/sentry/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.sentry.util.Objects;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -35,6 +36,8 @@ public final class Span implements ISpan {

private final @Nullable SpanFinishedCallback spanFinishedCallback;

private final @NotNull Map<String, Object> data = new ConcurrentHashMap<>();

Span(
final @NotNull SentryId traceId,
final @Nullable SpanId parentSpanId,
Expand Down Expand Up @@ -197,6 +200,10 @@ public boolean isFinished() {
return finished.get();
}

public @NotNull Map<String, Object> getData() {
return data;
}

public @Nullable Boolean isSampled() {
return context.getSampled();
}
Expand Down Expand Up @@ -227,4 +234,14 @@ public SentryId getTraceId() {
public Map<String, String> getTags() {
return context.getTags();
}

@Override
public void setData(@NotNull String key, @NotNull Object value) {
data.put(key, value);
}

@Override
public @Nullable Object getData(@NotNull String key) {
return data.get(key);
}
}
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/protocol/SentrySpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class SentrySpan {
private final @Nullable Map<String, Object> data;

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

@ApiStatus.Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public SentryTransaction(final @NotNull SentryTracer sentryTracer) {
for (final Map.Entry<String, String> tag : tracerContext.getTags().entrySet()) {
this.setTag(tag.getKey(), tag.getValue());
}

final Map<String, Object> data = sentryTracer.getData();
if (data != null) {
for (final Map.Entry<String, Object> tag : data.entrySet()) {
this.setExtra(tag.getKey(), tag.getValue());
}
}
}

public @NotNull List<SentrySpan> getSpans() {
Expand Down
8 changes: 8 additions & 0 deletions sentry/src/test/java/io/sentry/GsonSerializerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ class GsonSerializerTest {
trace.setTag("myTag", "myValue")
trace.sampled = true
val tracer = SentryTracer(trace, fixture.hub)
tracer.setData("dataKey", "dataValue")
val span = tracer.startChild("child")
span.finish(SpanStatus.OK)
tracer.finish()
Expand All @@ -527,6 +528,8 @@ class GsonSerializerTest {
assertNotNull(element["spans"].asJsonArray)
assertEquals("myValue", element["tags"].asJsonObject["myTag"].asString)

assertEquals("dataValue", element["extra"].asJsonObject["dataKey"].asString)

val jsonSpan = element["spans"].asJsonArray[0].asJsonObject
assertNotNull(jsonSpan["trace_id"])
assertNotNull(jsonSpan["span_id"])
Expand Down Expand Up @@ -563,6 +566,9 @@ class GsonSerializerTest {
"some-key": "some-value"
}
},
"extra": {
"extraKey": "extraValue"
},
"spans": [
{
"start_timestamp": "2021-03-05T08:51:12.838Z",
Expand Down Expand Up @@ -597,6 +603,8 @@ class GsonSerializerTest {
assertNotNull(transaction.contexts["custom"])
assertEquals("some-value", (transaction.contexts["custom"] as Map<*, *>)["some-key"])

assertEquals("extraValue", transaction.getExtra("extraKey"))

assertNotNull(transaction.spans)
assertEquals(1, transaction.spans.size)
val span = transaction.spans[0]
Expand Down
24 changes: 24 additions & 0 deletions sentry/src/test/java/io/sentry/SentryTracerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,28 @@ class SentryTracerTest {
assertNotNull(it.value)
}
}

@Test
fun `sets ITransaction data as extra in SentryTransaction`() {
val transaction = fixture.getSut(sampled = true)
transaction.setData("key", "val")
transaction.finish()
verify(fixture.hub).captureTransaction(check {
assertEquals("val", it.getExtra("key"))
}, anyOrNull())
}

@Test
fun `sets Span data as data in SentrySpan`() {
val transaction = fixture.getSut(sampled = true)
val span = transaction.startChild("op")
span.setData("key", "val")
span.finish()
transaction.finish()
verify(fixture.hub).captureTransaction(check {
assertNotNull(it.spans.first().data) {
assertEquals("val", it["key"])
}
}, anyOrNull())
}
}