|
3 | 3 | import static io.sentry.TypeCheckHint.WEBFLUX_FILTER_REQUEST;
|
4 | 4 | import static io.sentry.TypeCheckHint.WEBFLUX_FILTER_RESPONSE;
|
5 | 5 |
|
| 6 | +import io.sentry.Baggage; |
| 7 | +import io.sentry.BaggageHeader; |
6 | 8 | import io.sentry.Breadcrumb;
|
| 9 | +import io.sentry.CustomSamplingContext; |
7 | 10 | import io.sentry.Hint;
|
8 | 11 | import io.sentry.IHub;
|
| 12 | +import io.sentry.ITransaction; |
| 13 | +import io.sentry.NoOpHub; |
| 14 | +import io.sentry.Sentry; |
| 15 | +import io.sentry.SentryLevel; |
| 16 | +import io.sentry.SentryTraceHeader; |
| 17 | +import io.sentry.SpanStatus; |
| 18 | +import io.sentry.TransactionContext; |
| 19 | +import io.sentry.TransactionOptions; |
| 20 | +import io.sentry.exception.InvalidSentryTraceHeaderException; |
| 21 | +import io.sentry.protocol.TransactionNameSource; |
9 | 22 | import io.sentry.util.Objects;
|
| 23 | +import java.util.List; |
10 | 24 | import org.jetbrains.annotations.ApiStatus;
|
11 | 25 | import org.jetbrains.annotations.NotNull;
|
| 26 | +import org.jetbrains.annotations.Nullable; |
| 27 | +import org.springframework.http.HttpHeaders; |
| 28 | +import org.springframework.http.HttpMethod; |
| 29 | +import org.springframework.http.HttpStatusCode; |
12 | 30 | import org.springframework.http.server.reactive.ServerHttpRequest;
|
13 | 31 | import org.springframework.http.server.reactive.ServerHttpResponse;
|
14 | 32 | import org.springframework.web.server.ServerWebExchange;
|
|
19 | 37 | public abstract class AbstractSentryWebFilter implements WebFilter {
|
20 | 38 | private final @NotNull SentryRequestResolver sentryRequestResolver;
|
21 | 39 | public static final String SENTRY_HUB_KEY = "sentry-hub";
|
| 40 | + private static final String TRANSACTION_OP = "http.server"; |
22 | 41 |
|
23 | 42 | public AbstractSentryWebFilter(final @NotNull IHub hub) {
|
24 | 43 | Objects.requireNonNull(hub, "hub is required");
|
25 | 44 | this.sentryRequestResolver = new SentryRequestResolver(hub);
|
26 | 45 | }
|
27 | 46 |
|
28 |
| - protected void doFinally(final @NotNull IHub requestHub) { |
29 |
| - requestHub.popScope(); |
| 47 | + protected @Nullable ITransaction maybeStartTransaction( |
| 48 | + final @NotNull IHub requestHub, final @NotNull ServerHttpRequest request) { |
| 49 | + if (requestHub.isEnabled() |
| 50 | + && requestHub.getOptions().isTracingEnabled() |
| 51 | + && shouldTraceRequest(requestHub, request)) { |
| 52 | + return startTransaction(requestHub, request); |
| 53 | + } else { |
| 54 | + return null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + protected void doFinally( |
| 59 | + final @NotNull ServerWebExchange serverWebExchange, |
| 60 | + final @NotNull IHub requestHub, |
| 61 | + final @Nullable ITransaction transaction) { |
| 62 | + if (transaction != null) { |
| 63 | + finishTransaction(serverWebExchange, transaction); |
| 64 | + } |
| 65 | + if (requestHub.isEnabled()) { |
| 66 | + requestHub.popScope(); |
| 67 | + } |
| 68 | + Sentry.setCurrentHub(NoOpHub.getInstance()); |
30 | 69 | }
|
31 | 70 |
|
32 | 71 | protected void doFirst(
|
33 | 72 | final @NotNull ServerWebExchange serverWebExchange, final @NotNull IHub requestHub) {
|
34 |
| - serverWebExchange.getAttributes().put(SENTRY_HUB_KEY, requestHub); |
35 |
| - requestHub.pushScope(); |
36 |
| - final ServerHttpRequest request = serverWebExchange.getRequest(); |
37 |
| - final ServerHttpResponse response = serverWebExchange.getResponse(); |
38 |
| - |
39 |
| - final Hint hint = new Hint(); |
40 |
| - hint.set(WEBFLUX_FILTER_REQUEST, request); |
41 |
| - hint.set(WEBFLUX_FILTER_RESPONSE, response); |
42 |
| - final String methodName = request.getMethod() != null ? request.getMethod().name() : "unknown"; |
43 |
| - requestHub.addBreadcrumb(Breadcrumb.http(request.getURI().toString(), methodName), hint); |
44 |
| - requestHub.configureScope( |
45 |
| - scope -> scope.setRequest(sentryRequestResolver.resolveSentryRequest(request))); |
| 73 | + if (requestHub.isEnabled()) { |
| 74 | + serverWebExchange.getAttributes().put(SENTRY_HUB_KEY, requestHub); |
| 75 | + requestHub.pushScope(); |
| 76 | + final ServerHttpRequest request = serverWebExchange.getRequest(); |
| 77 | + final ServerHttpResponse response = serverWebExchange.getResponse(); |
| 78 | + |
| 79 | + final Hint hint = new Hint(); |
| 80 | + hint.set(WEBFLUX_FILTER_REQUEST, request); |
| 81 | + hint.set(WEBFLUX_FILTER_RESPONSE, response); |
| 82 | + final String methodName = |
| 83 | + request.getMethod() != null ? request.getMethod().name() : "unknown"; |
| 84 | + requestHub.addBreadcrumb(Breadcrumb.http(request.getURI().toString(), methodName), hint); |
| 85 | + requestHub.configureScope( |
| 86 | + scope -> scope.setRequest(sentryRequestResolver.resolveSentryRequest(request))); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + protected void doOnError(final @Nullable ITransaction transaction, final @NotNull Throwable e) { |
| 91 | + if (transaction != null) { |
| 92 | + transaction.setStatus(SpanStatus.INTERNAL_ERROR); |
| 93 | + transaction.setThrowable(e); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + protected boolean shouldTraceRequest( |
| 98 | + final @NotNull IHub hub, final @NotNull ServerHttpRequest request) { |
| 99 | + return hub.getOptions().isTraceOptionsRequests() |
| 100 | + || !HttpMethod.OPTIONS.equals(request.getMethod()); |
| 101 | + } |
| 102 | + |
| 103 | + private void finishTransaction(ServerWebExchange exchange, ITransaction transaction) { |
| 104 | + String transactionName = TransactionNameProvider.provideTransactionName(exchange); |
| 105 | + if (transactionName != null) { |
| 106 | + transaction.setName(transactionName, TransactionNameSource.ROUTE); |
| 107 | + transaction.setOperation(TRANSACTION_OP); |
| 108 | + } |
| 109 | + if (transaction.getStatus() == null) { |
| 110 | + final @Nullable ServerHttpResponse response = exchange.getResponse(); |
| 111 | + if (response != null) { |
| 112 | + final @Nullable HttpStatusCode statusCode = response.getStatusCode(); |
| 113 | + if (statusCode != null) { |
| 114 | + transaction.setStatus(SpanStatus.fromHttpStatusCode(statusCode.value())); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + transaction.finish(); |
| 119 | + } |
| 120 | + |
| 121 | + protected @NotNull ITransaction startTransaction( |
| 122 | + final @NotNull IHub hub, final @NotNull ServerHttpRequest request) { |
| 123 | + final @NotNull HttpHeaders headers = request.getHeaders(); |
| 124 | + final @Nullable List<String> sentryTraceHeaders = |
| 125 | + headers.get(SentryTraceHeader.SENTRY_TRACE_HEADER); |
| 126 | + final @Nullable List<String> baggageHeaders = headers.get(BaggageHeader.BAGGAGE_HEADER); |
| 127 | + final @NotNull String name = request.getMethod() + " " + request.getURI().getPath(); |
| 128 | + final @NotNull CustomSamplingContext customSamplingContext = new CustomSamplingContext(); |
| 129 | + customSamplingContext.set("request", request); |
| 130 | + |
| 131 | + final TransactionOptions transactionOptions = new TransactionOptions(); |
| 132 | + transactionOptions.setCustomSamplingContext(customSamplingContext); |
| 133 | + transactionOptions.setBindToScope(true); |
| 134 | + |
| 135 | + if (sentryTraceHeaders != null && sentryTraceHeaders.size() > 0) { |
| 136 | + final @NotNull Baggage baggage = |
| 137 | + Baggage.fromHeader(baggageHeaders, hub.getOptions().getLogger()); |
| 138 | + try { |
| 139 | + final @NotNull TransactionContext contexts = |
| 140 | + TransactionContext.fromSentryTrace( |
| 141 | + name, |
| 142 | + TransactionNameSource.URL, |
| 143 | + TRANSACTION_OP, |
| 144 | + new SentryTraceHeader(sentryTraceHeaders.get(0)), |
| 145 | + baggage, |
| 146 | + null); |
| 147 | + |
| 148 | + return hub.startTransaction(contexts, transactionOptions); |
| 149 | + } catch (InvalidSentryTraceHeaderException e) { |
| 150 | + hub.getOptions() |
| 151 | + .getLogger() |
| 152 | + .log(SentryLevel.DEBUG, e, "Failed to parse Sentry trace header: %s", e.getMessage()); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return hub.startTransaction( |
| 157 | + new TransactionContext(name, TransactionNameSource.URL, TRANSACTION_OP), |
| 158 | + transactionOptions); |
46 | 159 | }
|
47 | 160 | }
|
0 commit comments