Skip to content

Commit

Permalink
Add support for async dispatch requests
Browse files Browse the repository at this point in the history
Keeps the transaction open until the response is committed.
  • Loading branch information
aesy committed Dec 12, 2024
1 parent e227906 commit 768f9ac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 Down Expand Up @@ -110,21 +115,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 @@ -67,6 +67,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 Down Expand Up @@ -109,21 +114,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

0 comments on commit 768f9ac

Please sign in to comment.