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

Fix: If transaction or span is finished, do not allow to mutate #1940

Merged
merged 2 commits into from
Mar 8, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Fix: If transaction or span is finished, do not allow to mutate (#1940)

## 5.6.2

* Ref: Make ActivityFramesTracker public to be used by Hybrid SDKs (#1931)
Expand Down
40 changes: 40 additions & 0 deletions sentry/src/main/java/io/sentry/SentryTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ private ISpan createChild(
final @NotNull String operation,
final @Nullable String description,
@Nullable Date timestamp) {
if (root.isFinished()) {
return NoOpSpan.getInstance();
}

Objects.requireNonNull(parentSpanId, "parentSpanId is required");
Objects.requireNonNull(operation, "operation is required");
final Span span =
Expand Down Expand Up @@ -179,6 +183,10 @@ private ISpan createChild(
final @NotNull String operation,
final @Nullable String description,
@Nullable Date timestamp) {
if (root.isFinished()) {
return NoOpSpan.getInstance();
}

if (children.size() < hub.getOptions().getMaxSpans()) {
return root.startChild(operation, description, timestamp);
} else {
Expand Down Expand Up @@ -288,6 +296,10 @@ private boolean hasAllChildrenFinished() {

@Override
public void setOperation(final @NotNull String operation) {
if (root.isFinished()) {
return;
}

this.root.setOperation(operation);
}

Expand All @@ -298,6 +310,10 @@ public void setOperation(final @NotNull String operation) {

@Override
public void setDescription(final @Nullable String description) {
if (root.isFinished()) {
return;
}

this.root.setDescription(description);
}

Expand All @@ -308,6 +324,10 @@ public void setDescription(final @Nullable String description) {

@Override
public void setStatus(final @Nullable SpanStatus status) {
if (root.isFinished()) {
return;
}

this.root.setStatus(status);
}

Expand All @@ -318,6 +338,10 @@ public void setStatus(final @Nullable SpanStatus status) {

@Override
public void setThrowable(final @Nullable Throwable throwable) {
if (root.isFinished()) {
return;
}

this.root.setThrowable(throwable);
}

Expand All @@ -333,6 +357,10 @@ public void setThrowable(final @Nullable Throwable throwable) {

@Override
public void setTag(final @NotNull String key, final @NotNull String value) {
if (root.isFinished()) {
return;
}

this.root.setTag(key, value);
}

Expand All @@ -348,6 +376,10 @@ public boolean isFinished() {

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

this.root.setData(key, value);
}

Expand All @@ -367,6 +399,10 @@ public void setData(@NotNull String key, @NotNull Object value) {

@Override
public void setName(@NotNull String name) {
if (root.isFinished()) {
return;
}

this.name = name;
}

Expand All @@ -379,6 +415,10 @@ public void setName(@NotNull String name) {
@Deprecated
@ApiStatus.ScheduledForRemoval
public void setRequest(final @Nullable Request request) {
if (root.isFinished()) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we log a warning? It seems like an incorrect instrumentation

}

this.request = request;
}

Expand Down
32 changes: 32 additions & 0 deletions sentry/src/main/java/io/sentry/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,20 @@ public Span(
final @NotNull String operation,
final @Nullable String description,
final @Nullable Date timestamp) {
if (finished.get()) {
return NoOpSpan.getInstance();
}

return transaction.startChild(context.getSpanId(), operation, description, timestamp);
}

@Override
public @NotNull ISpan startChild(
final @NotNull String operation, final @Nullable String description) {
if (finished.get()) {
return NoOpSpan.getInstance();
}

return transaction.startChild(context.getSpanId(), operation, description);
}

Expand Down Expand Up @@ -152,6 +160,10 @@ void finish(@Nullable SpanStatus status, Date timestamp) {

@Override
public void setOperation(final @NotNull String operation) {
if (finished.get()) {
return;
}

this.context.setOperation(operation);
}

Expand All @@ -162,6 +174,10 @@ public void setOperation(final @NotNull String operation) {

@Override
public void setDescription(final @Nullable String description) {
if (finished.get()) {
return;
}

this.context.setDescription(description);
}

Expand All @@ -172,6 +188,10 @@ public void setDescription(final @Nullable String description) {

@Override
public void setStatus(final @Nullable SpanStatus status) {
if (finished.get()) {
return;
}

this.context.setStatus(status);
}

Expand All @@ -187,6 +207,10 @@ public void setStatus(final @Nullable SpanStatus status) {

@Override
public void setTag(final @NotNull String key, final @NotNull String value) {
if (finished.get()) {
return;
}

this.context.setTag(key, value);
}

Expand All @@ -210,6 +234,10 @@ public boolean isFinished() {

@Override
public void setThrowable(final @Nullable Throwable throwable) {
if (finished.get()) {
return;
}

this.throwable = throwable;
}

Expand Down Expand Up @@ -237,6 +265,10 @@ public Map<String, String> getTags() {

@Override
public void setData(@NotNull String key, @NotNull Object value) {
if (finished.get()) {
return;
}

data.put(key, value);
}

Expand Down
36 changes: 36 additions & 0 deletions sentry/src/test/java/io/sentry/SentryTracerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,42 @@ class SentryTracerTest {
assertTrue(transaction.isFinished)
}

@Test
fun `when span is finished, do nothing`() {
val transaction = fixture.getSut()
transaction.description = "desc"
transaction.setTag("myTag", "myValue")
transaction.setData("myData", "myValue")
val ex = RuntimeException()
transaction.throwable = ex
val req = Request()
transaction.request = req

transaction.finish(SpanStatus.OK)
assertTrue(transaction.isFinished)

assertEquals(NoOpSpan.getInstance(), transaction.startChild("op", "desc"))

transaction.finish(SpanStatus.UNKNOWN_ERROR)
transaction.operation = "newOp"
transaction.description = "newDesc"
transaction.status = SpanStatus.ABORTED
transaction.setTag("myTag", "myNewValue")
transaction.throwable = RuntimeException()
transaction.setData("myData", "myNewValue")
transaction.name = "newName"
transaction.request = Request()

assertEquals(SpanStatus.OK, transaction.status)
assertEquals("op", transaction.operation)
assertEquals("desc", transaction.description)
assertEquals("myValue", transaction.getTag("myTag"))
assertEquals("myValue", transaction.getData("myData"))
assertEquals("name", transaction.name)
assertEquals(req, transaction.request)
assertEquals(ex, transaction.throwable)
}

@Test
fun `when startTimestamp is given, use it as startTimestamp`() {
val date = Date(0)
Expand Down
31 changes: 31 additions & 0 deletions sentry/src/test/java/io/sentry/SpanTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,37 @@ class SpanTest {
assertEquals(timestamp, span.timestamp)
}

@Test
fun `when span is finished, do nothing`() {
val span = fixture.getSut()
span.description = "desc"
span.setTag("myTag", "myValue")
span.setData("myData", "myValue")
val ex = RuntimeException()
span.throwable = ex

span.finish(SpanStatus.OK)
assertTrue(span.isFinished)

assertEquals(NoOpSpan.getInstance(), span.startChild("op", "desc", null))
assertEquals(NoOpSpan.getInstance(), span.startChild("op", "desc"))

span.finish(SpanStatus.UNKNOWN_ERROR)
span.operation = "newOp"
span.description = "newDesc"
span.status = SpanStatus.ABORTED
span.setTag("myTag", "myNewValue")
span.throwable = RuntimeException()
span.setData("myData", "myNewValue")

assertEquals(SpanStatus.OK, span.status)
assertEquals("op", span.operation)
assertEquals("desc", span.description)
assertEquals("myValue", span.tags["myTag"])
assertEquals("myValue", span.data["myData"])
assertEquals(ex, span.throwable)
}

@Test
fun `child trace state is equal to transaction trace state`() {
val transaction = getTransaction()
Expand Down