Skip to content

Commit

Permalink
Open transaction only once in async request
Browse files Browse the repository at this point in the history
  • Loading branch information
aesy committed Dec 13, 2024
1 parent 768f9ac commit 653e1c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
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 = AsyncSentryTracingFilter.class.getName() + ".transaction";

private final @NotNull TransactionNameProvider transactionNameProvider;
private final @NotNull IHub hub;
Expand Down Expand Up @@ -104,9 +105,15 @@ private void doFilterWithTransaction(
FilterChain filterChain,
final @Nullable TransactionContext transactionContext)
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 {
// 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 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 = AsyncSentryTracingFilter.class.getName() + ".transaction";

private final @NotNull TransactionNameProvider transactionNameProvider;
private final @NotNull IHub hub;
Expand Down Expand Up @@ -103,9 +104,15 @@ private void doFilterWithTransaction(
FilterChain filterChain,
final @Nullable TransactionContext transactionContext)
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 {
// 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 Down

0 comments on commit 653e1c2

Please sign in to comment.