Skip to content

Commit

Permalink
[#11005] Update getMethod compatibility of spring-webflux
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim committed May 13, 2024
1 parent 579b8f5 commit 9a66857
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 9a66857

Please sign in to comment.