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

Add support for async dispatch requests #3983

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class SentryTracingFilter extends OncePerRequestFilter {
private static final String TRANSACTION_OP = "http.server";

private static final String TRACE_ORIGIN = "auto.http.spring_jakarta.webmvc";
private static final String TRANSACTION_ATTR = SentryTracingFilter.class.getName() + ".transaction";

private final @NotNull TransactionNameProvider transactionNameProvider;
private final @NotNull IHub hub;
Expand All @@ -52,6 +53,11 @@ public SentryTracingFilter() {
this(HubAdapter.getInstance());
}

@Override
protected boolean shouldNotFilterAsyncDispatch() {
return false;
}

/**
* Creates filter that resolves transaction name using transaction name provider given by
* parameter.
Expand All @@ -77,14 +83,8 @@ protected void doFilterInternal(
final @NotNull FilterChain filterChain)
throws ServletException, IOException {
if (hub.isEnabled()) {
final @Nullable String sentryTraceHeader =
httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER);
final @Nullable List<String> baggageHeader =
Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER));
final @Nullable TransactionContext transactionContext =
hub.continueTrace(sentryTraceHeader, baggageHeader);
if (hub.getOptions().isTracingEnabled() && shouldTraceRequest(httpRequest)) {
doFilterWithTransaction(httpRequest, httpResponse, filterChain, transactionContext);
doFilterWithTransaction(httpRequest, httpResponse, filterChain);
} else {
filterChain.doFilter(httpRequest, httpResponse);
}
Expand All @@ -96,12 +96,24 @@ protected void doFilterInternal(
private void doFilterWithTransaction(
HttpServletRequest httpRequest,
HttpServletResponse httpResponse,
FilterChain filterChain,
final @Nullable TransactionContext transactionContext)
FilterChain filterChain)
throws IOException, ServletException {
// at this stage we are not able to get real transaction name
final ITransaction transaction = startTransaction(httpRequest, transactionContext);
transaction.getSpanContext().setOrigin(TRACE_ORIGIN);
final ITransaction transaction;
if (isAsyncDispatch(httpRequest)) {
transaction = (ITransaction) httpRequest.getAttribute(TRANSACTION_ATTR);
} else {
final @Nullable String sentryTraceHeader =
httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER);
final @Nullable List<String> baggageHeader =
Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER));
final @Nullable TransactionContext transactionContext =
hub.continueTrace(sentryTraceHeader, baggageHeader);

// at this stage we are not able to get real transaction name
transaction = startTransaction(httpRequest, transactionContext);
transaction.getSpanContext().setOrigin(TRACE_ORIGIN);
httpRequest.setAttribute(TRANSACTION_ATTR, transaction);
}

try {
filterChain.doFilter(httpRequest, httpResponse);
Expand All @@ -110,21 +122,23 @@ private void doFilterWithTransaction(
transaction.setStatus(SpanStatus.INTERNAL_ERROR);
throw e;
} finally {
// after all filters run, templated path pattern is available in request attribute
final String transactionName = transactionNameProvider.provideTransactionName(httpRequest);
final TransactionNameSource transactionNameSource =
transactionNameProvider.provideTransactionSource();
// if transaction name is not resolved, the request has not been processed by a controller
// and we should not report it to Sentry
if (transactionName != null) {
transaction.setName(transactionName, transactionNameSource);
transaction.setOperation(TRANSACTION_OP);
// if exception has been thrown, transaction status is already set to INTERNAL_ERROR, and
// httpResponse.getStatus() returns 200.
if (transaction.getStatus() == null) {
transaction.setStatus(SpanStatus.fromHttpStatusCode(httpResponse.getStatus()));
if (!isAsyncStarted(httpRequest)) {
// after all filters run, templated path pattern is available in request attribute
final String transactionName = transactionNameProvider.provideTransactionName(httpRequest);
final TransactionNameSource transactionNameSource =
transactionNameProvider.provideTransactionSource();
// if transaction name is not resolved, the request has not been processed by a controller
// and we should not report it to Sentry
if (transactionName != null) {
transaction.setName(transactionName, transactionNameSource);
transaction.setOperation(TRANSACTION_OP);
// if exception has been thrown, transaction status is already set to INTERNAL_ERROR, and
// httpResponse.getStatus() returns 200.
if (transaction.getStatus() == null) {
transaction.setStatus(SpanStatus.fromHttpStatusCode(httpResponse.getStatus()));
}
transaction.finish();
}
transaction.finish();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SentryTracingFilter extends OncePerRequestFilter {
private static final String TRANSACTION_OP = "http.server";

private static final String TRACE_ORIGIN = "auto.http.spring.webmvc";
private static final String TRANSACTION_ATTR = SentryTracingFilter.class.getName() + ".transaction";

private final @NotNull TransactionNameProvider transactionNameProvider;
private final @NotNull IHub hub;
Expand Down Expand Up @@ -67,6 +68,11 @@ public SentryTracingFilter(final @NotNull IHub hub) {
this(hub, new SpringMvcTransactionNameProvider());
}

@Override
protected boolean shouldNotFilterAsyncDispatch() {
return false;
}

@Override
protected void doFilterInternal(
final @NotNull HttpServletRequest httpRequest,
Expand All @@ -75,15 +81,8 @@ protected void doFilterInternal(
throws ServletException, IOException {

if (hub.isEnabled()) {
final @Nullable String sentryTraceHeader =
httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER);
final @Nullable List<String> baggageHeader =
Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER));
final @Nullable TransactionContext transactionContext =
hub.continueTrace(sentryTraceHeader, baggageHeader);

if (hub.getOptions().isTracingEnabled() && shouldTraceRequest(httpRequest)) {
doFilterWithTransaction(httpRequest, httpResponse, filterChain, transactionContext);
doFilterWithTransaction(httpRequest, httpResponse, filterChain);
} else {
filterChain.doFilter(httpRequest, httpResponse);
}
Expand All @@ -95,12 +94,24 @@ protected void doFilterInternal(
private void doFilterWithTransaction(
HttpServletRequest httpRequest,
HttpServletResponse httpResponse,
FilterChain filterChain,
final @Nullable TransactionContext transactionContext)
FilterChain filterChain)
throws IOException, ServletException {
// at this stage we are not able to get real transaction name
final ITransaction transaction = startTransaction(httpRequest, transactionContext);
transaction.getSpanContext().setOrigin(TRACE_ORIGIN);
final ITransaction transaction;
if (isAsyncDispatch(httpRequest)) {
transaction = (ITransaction) httpRequest.getAttribute(TRANSACTION_ATTR);
} else {
final @Nullable String sentryTraceHeader =
httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER);
final @Nullable List<String> baggageHeader =
Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER));
final @Nullable TransactionContext transactionContext =
hub.continueTrace(sentryTraceHeader, baggageHeader);

// at this stage we are not able to get real transaction name
transaction = startTransaction(httpRequest, transactionContext);
transaction.getSpanContext().setOrigin(TRACE_ORIGIN);
httpRequest.setAttribute(TRANSACTION_ATTR, transaction);
}

try {
filterChain.doFilter(httpRequest, httpResponse);
Expand All @@ -109,21 +120,23 @@ private void doFilterWithTransaction(
transaction.setStatus(SpanStatus.INTERNAL_ERROR);
throw e;
} finally {
// after all filters run, templated path pattern is available in request attribute
final String transactionName = transactionNameProvider.provideTransactionName(httpRequest);
final TransactionNameSource transactionNameSource =
transactionNameProvider.provideTransactionSource();
// if transaction name is not resolved, the request has not been processed by a controller
// and we should not report it to Sentry
if (transactionName != null) {
transaction.setName(transactionName, transactionNameSource);
transaction.setOperation(TRANSACTION_OP);
// if exception has been thrown, transaction status is already set to INTERNAL_ERROR, and
// httpResponse.getStatus() returns 200.
if (transaction.getStatus() == null) {
transaction.setStatus(SpanStatus.fromHttpStatusCode(httpResponse.getStatus()));
if (!isAsyncStarted(httpRequest)) {
// after all filters run, templated path pattern is available in request attribute
final String transactionName = transactionNameProvider.provideTransactionName(httpRequest);
final TransactionNameSource transactionNameSource =
transactionNameProvider.provideTransactionSource();
// if transaction name is not resolved, the request has not been processed by a controller
// and we should not report it to Sentry
if (transactionName != null) {
transaction.setName(transactionName, transactionNameSource);
transaction.setOperation(TRANSACTION_OP);
// if exception has been thrown, transaction status is already set to INTERNAL_ERROR, and
// httpResponse.getStatus() returns 200.
if (transaction.getStatus() == null) {
transaction.setStatus(SpanStatus.fromHttpStatusCode(httpResponse.getStatus()));
}
transaction.finish();
}
transaction.finish();
}
}
}
Expand Down