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 all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Add HTTP response code to Spring WebFlux transactions ([#2870](https://github.com/getsentry/sentry-java/pull/2870))
- Add `sampled` to Dynamic Sampling Context ([#2869](https://github.com/getsentry/sentry-java/pull/2869))

### Fixes
Expand Down
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 @@ -3009,6 +3009,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
21 changes: 20 additions & 1 deletion 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,8 +119,23 @@ public void setGpu(final @NotNull Gpu gpu) {
return toContextType(Response.TYPE, Response.class);
}

public void withResponse(HintUtils.SentryConsumer<Response> callback) {
synchronized (responseLock) {
final @Nullable Response response = getResponse();
if (response != null) {
callback.accept(response);
} else {
final @NotNull Response newResponse = new Response();
setResponse(newResponse);
callback.accept(newResponse);
}
}
}

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

// region json
Expand Down