-
Notifications
You must be signed in to change notification settings - Fork 892
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
Move helper class to spring package so that loadClass can find it #3718
Merged
trask
merged 6 commits into
open-telemetry:main
from
laurit:spring-helper-class-package
Aug 20, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
instrumentation/spring/spring-web-3.1/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,18 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("org.springframework") | ||
module.set("spring-web") | ||
versions.set("[3.1.0.RELEASE,]") | ||
// these versions depend on javax.faces:jsf-api:1.1 which was released as pom only | ||
skip("1.2.1", "1.2.2", "1.2.3", "1.2.4") | ||
assertInverse.set(true) | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly("org.springframework:spring-web:3.1.0.RELEASE") | ||
} |
33 changes: 33 additions & 0 deletions
33
.../io/opentelemetry/javaagent/instrumentation/springweb/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,33 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.springweb; | ||
|
||
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-3.1"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
// class added in 3.1 | ||
return hasClassesNamed("org.springframework.web.method.HandlerMethod"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return singletonList(new WebApplicationContextInstrumentation()); | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
instrumentation/spring/spring-webmvc-3.1/wildfly-testing/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,82 @@ | ||
plugins { | ||
id("otel.javaagent-testing") | ||
} | ||
|
||
val testServer by configurations.creating | ||
val appLibrary by configurations.creating | ||
|
||
configurations.named("testCompileOnly") { | ||
extendsFrom(appLibrary) | ||
} | ||
|
||
dependencies { | ||
appLibrary("org.springframework:spring-webmvc:3.1.0.RELEASE") | ||
testImplementation("javax.servlet:javax.servlet-api:3.1.0") | ||
|
||
val arquillianVersion = "1.4.0.Final" | ||
testImplementation("org.jboss.arquillian.junit:arquillian-junit-container:${arquillianVersion}") | ||
testImplementation("org.jboss.arquillian.protocol:arquillian-protocol-servlet:${arquillianVersion}") | ||
testImplementation("org.jboss.arquillian.spock:arquillian-spock-container:1.0.0.CR1") | ||
testImplementation("org.jboss.shrinkwrap:shrinkwrap-impl-base:1.2.6") | ||
|
||
testRuntimeOnly("org.wildfly.arquillian:wildfly-arquillian-container-embedded:2.2.0.Final") | ||
|
||
testInstrumentation(project(":instrumentation:servlet:servlet-3.0:javaagent")) | ||
testInstrumentation(project(":instrumentation:spring:spring-webmvc-3.1:javaagent")) | ||
testInstrumentation(project(":instrumentation:spring:spring-web-3.1:javaagent")) | ||
|
||
// wildfly version used to run tests | ||
testServer("org.wildfly:wildfly-dist:18.0.0.Final@zip") | ||
} | ||
|
||
tasks { | ||
// extract wildfly dist, path is used from arquillian.xml | ||
val setupServer by registering(Copy::class) { | ||
from(zipTree(testServer.singleFile)) | ||
into(file("build/server/")) | ||
} | ||
|
||
// logback-classic contains /META-INF/services/javax.servlet.ServletContainerInitializer | ||
// that breaks deploy on embedded wildfly | ||
// create a copy of logback-classic jar that does not have this file | ||
val modifyLogbackJar by registering(Jar::class) { | ||
destinationDirectory.set(file("$buildDir/tmp")) | ||
archiveFileName.set("logback-classic-modified.jar") | ||
exclude("/META-INF/services/javax.servlet.ServletContainerInitializer") | ||
doFirst { | ||
configurations.configureEach { | ||
if (name.toLowerCase().endsWith("testruntimeclasspath")) { | ||
val logbackJar = find { it.name.contains("logback-classic") } | ||
from(zipTree(logbackJar)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
val copyDependencies by registering(Copy::class) { | ||
// test looks for spring jars that are bundled inside deployed application from this directory | ||
from(appLibrary).into("$buildDir/app-libs") | ||
} | ||
|
||
named<Test>("test") { | ||
dependsOn(modifyLogbackJar) | ||
dependsOn(setupServer) | ||
dependsOn(copyDependencies) | ||
|
||
doFirst { | ||
// --add-modules is unrecognized on jdk8, ignore it instead of failing | ||
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") | ||
// needed for java 11 to avoid org.jboss.modules.ModuleNotFoundException: java.se | ||
jvmArgs("--add-modules=java.se") | ||
// add offset to default port values | ||
jvmArgs("-Djboss.socket.binding.port-offset=300") | ||
|
||
// remove logback-classic from classpath | ||
classpath = classpath.filter { | ||
!it.absolutePath.contains("logback-classic") | ||
} | ||
// add modified copy of logback-classic to classpath | ||
classpath = classpath.plus(files("$buildDir/tmp/logback-classic-modified.jar")) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenTelemetryHandlerMappingFilter
is defined in the webmvc instrumentation, right? This makes spring-web and spring-webmvc instrumentations depend on each other; one can't function without the other.I think it'd be more preferable to have one
InstrumentationModule
delivering a single, coherent piece of functionality (webmvc instrumentation) even if we have to depend on two libs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that doesn't really work when 2 modules are in different class loaders. Muzzle will fail on spring-web because instrumentation uses classes from spring-webmvc hence they can't be in the same
InstrumentationModule
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's true -- the instrumentation won't be applied if the classloader doesn't contain all references.
🤯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have this problem for all instrumentations which use currently apply a muzzle
extraDependency
? is this change needed for this PR, or can we split out to discuss separately?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we assume that all spring jars are in the same class loader then this would not be needed. Nowadays this probably isn't super important, but years ago when spring was pushing their dm server and all spring jars had osgi manifests then when instrumenting spring it was best to make sure that the code you inject with instrumentation doesn't reference anything from bundles that instrumented bundle doesn't depend on. If we just insert reference to
OpenTelemetryHandlerMappingFilter
into something likeorg.springframework.web.context.support.AbstractRefreshableWebApplicationContext
we have created a dependency fromspring-web
tospring-webmvc
which doesn't exist in original code (dependency is the other way around) which would make application fail if it for some reason usedspring-web
but notspring-webmvc
.extraDependency
is usually completely fine, as long as instrumented library has a non optional dependency to something then we can freely use it in instrumentation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yes this makes perfect sense thx 👍👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just adding comment in case I come back here later 😄: we can freely use the
extraDependency
in the instrumentation, but we cannot freely instrument theextraDependency