1818
1919package org .apache .skywalking .apm .plugin .spring .mvc .commons .interceptor ;
2020
21+ import java .lang .reflect .Method ;
22+ import javax .servlet .http .HttpServletRequest ;
23+ import javax .servlet .http .HttpServletResponse ;
2124import org .apache .skywalking .apm .agent .core .context .CarrierItem ;
2225import org .apache .skywalking .apm .agent .core .context .ContextCarrier ;
2326import org .apache .skywalking .apm .agent .core .context .ContextManager ;
3437import org .apache .skywalking .apm .agent .core .util .MethodUtil ;
3538import org .apache .skywalking .apm .network .trace .component .ComponentsDefine ;
3639import org .apache .skywalking .apm .plugin .spring .mvc .commons .EnhanceRequireObjectCache ;
40+ import org .apache .skywalking .apm .plugin .spring .mvc .commons .HttpServletRequestWrapper ;
41+ import org .apache .skywalking .apm .plugin .spring .mvc .commons .HttpServletRequestWrappers ;
3742import org .apache .skywalking .apm .plugin .spring .mvc .commons .RequestUtil ;
3843import org .apache .skywalking .apm .plugin .spring .mvc .commons .SpringMVCPluginConfig ;
3944import org .apache .skywalking .apm .plugin .spring .mvc .commons .exception .IllegalMethodStackDepthException ;
4045import org .apache .skywalking .apm .plugin .spring .mvc .commons .exception .ServletResponseNotFoundException ;
4146import org .springframework .http .server .reactive .ServerHttpRequest ;
4247import org .springframework .http .server .reactive .ServerHttpResponse ;
4348
44- import javax .servlet .http .HttpServletRequest ;
45- import javax .servlet .http .HttpServletResponse ;
46- import java .lang .reflect .Method ;
47-
4849import static org .apache .skywalking .apm .plugin .spring .mvc .commons .Constants .CONTROLLER_METHOD_STACK_DEPTH ;
4950import static org .apache .skywalking .apm .plugin .spring .mvc .commons .Constants .FORWARD_REQUEST_FLAG ;
5051import static org .apache .skywalking .apm .plugin .spring .mvc .commons .Constants .REACTIVE_ASYNC_SPAN_IN_RUNTIME_CONTEXT ;
@@ -68,10 +69,11 @@ public abstract class AbstractMethodInterceptor implements InstanceMethodsAround
6869
6970 static {
7071 IS_SERVLET_GET_STATUS_METHOD_EXIST = MethodUtil .isMethodExist (
71- AbstractMethodInterceptor .class .getClassLoader (), SERVLET_RESPONSE_CLASS , GET_STATUS_METHOD );
72+ AbstractMethodInterceptor .class .getClassLoader (), SERVLET_RESPONSE_CLASS , GET_STATUS_METHOD );
7273 IS_JAKARTA_SERVLET_GET_STATUS_METHOD_EXIST = MethodUtil .isMethodExist (
73- AbstractMethodInterceptor .class .getClassLoader (),
74- JAKARTA_SERVLET_RESPONSE_CLASS , GET_STATUS_METHOD );
74+ AbstractMethodInterceptor .class .getClassLoader (),
75+ JAKARTA_SERVLET_RESPONSE_CLASS , GET_STATUS_METHOD
76+ );
7577 try {
7678 Class .forName (SERVLET_RESPONSE_CLASS , true , AbstractMethodInterceptor .class .getClassLoader ());
7779 IN_SERVLET_CONTAINER = true ;
@@ -113,53 +115,15 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
113115
114116 if (IN_SERVLET_CONTAINER && IS_JAVAX && HttpServletRequest .class .isAssignableFrom (request .getClass ())) {
115117 final HttpServletRequest httpServletRequest = (HttpServletRequest ) request ;
116- CarrierItem next = contextCarrier .items ();
117- while (next .hasNext ()) {
118- next = next .next ();
119- next .setHeadValue (httpServletRequest .getHeader (next .getHeadKey ()));
120- }
121-
122- String operationName = this .buildOperationName (method , httpServletRequest .getMethod (),
123- (EnhanceRequireObjectCache ) objInst .getSkyWalkingDynamicField ());
124- AbstractSpan span = ContextManager .createEntrySpan (operationName , contextCarrier );
125- Tags .URL .set (span , httpServletRequest .getRequestURL ().toString ());
126- Tags .HTTP .METHOD .set (span , httpServletRequest .getMethod ());
127- span .setComponent (ComponentsDefine .SPRING_MVC_ANNOTATION );
128- SpanLayer .asHttp (span );
118+ handleBeforeMethod (
119+ objInst , method , HttpServletRequestWrappers .wrap (httpServletRequest ), contextCarrier );
129120
130- if (SpringMVCPluginConfig .Plugin .SpringMVC .COLLECT_HTTP_PARAMS ) {
131- RequestUtil .collectHttpParam (httpServletRequest , span );
132- }
133-
134- if (!CollectionUtil .isEmpty (SpringMVCPluginConfig .Plugin .Http .INCLUDE_HTTP_HEADERS )) {
135- RequestUtil .collectHttpHeaders (httpServletRequest , span );
136- }
137- } else if (IN_SERVLET_CONTAINER && IS_JAKARTA && jakarta .servlet .http .HttpServletRequest .class .isAssignableFrom (request .getClass ())) {
121+ } else if (IN_SERVLET_CONTAINER && IS_JAKARTA && jakarta .servlet .http .HttpServletRequest .class .isAssignableFrom (
122+ request .getClass ())) {
138123 final jakarta .servlet .http .HttpServletRequest httpServletRequest = (jakarta .servlet .http .HttpServletRequest ) request ;
139- CarrierItem next = contextCarrier .items ();
140- while (next .hasNext ()) {
141- next = next .next ();
142- next .setHeadValue (httpServletRequest .getHeader (next .getHeadKey ()));
143- }
144-
145- String operationName =
146- this .buildOperationName (method , httpServletRequest .getMethod (),
147- (EnhanceRequireObjectCache ) objInst .getSkyWalkingDynamicField ());
148- AbstractSpan span =
149- ContextManager .createEntrySpan (operationName , contextCarrier );
150- Tags .URL .set (span , httpServletRequest .getRequestURL ().toString ());
151- Tags .HTTP .METHOD .set (span , httpServletRequest .getMethod ());
152- span .setComponent (ComponentsDefine .SPRING_MVC_ANNOTATION );
153- SpanLayer .asHttp (span );
154-
155- if (SpringMVCPluginConfig .Plugin .SpringMVC .COLLECT_HTTP_PARAMS ) {
156- RequestUtil .collectHttpParam (httpServletRequest , span );
157- }
124+ handleBeforeMethod (
125+ objInst , method , HttpServletRequestWrappers .wrap (httpServletRequest ), contextCarrier );
158126
159- if (!CollectionUtil
160- .isEmpty (SpringMVCPluginConfig .Plugin .Http .INCLUDE_HTTP_HEADERS )) {
161- RequestUtil .collectHttpHeaders (httpServletRequest , span );
162- }
163127 } else if (ServerHttpRequest .class .isAssignableFrom (request .getClass ())) {
164128 final ServerHttpRequest serverHttpRequest = (ServerHttpRequest ) request ;
165129 CarrierItem next = contextCarrier .items ();
@@ -168,8 +132,10 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
168132 next .setHeadValue (serverHttpRequest .getHeaders ().getFirst (next .getHeadKey ()));
169133 }
170134
171- String operationName = this .buildOperationName (method , serverHttpRequest .getMethod ().name (),
172- (EnhanceRequireObjectCache ) objInst .getSkyWalkingDynamicField ());
135+ String operationName = this .buildOperationName (
136+ method , serverHttpRequest .getMethod ().name (),
137+ (EnhanceRequireObjectCache ) objInst .getSkyWalkingDynamicField ()
138+ );
173139 AbstractSpan span = ContextManager .createEntrySpan (operationName , contextCarrier );
174140 Tags .URL .set (span , serverHttpRequest .getURI ().toString ());
175141 Tags .HTTP .METHOD .set (span , serverHttpRequest .getMethod ().name ());
@@ -198,6 +164,31 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
198164 }
199165 }
200166
167+ private void handleBeforeMethod (EnhancedInstance objInst , Method method ,
168+ HttpServletRequestWrapper httpServletRequest , ContextCarrier contextCarrier ) {
169+ CarrierItem next = contextCarrier .items ();
170+ while (next .hasNext ()) {
171+ next = next .next ();
172+ next .setHeadValue (httpServletRequest .getHeader (next .getHeadKey ()));
173+ }
174+
175+ String operationName = this .buildOperationName (
176+ method , httpServletRequest .getMethod (), (EnhanceRequireObjectCache ) objInst .getSkyWalkingDynamicField ());
177+ AbstractSpan span = ContextManager .createEntrySpan (operationName , contextCarrier );
178+ Tags .URL .set (span , httpServletRequest .getRequestURL ().toString ());
179+ Tags .HTTP .METHOD .set (span , httpServletRequest .getMethod ());
180+ span .setComponent (ComponentsDefine .SPRING_MVC_ANNOTATION );
181+ SpanLayer .asHttp (span );
182+
183+ if (SpringMVCPluginConfig .Plugin .SpringMVC .COLLECT_HTTP_PARAMS ) {
184+ RequestUtil .collectHttpParam (httpServletRequest , span );
185+ }
186+
187+ if (!CollectionUtil .isEmpty (SpringMVCPluginConfig .Plugin .Http .INCLUDE_HTTP_HEADERS )) {
188+ RequestUtil .collectHttpHeaders (httpServletRequest , span );
189+ }
190+ }
191+
201192 @ Override
202193 public Object afterMethod (EnhancedInstance objInst , Method method , Object [] allArguments , Class <?>[] argumentsTypes ,
203194 Object ret ) throws Throwable {
@@ -232,9 +223,11 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA
232223
233224 Integer statusCode = null ;
234225
235- if (IS_SERVLET_GET_STATUS_METHOD_EXIST && HttpServletResponse .class .isAssignableFrom (response .getClass ())) {
226+ if (IS_SERVLET_GET_STATUS_METHOD_EXIST && HttpServletResponse .class .isAssignableFrom (
227+ response .getClass ())) {
236228 statusCode = ((HttpServletResponse ) response ).getStatus ();
237- } else if (IS_JAKARTA_SERVLET_GET_STATUS_METHOD_EXIST && jakarta .servlet .http .HttpServletResponse .class .isAssignableFrom (response .getClass ())) {
229+ } else if (IS_JAKARTA_SERVLET_GET_STATUS_METHOD_EXIST && jakarta .servlet .http .HttpServletResponse .class .isAssignableFrom (
230+ response .getClass ())) {
238231 statusCode = ((jakarta .servlet .http .HttpServletResponse ) response ).getStatus ();
239232 } else if (ServerHttpResponse .class .isAssignableFrom (response .getClass ())) {
240233 if (IS_SERVLET_GET_STATUS_METHOD_EXIST || IS_JAKARTA_SERVLET_GET_STATUS_METHOD_EXIST ) {
@@ -263,7 +256,8 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA
263256 if (!SpringMVCPluginConfig .Plugin .SpringMVC .COLLECT_HTTP_PARAMS && span .isProfiling ()) {
264257 if (IS_JAVAX && HttpServletRequest .class .isAssignableFrom (request .getClass ())) {
265258 RequestUtil .collectHttpParam ((HttpServletRequest ) request , span );
266- } else if (IS_JAKARTA && jakarta .servlet .http .HttpServletRequest .class .isAssignableFrom (request .getClass ())) {
259+ } else if (IS_JAKARTA && jakarta .servlet .http .HttpServletRequest .class .isAssignableFrom (
260+ request .getClass ())) {
267261 RequestUtil .collectHttpParam ((jakarta .servlet .http .HttpServletRequest ) request , span );
268262 } else if (ServerHttpRequest .class .isAssignableFrom (request .getClass ())) {
269263 RequestUtil .collectHttpParam ((ServerHttpRequest ) request , span );
@@ -309,7 +303,7 @@ private String buildOperationName(Method method, String httpMethod, EnhanceRequi
309303 pathMappingCache .addPathMapping (method , requestURL );
310304 requestURL = pathMappingCache .findPathMapping (method );
311305 }
312- operationName = String .join (":" , httpMethod , requestURL );
306+ operationName = String .join (":" , httpMethod , requestURL );
313307 }
314308
315309 return operationName ;
0 commit comments