-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instrument spring-web 6 & spring-webmvc 6 (#7366)
Part of #7203 This PR is mostly copy-paste and working around the differences, conceptually the new instrumentation is the same as the old one
- Loading branch information
Mateusz Rzeszutek
authored
Dec 12, 2022
1 parent
7c39e54
commit 59b7513
Showing
35 changed files
with
1,031 additions
and
244 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...opentelemetry/javaagent/instrumentation/spring/core/SpringCoreIgnoredTypesConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.core; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesBuilder; | ||
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesConfigurer; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
|
||
@AutoService(IgnoredTypesConfigurer.class) | ||
public class SpringCoreIgnoredTypesConfigurer implements IgnoredTypesConfigurer { | ||
|
||
@Override | ||
public void configure(IgnoredTypesBuilder builder, ConfigProperties config) { | ||
// a Runnable task class that we don't need to touch | ||
builder | ||
.ignoreClass("org.springframework.util.ConcurrentLruCache$AddTask") | ||
.ignoreTaskClass("org.springframework.util.ConcurrentLruCache$AddTask"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
instrumentation/spring/spring-web/spring-web-6.0/javaagent/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("org.springframework") | ||
module.set("spring-web") | ||
versions.set("[6.0.0,)") | ||
assertInverse.set(true) | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly("org.springframework:spring-web:6.0.0") | ||
} |
34 changes: 34 additions & 0 deletions
34
...entelemetry/javaagent/instrumentation/spring/web/v6_0/SpringWebInstrumentationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.web.v6_0; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static java.util.Collections.singletonList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.List; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class SpringWebInstrumentationModule extends InstrumentationModule { | ||
|
||
public SpringWebInstrumentationModule() { | ||
super("spring-web", "spring-web-6.0"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
// class added in 6.0 | ||
return hasClassesNamed("org.springframework.web.ErrorResponse"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return singletonList(new WebApplicationContextInstrumentation()); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...metry/javaagent/instrumentation/spring/web/v6_0/WebApplicationContextInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.web.v6_0; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass; | ||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.GenericBeanDefinition; | ||
|
||
/** | ||
* This instrumentation adds the OpenTelemetryHandlerMappingFilter definition to the spring context | ||
* When the context is created, the filter will be added to the beginning of the filter chain. | ||
*/ | ||
public class WebApplicationContextInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderOptimization() { | ||
return hasClassesNamed( | ||
"org.springframework.context.support.AbstractApplicationContext", | ||
"org.springframework.web.context.WebApplicationContext"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return extendsClass(named("org.springframework.context.support.AbstractApplicationContext")) | ||
.and(implementsInterface(named("org.springframework.web.context.WebApplicationContext"))); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(named("postProcessBeanFactory")) | ||
.and( | ||
takesArgument( | ||
0, | ||
named( | ||
"org.springframework.beans.factory.config.ConfigurableListableBeanFactory"))), | ||
WebApplicationContextInstrumentation.class.getName() + "$FilterInjectingAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class FilterInjectingAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter(@Advice.Argument(0) ConfigurableListableBeanFactory beanFactory) { | ||
if (beanFactory instanceof BeanDefinitionRegistry | ||
&& !beanFactory.containsBean("otelAutoDispatcherFilter")) { | ||
try { | ||
// Firstly check whether DispatcherServlet is present. We need to load an instrumented | ||
// class from spring-webmvc to trigger injection that makes | ||
// OpenTelemetryHandlerMappingFilter available. | ||
Class<?> dispatcherServletClass = | ||
beanFactory | ||
.getBeanClassLoader() | ||
.loadClass("org.springframework.web.servlet.DispatcherServlet"); | ||
|
||
// Now attempt to load our injected instrumentation class from the same class loader as | ||
// DispatcherServlet | ||
Class<?> clazz = | ||
dispatcherServletClass | ||
.getClassLoader() | ||
.loadClass( | ||
"org.springframework.web.servlet.v6_0.OpenTelemetryHandlerMappingFilter"); | ||
GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); | ||
beanDefinition.setScope(SCOPE_SINGLETON); | ||
beanDefinition.setBeanClass(clazz); | ||
|
||
((BeanDefinitionRegistry) beanFactory) | ||
.registerBeanDefinition("otelAutoDispatcherFilter", beanDefinition); | ||
} catch (ClassNotFoundException ignored) { | ||
// Ignore | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
...ring-webmvc/spring-webmvc-3.1/javaagent/src/test/groovy/test/boot/AuthServerConfig.groovy
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.