|
5 | 5 |
|
6 | 6 | package io.opentelemetry.javaagent.instrumentation.spring.webmvc;
|
7 | 7 |
|
8 |
| -import io.opentelemetry.instrumentation.api.incubator.semconv.util.SpanNames; |
9 |
| -import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; |
| 8 | +import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter; |
10 | 9 | import java.lang.reflect.Method;
|
11 | 10 | import javax.annotation.Nullable;
|
12 | 11 | import org.springframework.web.HttpRequestHandler;
|
13 | 12 | import org.springframework.web.method.HandlerMethod;
|
14 | 13 | import org.springframework.web.servlet.mvc.Controller;
|
15 | 14 |
|
16 |
| -public class HandlerSpanNameExtractor implements SpanNameExtractor<Object> { |
| 15 | +public class HandlerCodeAttributesGetter implements CodeAttributesGetter<Object> { |
17 | 16 |
|
18 | 17 | @Nullable private static final Class<?> JAVAX_SERVLET = loadOrNull("javax.servlet.Servlet");
|
19 | 18 | @Nullable private static final Class<?> JAKARTA_SERVLET = loadOrNull("jakarta.servlet.Servlet");
|
20 | 19 |
|
| 20 | + @Nullable |
21 | 21 | @Override
|
22 |
| - public String extract(Object handler) { |
23 |
| - Class<?> clazz; |
24 |
| - String methodName; |
| 22 | + public Class<?> getCodeClass(Object handler) { |
| 23 | + if (handler instanceof HandlerMethod) { |
| 24 | + // name span based on the class and method name defined in the handler |
| 25 | + Method method = ((HandlerMethod) handler).getMethod(); |
| 26 | + return method.getDeclaringClass(); |
| 27 | + } else { |
| 28 | + return handler.getClass(); |
| 29 | + } |
| 30 | + } |
25 | 31 |
|
| 32 | + @Nullable |
| 33 | + @Override |
| 34 | + public String getMethodName(Object handler) { |
26 | 35 | if (handler instanceof HandlerMethod) {
|
27 | 36 | // name span based on the class and method name defined in the handler
|
28 | 37 | Method method = ((HandlerMethod) handler).getMethod();
|
29 |
| - clazz = method.getDeclaringClass(); |
30 |
| - methodName = method.getName(); |
| 38 | + return method.getName(); |
31 | 39 | } else if (handler instanceof HttpRequestHandler) {
|
32 | 40 | // org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
|
33 |
| - clazz = handler.getClass(); |
34 |
| - methodName = "handleRequest"; |
| 41 | + return "handleRequest"; |
35 | 42 | } else if (handler instanceof Controller) {
|
36 | 43 | // org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
|
37 |
| - clazz = handler.getClass(); |
38 |
| - methodName = "handleRequest"; |
| 44 | + return "handleRequest"; |
39 | 45 | } else if (isServlet(handler)) {
|
40 | 46 | // org.springframework.web.servlet.handler.SimpleServletHandlerAdapter
|
41 |
| - clazz = handler.getClass(); |
42 |
| - methodName = "service"; |
| 47 | + return "service"; |
43 | 48 | } else {
|
44 | 49 | // perhaps org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
|
45 |
| - clazz = handler.getClass(); |
46 |
| - methodName = "<annotation>"; |
| 50 | + return "<annotation>"; |
47 | 51 | }
|
48 |
| - |
49 |
| - return SpanNames.fromMethod(clazz, methodName); |
50 | 52 | }
|
51 | 53 |
|
52 | 54 | private static boolean isServlet(Object handler) {
|
|
0 commit comments