Skip to content

Commit

Permalink
Add overload for transaction/span.finish(SpanStatus). (#1182)
Browse files Browse the repository at this point in the history
Fixes #1180
  • Loading branch information
maciejwalkowiak committed Jan 18, 2021
1 parent bba0c16 commit 9e98134
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Enchancement: Support SENTRY_TRACES_SAMPLE_RATE conf. via env variables (#1171)
* Enhancement: Pass request to CustomSamplingContext in Spring integration (#1172)
* Fix: Free Local Refs manually due to Android local ref. count limits
* Enhancement: Add overload for `transaction/span.finish(SpanStatus)` (#1182)

# 4.0.0-alpha.3

Expand Down
4 changes: 4 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ 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 getSpanContext ()Lio/sentry/SpanContext;
public abstract fun getThrowable ()Ljava/lang/Throwable;
public abstract fun setDescription (Ljava/lang/String;)V
Expand Down Expand Up @@ -343,6 +344,7 @@ public final class io/sentry/NoOpLogger : io/sentry/ILogger {
public final class io/sentry/NoOpTransaction : io/sentry/ITransaction {
public fun <init> ()V
public fun finish ()V
public fun finish (Lio/sentry/SpanStatus;)V
public fun getContexts ()Lio/sentry/protocol/Contexts;
public fun getDescription ()Ljava/lang/String;
public fun getEventId ()Lio/sentry/protocol/SentryId;
Expand Down Expand Up @@ -811,6 +813,7 @@ public final class io/sentry/SentryTraceHeader {
public final class io/sentry/SentryTransaction : io/sentry/SentryBaseEvent, io/sentry/ITransaction {
public fun <init> (Ljava/lang/String;Lio/sentry/SpanContext;Lio/sentry/IHub;)V
public fun finish ()V
public fun finish (Lio/sentry/SpanStatus;)V
public fun getDescription ()Ljava/lang/String;
public fun getLatestActiveSpan ()Lio/sentry/Span;
public fun getSpanContext ()Lio/sentry/SpanContext;
Expand Down Expand Up @@ -877,6 +880,7 @@ public final class io/sentry/ShutdownHookIntegration : io/sentry/Integration {

public final class io/sentry/Span : io/sentry/SpanContext, io/sentry/ISpan {
public fun finish ()V
public fun finish (Lio/sentry/SpanStatus;)V
public fun getSpanContext ()Lio/sentry/SpanContext;
public fun getStartTimestamp ()Ljava/util/Date;
public fun getThrowable ()Ljava/lang/Throwable;
Expand Down
7 changes: 7 additions & 0 deletions sentry/src/main/java/io/sentry/ISpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public interface ISpan {
/** Sets span timestamp marking this span as finished. */
void finish();

/**
* Sets span timestamp marking this span as finished.
*
* @param status - the status
*/
void finish(@Nullable SpanStatus status);

/**
* Sets span operation.
*
Expand Down
3 changes: 3 additions & 0 deletions sentry/src/main/java/io/sentry/NoOpTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public void setRequest(@Nullable Request request) {}
@Override
public void finish() {}

@Override
public void finish(@Nullable SpanStatus status) {}

@Override
public void setOperation(@Nullable String operation) {}

Expand Down
11 changes: 11 additions & 0 deletions sentry/src/main/java/io/sentry/SentryTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ public void finish() {
this.hub.captureTransaction(this, null);
}

@Override
public void finish(@Nullable SpanStatus status) {
this.setStatus(status);
this.finish();
}

/**
* Sets transaction operation.
*
Expand Down Expand Up @@ -220,6 +226,11 @@ Date getTimestamp() {
return timestamp;
}

@Nullable
SpanStatus getStatus() {
return this.getContexts().getTrace().getStatus();
}

@Override
@TestOnly
public @NotNull List<Span> getSpans() {
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public void finish() {
}
}

@Override
public void finish(@Nullable SpanStatus status) {
this.status = status;
this.finish();
}

@Override
public @NotNull SpanContext getSpanContext() {
return this;
Expand Down
8 changes: 8 additions & 0 deletions sentry/src/test/java/io/sentry/SentryTransactionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class SentryTransactionTest {
assertNotNull(transaction.timestamp)
}

@Test
fun `when transaction is finished with status, timestamp and status are set`() {
val transaction = SentryTransaction("name")
transaction.finish(SpanStatus.ABORTED)
assertNotNull(transaction.timestamp)
assertEquals(SpanStatus.ABORTED, transaction.status)
}

@Test
fun `when transaction is finished, transaction is captured`() {
val hub = mock<IHub>()
Expand Down
8 changes: 8 additions & 0 deletions sentry/src/test/java/io/sentry/SpanTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class SpanTest {
assertNotNull(span.timestamp)
}

@Test
fun `finishing span with status sets the timestamp and status`() {
val span = Span(SentryId(), SpanId(), SentryTransaction("name"), mock())
span.finish(SpanStatus.CANCELLED)
assertNotNull(span.timestamp)
assertEquals(SpanStatus.CANCELLED, span.status)
}

@Test
fun `starting a child sets parent span id`() {
val span = Span(SentryId(), SpanId(), SentryTransaction("name"), mock())
Expand Down

0 comments on commit 9e98134

Please sign in to comment.