-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge the kamon-bundle project and several build changes required, re…
…lated #626
- Loading branch information
Showing
14 changed files
with
427 additions
and
218 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
bundle/kamon-bundle/src/main/scala/kamon/bundle/Bundle.scala
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,93 @@ | ||
package kamon.bundle | ||
|
||
import java.lang.management.ManagementFactory | ||
import java.nio.file.{Files, StandardCopyOption} | ||
|
||
import net.bytebuddy.agent.ByteBuddyAgent | ||
|
||
object Bundle { | ||
|
||
private val _instrumentationClassLoaderProp = "kanela.instrumentation.classLoader" | ||
|
||
/** | ||
* Attaches the Kanela agent to the current JVM. This method will ignore any attempts to attach the agent if it has | ||
* been attached already. | ||
*/ | ||
def attach(): Unit = { | ||
val springBootClassLoader = findSpringBootJarLauncherClassLoader() | ||
|
||
if(isKanelaLoaded) { | ||
|
||
// If Kanela has already been loaded and we are running on a Spring Boot application, we might need to reload | ||
// Kanela to ensure it will use the proper ClassLoader for loading the instrumentations. | ||
springBootClassLoader.foreach(sbClassLoader => { | ||
withInstrumentationClassLoader(sbClassLoader)(reloadKanela()) | ||
}) | ||
|
||
} else { | ||
|
||
val embeddedAgentFile = Bundle.getClass.getClassLoader.getResourceAsStream(BuildInfo.kanelaAgentJarName) | ||
val temporaryAgentFile = Files.createTempFile(BuildInfo.kanelaAgentJarName, ".jar") | ||
Files.copy(embeddedAgentFile, temporaryAgentFile, StandardCopyOption.REPLACE_EXISTING) | ||
|
||
withInstrumentationClassLoader(springBootClassLoader.orNull) { | ||
ByteBuddyAgent.attach(temporaryAgentFile.toFile, pid()) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Tries to determine whether the Kanela agent has been loaded already. Since there are no APIs to determine what | ||
* agents have been loaded on the current JVM, we rely on two cues that indicate that Kanela is present: first, the | ||
* "kanela.loaded" System property which is set to "true" when the Kanela agent is started and second, the presence | ||
* of the Kanela class in the System ClassLoader. None of these two cues are definite proof, but having both of them | ||
* gives a level of certainty of the Kanela agent being loaded already. | ||
*/ | ||
private def isKanelaLoaded(): Boolean = { | ||
val isLoadedProperty = java.lang.Boolean.parseBoolean(System.getProperty("kanela.loaded")) | ||
val hasKanelaClasses = try { | ||
Class.forName("kanela.agent.Kanela", false, ClassLoader.getSystemClassLoader) != null | ||
} catch { case _: Throwable => false } | ||
|
||
hasKanelaClasses && isLoadedProperty | ||
} | ||
|
||
/** | ||
* Tries to find Spring Boot's classloader, if any. When running a Spring Boot application packaged with the | ||
* "spring-boot-maven-plugin", a fat jar will be created with all the dependencies in it and a special ClassLoader is | ||
* used to unpack them when the jar launches. This function will try to find that ClassLoader which should be used to | ||
* load all Kanela modules. | ||
*/ | ||
private def findSpringBootJarLauncherClassLoader(): Option[ClassLoader] = { | ||
Option(Thread.currentThread().getContextClassLoader()) | ||
.filter(cl => cl.getClass.getName == "org.springframework.boot.loader.LaunchedURLClassLoader") | ||
} | ||
|
||
|
||
/** | ||
* Reloads the Kanela agent. This will cause all instrumentation definitions to be dropped and re-initialized. | ||
*/ | ||
private def reloadKanela(): Unit = { | ||
|
||
// We know that if the agent has been attached, its classes are in the System ClassLoader so we try to find | ||
// the Kanela class from there and call reload on it. | ||
Class.forName("kanela.agent.Kanela", true, ClassLoader.getSystemClassLoader) | ||
.getDeclaredMethod("reload") | ||
.invoke(null) | ||
} | ||
|
||
private def pid(): String = { | ||
val jvm = ManagementFactory.getRuntimeMXBean.getName | ||
jvm.substring(0, jvm.indexOf('@')) | ||
} | ||
|
||
def withInstrumentationClassLoader[T](classLoader: ClassLoader)(thunk: => T): T = { | ||
try { | ||
if(classLoader != null) | ||
System.getProperties.put(_instrumentationClassLoaderProp, classLoader) | ||
thunk | ||
} finally { | ||
System.getProperties.remove(_instrumentationClassLoaderProp) | ||
} | ||
} | ||
} |
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,14 @@ | ||
<configuration> | ||
<statusListener class="ch.qos.logback.core.status.NopStatusListener"/> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<filter class="kamon.log.LogbackFilter"/> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
</configuration> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import sbt.Tests.{Group, SubProcess} | ||
|
||
testGrouping in Test := groupByExperimentalExecutorTests((definedTests in Test).value, kanelaAgentJar.value) | ||
|
||
def groupByExperimentalExecutorTests(tests: Seq[TestDefinition], kanelaJar: File): Seq[Group] = { | ||
val (stable, experimental) = tests.partition(t => t.name != "kamon.instrumentation.executor.CaptureContextOnSubmitInstrumentationSpec") | ||
|
||
val stableGroup = Group("stableTests", stable, SubProcess( | ||
ForkOptions().withRunJVMOptions(Vector( | ||
"-javaagent:" + kanelaJar.toString | ||
)) | ||
)) | ||
|
||
val experimentalGroup = Group("experimentalTests", experimental, SubProcess( | ||
ForkOptions().withRunJVMOptions(Vector( | ||
"-javaagent:" + kanelaJar.toString, | ||
"-Dkanela.modules.executor-service.enabled=false", | ||
"-Dkanela.modules.executor-service-capture-on-submit.enabled=true" | ||
)) | ||
)) | ||
|
||
Seq(stableGroup, experimentalGroup) | ||
} |
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.