diff --git a/agent-module/plugins/spring-stub/src/main/java/org/springframework/http/HttpRequest.java b/agent-module/plugins/spring-stub/src/main/java/org/springframework/http/HttpRequest.java new file mode 100644 index 000000000000..3b24277c0da9 --- /dev/null +++ b/agent-module/plugins/spring-stub/src/main/java/org/springframework/http/HttpRequest.java @@ -0,0 +1,30 @@ +/* + * Copyright 2024 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.http; + +import java.net.URI; + +// Stub +public interface HttpRequest extends HttpMessage { + + HttpMethod getMethod(); + + URI getURI(); + + // Deleted from version of spring 6.1 + String getMethodValue(); +} diff --git a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/SpringWebFluxPlugin.java b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/SpringWebFluxPlugin.java index c0711f0641a2..661c9dc1b176 100644 --- a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/SpringWebFluxPlugin.java +++ b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/SpringWebFluxPlugin.java @@ -239,7 +239,8 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, // Add attribute listener. final InstrumentMethod lookupHandlerMethod = target.getDeclaredMethod("lookupHandlerMethod", "org.springframework.web.server.ServerWebExchange"); if (lookupHandlerMethod != null) { - lookupHandlerMethod.addInterceptor(AbstractHandlerMethodMappingInterceptor.class, va(uriStatCollectMethod)); + final int springVersion = SpringVersion.getVersion(classLoader); + lookupHandlerMethod.addInterceptor(AbstractHandlerMethodMappingInterceptor.class, va(uriStatCollectMethod, springVersion)); } return target.toBytecode(); } @@ -259,7 +260,8 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, // Add attribute listener. final InstrumentMethod exposePathWithinMapping = target.getDeclaredMethod("lookupHandler", "org.springframework.http.server.PathContainer", "org.springframework.web.server.ServerWebExchange"); if (exposePathWithinMapping != null) { - exposePathWithinMapping.addInterceptor(AbstractUrlHandlerMappingInterceptor.class, va(uriStatCollectMethod)); + final int springVersion = SpringVersion.getVersion(classLoader); + exposePathWithinMapping.addInterceptor(AbstractUrlHandlerMappingInterceptor.class, va(uriStatCollectMethod, springVersion)); } return target.toBytecode(); } diff --git a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/AbstractHandlerMethodMappingInterceptor.java b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/AbstractHandlerMethodMappingInterceptor.java index 44c5ea1b9bf9..ee4fafda4c94 100644 --- a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/AbstractHandlerMethodMappingInterceptor.java +++ b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/AbstractHandlerMethodMappingInterceptor.java @@ -9,16 +9,20 @@ import com.navercorp.pinpoint.common.util.ArrayArgumentUtils; import com.navercorp.pinpoint.common.util.StringUtils; import com.navercorp.pinpoint.plugin.spring.webflux.SpringWebFluxConstants; +import com.navercorp.pinpoint.plugin.spring.webflux.interceptor.util.HttpMethodProvider; +import com.navercorp.pinpoint.plugin.spring.webflux.interceptor.util.HttpMethodProviderFactory; import org.springframework.web.server.ServerWebExchange; public class AbstractHandlerMethodMappingInterceptor implements AroundInterceptor { private final PluginLogger logger = PluginLogManager.getLogger(getClass()); private final TraceContext traceContext; private final Boolean uriStatCollectMethod; + private final HttpMethodProvider httpMethodProvider; - public AbstractHandlerMethodMappingInterceptor(final TraceContext traceContext, Boolean uriStatCollectMethod) { + public AbstractHandlerMethodMappingInterceptor(final TraceContext traceContext, Boolean uriStatCollectMethod, int springVersion) { this.traceContext = traceContext; this.uriStatCollectMethod = uriStatCollectMethod; + this.httpMethodProvider = HttpMethodProviderFactory.getHttpMethodProvider(springVersion); } @Override @@ -43,7 +47,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab } if (uriStatCollectMethod) { - final String method = webExchange.getRequest().getMethodValue(); + final String method = httpMethodProvider.getMethod(webExchange.getRequest()); if (StringUtils.hasLength(method)) { spanRecorder.recordUriHttpMethod(method); } diff --git a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/AbstractUrlHandlerMappingInterceptor.java b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/AbstractUrlHandlerMappingInterceptor.java index e1663a6d5500..235a515ad471 100644 --- a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/AbstractUrlHandlerMappingInterceptor.java +++ b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/AbstractUrlHandlerMappingInterceptor.java @@ -9,16 +9,20 @@ import com.navercorp.pinpoint.common.util.ArrayArgumentUtils; import com.navercorp.pinpoint.common.util.StringUtils; import com.navercorp.pinpoint.plugin.spring.webflux.SpringWebFluxConstants; +import com.navercorp.pinpoint.plugin.spring.webflux.interceptor.util.HttpMethodProvider; +import com.navercorp.pinpoint.plugin.spring.webflux.interceptor.util.HttpMethodProviderFactory; import org.springframework.web.server.ServerWebExchange; public class AbstractUrlHandlerMappingInterceptor implements AroundInterceptor { private final PluginLogger logger = PluginLogManager.getLogger(getClass()); private final TraceContext traceContext; private final Boolean uriStatCollectMethod; + private final HttpMethodProvider httpMethodProvider; - public AbstractUrlHandlerMappingInterceptor(TraceContext traceContext, Boolean uriStatCollectMethod) { + public AbstractUrlHandlerMappingInterceptor(TraceContext traceContext, Boolean uriStatCollectMethod, int springVersion) { this.traceContext = traceContext; this.uriStatCollectMethod = uriStatCollectMethod; + this.httpMethodProvider = HttpMethodProviderFactory.getHttpMethodProvider(springVersion); } @Override @@ -43,7 +47,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab } if (uriStatCollectMethod) { - final String method = webExchange.getRequest().getMethodValue(); + final String method = httpMethodProvider.getMethod(webExchange.getRequest()); if (StringUtils.hasLength(method)) { spanRecorder.recordUriHttpMethod(method); } diff --git a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/HttpMethodProvider.java b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/HttpMethodProvider.java new file mode 100644 index 000000000000..be27a43c6734 --- /dev/null +++ b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/HttpMethodProvider.java @@ -0,0 +1,20 @@ +/* + * Copyright 2024 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.plugin.spring.webflux.interceptor.util; + +public interface HttpMethodProvider { + String getMethod(Object target); +} diff --git a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/HttpMethodProviderFactory.java b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/HttpMethodProviderFactory.java new file mode 100644 index 000000000000..16364a318383 --- /dev/null +++ b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/HttpMethodProviderFactory.java @@ -0,0 +1,35 @@ +/* + * Copyright 2024 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.plugin.spring.webflux.interceptor.util; + +import com.navercorp.pinpoint.plugin.spring.webflux.SpringVersion; + +public class HttpMethodProviderFactory { + + public HttpMethodProviderFactory() { + } + + public static HttpMethodProvider getHttpMethodProvider(int version) { + switch (version) { + case SpringVersion.SPRING_VERSION_5: + return new Spring5HttpMethodProvider(); + case SpringVersion.SPRING_VERSION_6: + return new Spring6HttpMethodProvider(); + default: + return new UnsupportedHttpMethodProvider(); + } + } +} diff --git a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/Spring5HttpMethodProvider.java b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/Spring5HttpMethodProvider.java new file mode 100644 index 000000000000..6c225a92af97 --- /dev/null +++ b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/Spring5HttpMethodProvider.java @@ -0,0 +1,32 @@ +/* + * Copyright 2024 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.plugin.spring.webflux.interceptor.util; + +import org.springframework.http.HttpRequest; + +public class Spring5HttpMethodProvider implements HttpMethodProvider { + @Override + public String getMethod(Object target) { + if (target instanceof HttpRequest) { + final HttpRequest request = (HttpRequest) target; + try { + return request.getMethodValue(); + } catch (Exception ignored) { + } + } + return null; + } +} diff --git a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/Spring6HttpMethodProvider.java b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/Spring6HttpMethodProvider.java new file mode 100644 index 000000000000..4b1ac8429ba5 --- /dev/null +++ b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/Spring6HttpMethodProvider.java @@ -0,0 +1,32 @@ +/* + * Copyright 2024 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.plugin.spring.webflux.interceptor.util; + +import org.springframework.http.HttpRequest; + +public class Spring6HttpMethodProvider implements HttpMethodProvider { + @Override + public String getMethod(Object target) { + if (target instanceof HttpRequest) { + final HttpRequest request = (HttpRequest) target; + try { + return request.getMethod().name(); + } catch (Exception ignored) { + } + } + return null; + } +} diff --git a/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/UnsupportedHttpMethodProvider.java b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/UnsupportedHttpMethodProvider.java new file mode 100644 index 000000000000..37245b97148b --- /dev/null +++ b/agent-module/plugins/spring-webflux/src/main/java/com/navercorp/pinpoint/plugin/spring/webflux/interceptor/util/UnsupportedHttpMethodProvider.java @@ -0,0 +1,23 @@ +/* + * Copyright 2024 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.plugin.spring.webflux.interceptor.util; + +public class UnsupportedHttpMethodProvider implements HttpMethodProvider { + @Override + public String getMethod(Object target) { + return null; + } +}