|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.spring.web.v6_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass; |
| 9 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; |
| 10 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 14 | +import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON; |
| 15 | + |
| 16 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 17 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 18 | +import net.bytebuddy.asm.Advice; |
| 19 | +import net.bytebuddy.description.type.TypeDescription; |
| 20 | +import net.bytebuddy.matcher.ElementMatcher; |
| 21 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 22 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 23 | +import org.springframework.beans.factory.support.GenericBeanDefinition; |
| 24 | + |
| 25 | +/** |
| 26 | + * This instrumentation adds the OpenTelemetryHandlerMappingFilter definition to the spring context |
| 27 | + * When the context is created, the filter will be added to the beginning of the filter chain. |
| 28 | + */ |
| 29 | +public class WebApplicationContextInstrumentation implements TypeInstrumentation { |
| 30 | + |
| 31 | + @Override |
| 32 | + public ElementMatcher<ClassLoader> classLoaderOptimization() { |
| 33 | + return hasClassesNamed( |
| 34 | + "org.springframework.context.support.AbstractApplicationContext", |
| 35 | + "org.springframework.web.context.WebApplicationContext"); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 40 | + return extendsClass(named("org.springframework.context.support.AbstractApplicationContext")) |
| 41 | + .and(implementsInterface(named("org.springframework.web.context.WebApplicationContext"))); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void transform(TypeTransformer transformer) { |
| 46 | + transformer.applyAdviceToMethod( |
| 47 | + isMethod() |
| 48 | + .and(named("postProcessBeanFactory")) |
| 49 | + .and( |
| 50 | + takesArgument( |
| 51 | + 0, |
| 52 | + named( |
| 53 | + "org.springframework.beans.factory.config.ConfigurableListableBeanFactory"))), |
| 54 | + WebApplicationContextInstrumentation.class.getName() + "$FilterInjectingAdvice"); |
| 55 | + } |
| 56 | + |
| 57 | + @SuppressWarnings("unused") |
| 58 | + public static class FilterInjectingAdvice { |
| 59 | + |
| 60 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 61 | + public static void onEnter(@Advice.Argument(0) ConfigurableListableBeanFactory beanFactory) { |
| 62 | + if (beanFactory instanceof BeanDefinitionRegistry |
| 63 | + && !beanFactory.containsBean("otelAutoDispatcherFilter")) { |
| 64 | + try { |
| 65 | + // Firstly check whether DispatcherServlet is present. We need to load an instrumented |
| 66 | + // class from spring-webmvc to trigger injection that makes |
| 67 | + // OpenTelemetryHandlerMappingFilter available. |
| 68 | + Class<?> dispatcherServletClass = |
| 69 | + beanFactory |
| 70 | + .getBeanClassLoader() |
| 71 | + .loadClass("org.springframework.web.servlet.DispatcherServlet"); |
| 72 | + |
| 73 | + // Now attempt to load our injected instrumentation class from the same class loader as |
| 74 | + // DispatcherServlet |
| 75 | + Class<?> clazz = |
| 76 | + dispatcherServletClass |
| 77 | + .getClassLoader() |
| 78 | + .loadClass( |
| 79 | + "org.springframework.web.servlet.v6_0.OpenTelemetryHandlerMappingFilter"); |
| 80 | + GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); |
| 81 | + beanDefinition.setScope(SCOPE_SINGLETON); |
| 82 | + beanDefinition.setBeanClass(clazz); |
| 83 | + |
| 84 | + ((BeanDefinitionRegistry) beanFactory) |
| 85 | + .registerBeanDefinition("otelAutoDispatcherFilter", beanDefinition); |
| 86 | + } catch (ClassNotFoundException ignored) { |
| 87 | + // Ignore |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments