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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP response code to Spring WebFlux transactions #2870

Merged
merged 5 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,17 @@ private void finishTransaction(ServerWebExchange exchange, ITransaction transact
transaction.setName(transactionName, TransactionNameSource.ROUTE);
transaction.setOperation(TRANSACTION_OP);
}
if (transaction.getStatus() == null) {
final @Nullable ServerHttpResponse response = exchange.getResponse();
if (response != null) {
final @Nullable HttpStatusCode statusCode = response.getStatusCode();
if (statusCode != null) {
final @Nullable ServerHttpResponse response = exchange.getResponse();
if (response != null) {
final @Nullable HttpStatusCode statusCode = response.getStatusCode();
if (statusCode != null) {
transaction
.getContexts()
.withResponse(
(sentryResponse) -> {
sentryResponse.setStatusCode(statusCode.value());
});
if (transaction.getStatus() == null) {
transaction.setStatus(SpanStatus.fromHttpStatusCode(statusCode.value()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class SentryWebFluxTracingFilterTest {
assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.OK)
assertThat(it.contexts.trace!!.operation).isEqualTo("http.server")
assertThat(it.contexts.trace!!.origin).isEqualTo("auto.spring_jakarta.webflux")
assertThat(it.contexts.response!!.statusCode).isEqualTo(200)
},
anyOrNull<TraceContext>(),
anyOrNull(),
Expand All @@ -133,6 +134,7 @@ class SentryWebFluxTracingFilterTest {
verify(fixture.hub).captureTransaction(
check {
assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.INTERNAL_ERROR)
assertThat(it.contexts.response!!.statusCode).isEqualTo(500)
},
anyOrNull<TraceContext>(),
anyOrNull(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,17 @@ private void finishTransaction(ServerWebExchange exchange, ITransaction transact
transaction.setName(transactionName, TransactionNameSource.ROUTE);
transaction.setOperation(TRANSACTION_OP);
}
if (transaction.getStatus() == null) {
final @Nullable ServerHttpResponse response = exchange.getResponse();
if (response != null) {
final @Nullable Integer rawStatusCode = response.getRawStatusCode();
if (rawStatusCode != null) {
final @Nullable ServerHttpResponse response = exchange.getResponse();
if (response != null) {
final @Nullable Integer rawStatusCode = response.getRawStatusCode();
if (rawStatusCode != null) {
transaction
.getContexts()
.withResponse(
(sentryResponse) -> {
sentryResponse.setStatusCode(rawStatusCode);
});
if (transaction.getStatus() == null) {
transaction.setStatus(SpanStatus.fromHttpStatusCode(rawStatusCode));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class SentryWebFluxTracingFilterTest {
assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.OK)
assertThat(it.contexts.trace!!.operation).isEqualTo("http.server")
assertThat(it.contexts.trace!!.origin).isEqualTo("auto.spring.webflux")
assertThat(it.contexts.response!!.statusCode).isEqualTo(200)
},
anyOrNull<TraceContext>(),
anyOrNull(),
Expand All @@ -134,6 +135,7 @@ class SentryWebFluxTracingFilterTest {
verify(fixture.hub).captureTransaction(
check {
assertThat(it.contexts.trace!!.status).isEqualTo(SpanStatus.INTERNAL_ERROR)
assertThat(it.contexts.response!!.statusCode).isEqualTo(500)
},
anyOrNull<TraceContext>(),
anyOrNull(),
Expand Down
1 change: 1 addition & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -3004,6 +3004,7 @@ public final class io/sentry/protocol/Contexts : java/util/concurrent/Concurrent
public fun setResponse (Lio/sentry/protocol/Response;)V
public fun setRuntime (Lio/sentry/protocol/SentryRuntime;)V
public fun setTrace (Lio/sentry/SpanContext;)V
public fun withResponse (Lio/sentry/util/HintUtils$SentryConsumer;)V
}

public final class io/sentry/protocol/Contexts$Deserializer : io/sentry/JsonDeserializer {
Expand Down
17 changes: 17 additions & 0 deletions sentry/src/main/java/io/sentry/protocol/Contexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.sentry.JsonSerializable;
import io.sentry.ObjectWriter;
import io.sentry.SpanContext;
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.IOException;
Expand All @@ -19,6 +20,9 @@
public final class Contexts extends ConcurrentHashMap<String, Object> implements JsonSerializable {
private static final long serialVersionUID = 252445813254943011L;

/** Response lock, Ops should be atomic */
private final @NotNull Object responseLock = new Object();

public Contexts() {}

public Contexts(final @NotNull Contexts contexts) {
Expand Down Expand Up @@ -115,6 +119,19 @@
return toContextType(Response.TYPE, Response.class);
}

public void withResponse(HintUtils.SentryConsumer<Response> callback) {
synchronized (responseLock) {
final @Nullable Response response = getResponse();

Check warning on line 124 in sentry/src/main/java/io/sentry/protocol/Contexts.java

View check run for this annotation

Codecov / codecov/patch

sentry/src/main/java/io/sentry/protocol/Contexts.java#L123-L124

Added lines #L123 - L124 were not covered by tests
if (response != null) {
callback.accept(response);

Check warning on line 126 in sentry/src/main/java/io/sentry/protocol/Contexts.java

View check run for this annotation

Codecov / codecov/patch

sentry/src/main/java/io/sentry/protocol/Contexts.java#L126

Added line #L126 was not covered by tests
} else {
final @NotNull Response newResponse = new Response();
setResponse(newResponse);
callback.accept(newResponse);

Check warning on line 130 in sentry/src/main/java/io/sentry/protocol/Contexts.java

View check run for this annotation

Codecov / codecov/patch

sentry/src/main/java/io/sentry/protocol/Contexts.java#L128-L130

Added lines #L128 - L130 were not covered by tests
}
}
}

Check warning on line 133 in sentry/src/main/java/io/sentry/protocol/Contexts.java

View check run for this annotation

Codecov / codecov/patch

sentry/src/main/java/io/sentry/protocol/Contexts.java#L132-L133

Added lines #L132 - L133 were not covered by tests

public void setResponse(final @NotNull Response response) {
this.put(Response.TYPE, response);
}
Expand Down