Skip to content

Commit

Permalink
[#9522] Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim committed Jan 9, 2023
1 parent 0a049cc commit 3ee6632
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.common.util.ArrayArgumentUtils;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.plugin.spring.webflux.SpringWebFluxConstants;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;

public class AbstractHandlerMethodMappingInterceptor implements AroundInterceptor {
private final PLogger logger = PLoggerFactory.getLogger(getClass());

private final TraceContext traceContext;

public AbstractHandlerMethodMappingInterceptor(final TraceContext traceContext) {
Expand All @@ -19,34 +22,28 @@ public AbstractHandlerMethodMappingInterceptor(final TraceContext traceContext)

@Override
public void before(Object target, Object[] args) {

}

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
final Trace trace = traceContext.currentRawTraceObject();
if (trace != null) {
ServerWebExchange webExchange = ArrayArgumentUtils.getArgument(args, 0, ServerWebExchange.class);
String uri = extractAttribute(webExchange, SpringWebFluxConstants.SPRING_WEBFLUX_DEFAULT_URI_ATTRIBUTE_KEYS);
if (uri != null) {
SpanRecorder spanRecorder = trace.getSpanRecorder();
spanRecorder.recordUriTemplate(uri, false);
}
if (trace == null) {
return;
}
}

private String extractAttribute(ServerWebExchange webExchange, String[] keys) {
for (String attributeName : keys) {
Object uriMapping = webExchange.getAttribute(attributeName);
if (!(uriMapping instanceof PathPattern)) {
continue;
try {
final ServerWebExchange webExchange = ArrayArgumentUtils.getArgument(args, 0, ServerWebExchange.class);
if (webExchange != null) {
final String uri = ServerWebExchangeAttributeUtils.extractAttribute(webExchange, SpringWebFluxConstants.SPRING_WEBFLUX_DEFAULT_URI_ATTRIBUTE_KEYS);
if (StringUtils.hasLength(uri)) {
final SpanRecorder spanRecorder = trace.getSpanRecorder();
spanRecorder.recordUriTemplate(uri, false);
}
}

String uriTemplate = ((PathPattern) uriMapping).getPatternString();
if (StringUtils.hasLength(uriTemplate)) {
return uriTemplate;
} catch (Throwable th) {
if (logger.isWarnEnabled()) {
logger.warn("AFTER error. Caused:{}", th.getMessage(), th);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.common.util.ArrayArgumentUtils;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.plugin.spring.webflux.SpringWebFluxConstants;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;

public class AbstractUrlHandlerMappingInterceptor implements AroundInterceptor {
private final PLogger logger = PLoggerFactory.getLogger(getClass());

private final TraceContext traceContext;

public AbstractUrlHandlerMappingInterceptor(TraceContext traceContext) {
Expand All @@ -19,35 +22,28 @@ public AbstractUrlHandlerMappingInterceptor(TraceContext traceContext) {

@Override
public void before(Object target, Object[] args) {

}

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
final Trace trace = traceContext.currentRawTraceObject();
if (trace != null) {
ServerWebExchange webExchange = ArrayArgumentUtils.getArgument(args, 1, ServerWebExchange.class);
String uri = extractAttribute(webExchange, SpringWebFluxConstants.SPRING_WEBFLUX_DEFAULT_URI_ATTRIBUTE_KEYS);
if (uri != null) {
SpanRecorder spanRecorder = trace.getSpanRecorder();
spanRecorder.recordUriTemplate(uri, false);
}
if (trace == null) {
return;
}
}

private String extractAttribute(ServerWebExchange webExchange, String[] keys) {
for (String attributeName : keys) {
Object uriMapping = webExchange.getAttribute(attributeName);
if (!(uriMapping instanceof PathPattern)) {
continue;
try {
final ServerWebExchange webExchange = ArrayArgumentUtils.getArgument(args, 1, ServerWebExchange.class);
if (webExchange != null) {
final String uri = ServerWebExchangeAttributeUtils.extractAttribute(webExchange, SpringWebFluxConstants.SPRING_WEBFLUX_DEFAULT_URI_ATTRIBUTE_KEYS);
if (StringUtils.hasLength(uri)) {
final SpanRecorder spanRecorder = trace.getSpanRecorder();
spanRecorder.recordUriTemplate(uri, false);
}
}

String uriTemplate = ((PathPattern) uriMapping).getPatternString();
if (StringUtils.hasLength(uriTemplate)) {
return uriTemplate;
} catch (Throwable th) {
if (logger.isWarnEnabled()) {
logger.warn("AFTER error. Caused:{}", th.getMessage(), th);
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,26 @@ public AsyncContext getAsyncContext(Object target, Object[] args) {
@Override
public void doInBeforeTrace(SpanEventRecorder recorder, AsyncContext asyncContext, Object target, Object[] args) {
if (uriStatEnable && uriStatUseUserInput) {
Trace trace = traceContext.currentRawTraceObject();
final Trace trace = traceContext.currentRawTraceObject();
if (trace == null) {
return;
}

ServerWebExchange exchange = ArrayArgumentUtils.getArgument(args, 0, ServerWebExchange.class);
for (String attributeName : SpringWebFluxConstants.SPRING_WEBFLUX_URI_USER_INPUT_ATTRIBUTE_KEYS) {
Object uriMapping = exchange.getAttribute(attributeName);
if (!(uriMapping instanceof String)) {
continue;
}

String uriTemplate = (String) uriMapping;
final ServerWebExchange exchange = ArrayArgumentUtils.getArgument(args, 0, ServerWebExchange.class);
if (exchange != null) {
for (String attributeName : SpringWebFluxConstants.SPRING_WEBFLUX_URI_USER_INPUT_ATTRIBUTE_KEYS) {
final Object uriMapping = exchange.getAttribute(attributeName);
if (!(uriMapping instanceof String)) {
continue;
}

if (StringUtils.hasLength(uriTemplate)) {
SpanRecorder spanRecorder = trace.getSpanRecorder();
spanRecorder.recordUriTemplate(uriTemplate, true);
final String uriTemplate = (String) uriMapping;
if (StringUtils.hasLength(uriTemplate)) {
final SpanRecorder spanRecorder = trace.getSpanRecorder();
spanRecorder.recordUriTemplate(uriTemplate, true);
}
}
}

}
}

Expand Down Expand Up @@ -106,17 +106,14 @@ public void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] a
if (result instanceof AsyncContextAccessor) {
((AsyncContextAccessor) (result))._$PINPOINT$_setAsyncContext(publisherAsyncContext);
if (isDebug) {
logger.debug("Set AsyncContext result={}", result);
logger.debug("Set AsyncContext to result. asyncContext={}", publisherAsyncContext);
}
}
}
}

private boolean validate(final Object[] args) {
if (ArrayUtils.isEmpty(args)) {
if (isDebug) {
logger.debug("Invalid args object. args={}.", args);
}
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2023 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;

import com.navercorp.pinpoint.common.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;

public class ServerWebExchangeAttributeUtils {

public static String extractAttribute(ServerWebExchange webExchange, String[] keys) {
for (String attributeName : keys) {
final Object uriMapping = webExchange.getAttribute(attributeName);
if (!(uriMapping instanceof PathPattern)) {
continue;
}

final String uriTemplate = ((PathPattern) uriMapping).getPatternString();
if (StringUtils.hasLength(uriTemplate)) {
return uriTemplate;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.common.util.ArrayArgumentUtils;
import com.navercorp.pinpoint.common.util.StringUtils;


public class ExposePathWithinMappingInterceptor implements AroundInterceptor {
private final PLogger logger = PLoggerFactory.getLogger(getClass());
private final TraceContext traceContext;


public ExposePathWithinMappingInterceptor(final TraceContext traceContext) {
this.traceContext = traceContext;
}
Expand All @@ -22,10 +25,20 @@ public void before(Object target, Object[] args) {
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
final Trace trace = traceContext.currentRawTraceObject();
if (trace != null) {
String url = ArrayArgumentUtils.getArgument(args, 0, String.class);
SpanRecorder spanRecorder = trace.getSpanRecorder();
spanRecorder.recordUriTemplate(url, false);
if (trace == null) {
return;
}

try {
final String url = ArrayArgumentUtils.getArgument(args, 0, String.class);
if (StringUtils.hasLength(url)) {
final SpanRecorder spanRecorder = trace.getSpanRecorder();
spanRecorder.recordUriTemplate(url);
}
} catch (Throwable th) {
if (logger.isWarnEnabled()) {
logger.warn("AFTER error. Caused:{}", th.getMessage(), th);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.common.util.ArrayArgumentUtils;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.plugin.spring.web.SpringWebMvcConstants;

import javax.servlet.ServletRequest;

public class LookupHandlerMethodInterceptor implements AroundInterceptor {
private final PLogger logger = PLoggerFactory.getLogger(getClass());
private final TraceContext traceContext;

public LookupHandlerMethodInterceptor(final TraceContext traceContext) {
Expand All @@ -24,26 +27,23 @@ public void before(Object target, Object[] args) {
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
final Trace trace = traceContext.currentRawTraceObject();
if (trace != null) {
ServletRequest request = ArrayArgumentUtils.getArgument(args, 1, ServletRequest.class);
String uri = extractAttribute(request, SpringWebMvcConstants.SPRING_MVC_DEFAULT_URI_ATTRIBUTE_KEYS);
if (uri != null) {
SpanRecorder spanRecorder = trace.getSpanRecorder();
spanRecorder.recordUriTemplate(uri, false);
}
if (trace == null) {
return;
}
}
private String extractAttribute(ServletRequest request, String[] keys) {
for (String attributeName : keys) {
Object uriMapping = request.getAttribute(attributeName);
if (!(uriMapping instanceof String)) {
continue;
}

if (StringUtils.hasLength((String) uriMapping)) {
return (String) uriMapping;
try {
final ServletRequest request = ArrayArgumentUtils.getArgument(args, 1, ServletRequest.class);
if (request != null) {
final String uri = ServletRequestAttributeUtils.extractAttribute(request, SpringWebMvcConstants.SPRING_MVC_DEFAULT_URI_ATTRIBUTE_KEYS);
if (StringUtils.hasLength(uri)) {
final SpanRecorder spanRecorder = trace.getSpanRecorder();
spanRecorder.recordUriTemplate(uri);
}
}
} catch (Throwable th) {
if (logger.isWarnEnabled()) {
logger.warn("AFTER error. Caused:{}", th.getMessage(), th);
}
}
return null;
}
}
Loading

0 comments on commit 3ee6632

Please sign in to comment.