Skip to content

Commit 6092a40

Browse files
committed
ignore double boots
1 parent 1850ec4 commit 6092a40

File tree

2 files changed

+62
-43
lines changed

2 files changed

+62
-43
lines changed

control/src/main/kotlin/spp/probe/SourceProbe.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ object SourceProbe {
5555

5656
@JvmStatic
5757
fun bootAsPlugin(inst: Instrumentation) {
58-
if (ProbeConfiguration.isNotQuite) println("SourceProbe initiated")
58+
if (isAgentInitialized) {
59+
if (ProbeConfiguration.isNotQuite) println("SourceProbe is already initialized")
60+
return
61+
}
62+
if (ProbeConfiguration.isNotQuite) println("SourceProbe initiated via plugin")
5963

6064
//todo: pipe data if in debug mode
6165
System.setProperty("vertx.logger-delegate-factory-class-name", NopLogDelegateFactory::class.java.canonicalName)
@@ -90,7 +94,11 @@ object SourceProbe {
9094

9195
@JvmStatic
9296
fun premain(args: String?, inst: Instrumentation) {
93-
if (ProbeConfiguration.isNotQuite) println("SourceProbe initiated")
97+
if (isAgentInitialized) {
98+
if (ProbeConfiguration.isNotQuite) println("SourceProbe is already initialized")
99+
return
100+
}
101+
if (ProbeConfiguration.isNotQuite) println("SourceProbe initiated via agent")
94102

95103
//todo: pipe data if in debug mode
96104
System.setProperty("vertx.logger-delegate-factory-class-name", NopLogDelegateFactory::class.java.canonicalName)

control/src/main/kotlin/spp/probe/SourceProbePluginDefine.kt

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,65 @@ import java.util.concurrent.atomic.AtomicBoolean
1717
*/
1818
class SourceProbePluginDefine : ClassInstanceMethodsEnhancePluginDefine() {
1919

20+
companion object {
21+
private val probeStarted = AtomicBoolean()
22+
private val cache = HashMap<String, ClassMatch>()
23+
}
24+
2025
override fun enhanceClass(): ClassMatch {
21-
val defaultClass = Class.forName(
22-
"org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder\$Default"
23-
)
24-
val dispatcherField = defaultClass.getDeclaredField("DISPATCHER")
25-
makeAccessible(dispatcherField)
26-
val realDispatcher = dispatcherField.get(null)
27-
dispatcherField.set(null, null)
26+
if (SourceProbe.isAgentInitialized) {
27+
if (ProbeConfiguration.isNotQuite) println("SourceProbe is already initialized")
28+
return NameMatch.byName("") //ignore
29+
}
2830

29-
val dispatcherClass = Class.forName(
30-
"org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder\$Default\$Dispatcher"
31-
)
32-
val addTransformerMethod = dispatcherClass.getDeclaredMethod(
33-
"addTransformer",
34-
Instrumentation::class.java, ClassFileTransformer::class.java, Boolean::class.java
35-
)
36-
val setNativeMethodPrefixMethod = dispatcherClass.getDeclaredMethod(
37-
"setNativeMethodPrefix",
38-
Instrumentation::class.java, ClassFileTransformer::class.java, String::class.java
39-
)
40-
val isNativeMethodPrefixSupportedMethod = dispatcherClass.getDeclaredMethod(
41-
"isNativeMethodPrefixSupported", Instrumentation::class.java
42-
)
31+
return cache.computeIfAbsent("cache") {
32+
val defaultClass = Class.forName(
33+
"org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder\$Default"
34+
)
35+
val dispatcherField = defaultClass.getDeclaredField("DISPATCHER")
36+
makeAccessible(dispatcherField)
37+
val realDispatcher = dispatcherField.get(null)
38+
dispatcherField.set(null, null)
4339

44-
val probeStarted = AtomicBoolean()
45-
val proxyDispatcher = Proxy.newProxyInstance(
46-
dispatcherClass.classLoader,
47-
arrayOf(dispatcherClass)
48-
) { _, method, args ->
49-
return@newProxyInstance when (method.name) {
50-
"addTransformer" -> {
51-
if (probeStarted.compareAndSet(false, true)) {
52-
SourceProbe.bootAsPlugin(args?.get(0) as Instrumentation)
40+
val dispatcherClass = Class.forName(
41+
"org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder\$Default\$Dispatcher"
42+
)
43+
val addTransformerMethod = dispatcherClass.getDeclaredMethod(
44+
"addTransformer",
45+
Instrumentation::class.java, ClassFileTransformer::class.java, Boolean::class.java
46+
)
47+
val setNativeMethodPrefixMethod = dispatcherClass.getDeclaredMethod(
48+
"setNativeMethodPrefix",
49+
Instrumentation::class.java, ClassFileTransformer::class.java, String::class.java
50+
)
51+
val isNativeMethodPrefixSupportedMethod = dispatcherClass.getDeclaredMethod(
52+
"isNativeMethodPrefixSupported", Instrumentation::class.java
53+
)
54+
55+
val proxyDispatcher = Proxy.newProxyInstance(
56+
dispatcherClass.classLoader,
57+
arrayOf(dispatcherClass)
58+
) { _, method, args ->
59+
return@newProxyInstance when (method.name) {
60+
"addTransformer" -> {
61+
if (probeStarted.compareAndSet(false, true)) {
62+
SourceProbe.bootAsPlugin(args?.get(0) as Instrumentation)
63+
}
64+
addTransformerMethod.invoke(realDispatcher, args[0], args[1], args[2])
5365
}
54-
addTransformerMethod.invoke(realDispatcher, args[0], args[1], args[2])
55-
}
56-
"setNativeMethodPrefix" -> {
57-
setNativeMethodPrefixMethod.invoke(realDispatcher, args[0], args[1], args[2])
58-
}
59-
"isNativeMethodPrefixSupported" -> {
60-
isNativeMethodPrefixSupportedMethod.invoke(realDispatcher, args[0])
66+
"setNativeMethodPrefix" -> {
67+
setNativeMethodPrefixMethod.invoke(realDispatcher, args[0], args[1], args[2])
68+
}
69+
"isNativeMethodPrefixSupported" -> {
70+
isNativeMethodPrefixSupportedMethod.invoke(realDispatcher, args[0])
71+
}
72+
else -> throw IllegalStateException("Unknown method: ${method.name}")
6173
}
62-
else -> throw IllegalStateException("Unknown method: ${method.name}")
6374
}
64-
}
65-
dispatcherField.set(null, proxyDispatcher)
75+
dispatcherField.set(null, proxyDispatcher)
6676

67-
return NameMatch.byName("") //ignore
77+
return@computeIfAbsent NameMatch.byName("") //ignore
78+
}
6879
}
6980

7081
@Throws(Exception::class)

0 commit comments

Comments
 (0)