Skip to content

Commit

Permalink
Merge pull request #1544 from newrelic/spring6-pointcut
Browse files Browse the repository at this point in the history
Spring 6 Jakarta point cuts
  • Loading branch information
jtduffy authored Oct 11, 2023
2 parents d8afa47 + f9f6132 commit 5a75a5e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
public final class TomcatServletRequestListener implements ServletRequestListener {

private static final String SERVLET_EXCEPTION_ATTRIBUTE_NAME = "jakarta.servlet.error.exception";
private static final String SPRING6_SERVLET_EXCEPTION_ATTRIBUTE_NAME = "org.springframework.web.servlet.DispatcherServlet.EXCEPTION";
private static final String REQUEST_FIELD = "request";

private final Field requestField;
Expand All @@ -45,7 +44,7 @@ private Field getRequestField() {
@CatchAndLog
@Override
public void requestDestroyed(ServletRequestEvent sre) {
Throwable exception = retrieveExceptionFromServlet(sre);
Throwable exception =(Throwable) sre.getServletRequest().getAttribute(SERVLET_EXCEPTION_ATTRIBUTE_NAME);
if (exception != null) {
AgentBridge.privateApi.reportException(exception);
}
Expand Down Expand Up @@ -98,12 +97,4 @@ private Request_Weaved getRequest(HttpServletRequest httpServletRequest) {
}
return null;
}

private Throwable retrieveExceptionFromServlet(ServletRequestEvent sre) {
//As of Spring 6, when controller advice is used for controller exception handling, the caught exception is
//now stored in the servlet attribute map with the "org.springframework.web.servlet.DispatcherServlet.EXCEPTION" key.
return (Throwable) (sre.getServletRequest().getAttribute(SERVLET_EXCEPTION_ATTRIBUTE_NAME) != null ?
sre.getServletRequest().getAttribute(SERVLET_EXCEPTION_ATTRIBUTE_NAME) :
sre.getServletRequest().getAttribute(SPRING6_SERVLET_EXCEPTION_ATTRIBUTE_NAME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public HandleInternalInvokerPointCut(PointCutClassTransformer classTransformer)
"(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Lorg/springframework/web/method/HandlerMethod;)Lorg/springframework/web/servlet/ModelAndView;"),
createExactMethodMatcher(
"invokeHandlerMethod",
"(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Lorg/springframework/web/method/HandlerMethod;)Lorg/springframework/web/servlet/ModelAndView;")));
"(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Lorg/springframework/web/method/HandlerMethod;)Lorg/springframework/web/servlet/ModelAndView;"),
createExactMethodMatcher(
"invokeHandleMethod",
"(Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Lorg/springframework/web/method/HandlerMethod;)Lorg/springframework/web/servlet/ModelAndView;"),
createExactMethodMatcher(
"invokeHandlerMethod",
"(Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Lorg/springframework/web/method/HandlerMethod;)Lorg/springframework/web/servlet/ModelAndView;")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ public HandlerInterceptorPointCut(PointCutClassTransformer classTransformer) {
"(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljava/lang/Object;Lorg/springframework/web/servlet/ModelAndView;)V"),
new ExactMethodMatcher(
"afterCompletion",
"(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljava/lang/Object;Ljava/lang/Exception;)V")));
"(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljava/lang/Object;Ljava/lang/Exception;)V"),
new ExactMethodMatcher("preHandle",
"(Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljava/lang/Object;)Z"),
new ExactMethodMatcher(
"postHandle",
"(Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljava/lang/Object;Lorg/springframework/web/servlet/ModelAndView;)V"),
new ExactMethodMatcher(
"afterCompletion",
"(Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljava/lang/Object;Ljava/lang/Exception;)V")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ public SpringDispatcherPointCut(PointCutClassTransformer classTransformer) {
RENDER_METHOD_NAME,
"(Lorg/springframework/web/servlet/ModelAndView;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V"),
new ExactMethodMatcher("doDispatch",
"(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V")));
"(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V"),
new ExactMethodMatcher(
RENDER_METHOD_NAME,
"(Lorg/springframework/web/servlet/ModelAndView;Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;)V"),
new ExactMethodMatcher("doDispatch",
"(Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;)V")));
}

@Override
public Tracer doGetTracer(Transaction transaction, ClassMethodSignature sig, Object dispatcher, Object[] args) {
if (RENDER_METHOD_NAME == sig.getMethodName()) {
if (RENDER_METHOD_NAME.equals(sig.getMethodName())) {
StringBuilder metricName = new StringBuilder("SpringView");
if (canSetTransactionName(transaction)) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ public SpringExceptionHandlerPointCut(PointCutClassTransformer classTransformer)
"(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljava/lang/Object;Ljava/lang/Exception;)Lorg/springframework/web/servlet/ModelAndView;"),
new ExactMethodMatcher(
"triggerAfterCompletion",
"(Lorg/springframework/web/servlet/HandlerExecutionChain;ILjavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljava/lang/Exception;)V")));
"(Lorg/springframework/web/servlet/HandlerExecutionChain;ILjavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljava/lang/Exception;)V"),
new ExactMethodMatcher(
PROCESS_HANDLER_EXCEPTION_METHOD_NAME,
"(Ljakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljava/lang/Object;Ljava/lang/Exception;)Lorg/springframework/web/servlet/ModelAndView;"),
new ExactMethodMatcher(
"triggerAfterCompletion",
"(Lorg/springframework/web/servlet/HandlerExecutionChain;ILjakarta/servlet/http/HttpServletRequest;Ljakarta/servlet/http/HttpServletResponse;Ljava/lang/Exception;)V")));
}

@Override
Expand Down

0 comments on commit 5a75a5e

Please sign in to comment.