Skip to content

Add Spring web operation name handler #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ public void onAfterConcurrentHandlingStarted(HttpServletRequest httpServletReque
}
};

/**
* Use the Spring RequestMapping's value as the span's operation name.
*/
HandlerInterceptorSpanDecorator HANDLER_SPRING_WEB_METHOD_OPERATION_NAME = new HandlerInterceptorSpanDecorator() {
@Override
public void onPreHandle(HttpServletRequest httpServletRequest, Object handler, Span span) {
String webOperationName = HandlerUtils.getOperationName(handler);
if (webOperationName != null) {
span.setOperationName(webOperationName);
return;
}

String metaData = HandlerInterceptorSpanDecorator.HandlerUtils.methodName(handler);
if (metaData != null) {
span.setOperationName(metaData);
}
}

@Override
public void onAfterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, Exception ex, Span span) {
}

@Override
public void onAfterConcurrentHandlingStarted(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, Span span) {
}
};

/**
* Helper class for deriving tags/logs from handler object.
*/
Expand Down Expand Up @@ -163,8 +190,31 @@ public static String methodName(Object handler) {
}

public static String requestMapping(Object handler) {
String[] mappings = ((HandlerMethod) handler).getMethodAnnotation(RequestMapping.class).path();
String[] mappings = ((HandlerMethod) handler).getMethodAnnotation(RequestMapping.class).value();
return Arrays.toString(mappings);
}

public static String methodRequestMapping(Object handler) {
String[] mappings = (((HandlerMethod) handler).getMethodAnnotation(RequestMapping.class)).value();
return mappings != null && mappings.length > 0 ? mappings[0] : null;
}

public static String clazzRequestMapping(Object handler) {
String[] mappings = ((HandlerMethod) handler).getBeanType().getAnnotation(RequestMapping.class).value();
return mappings != null && mappings.length > 0 ? mappings[0] : null;
}

public static String getOperationName(Object handler){
StringBuffer operationName = null;
String methodRequestMapping = methodRequestMapping(handler);
if (methodRequestMapping != null) {
operationName = new StringBuffer(methodRequestMapping);
String clazzRequestMapping = clazzRequestMapping(handler);
if (clazzRequestMapping != null) {
operationName.insert(0, clazzRequestMapping);
}
}
return operationName==null?null:operationName.toString();
}
}
}