Skip to content

Commit f67cd84

Browse files
committed
chore: increase logging
1 parent d29f452 commit f67cd84

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

services/src/main/kotlin/spp/probe/services/instrument/LiveInstrumentService.kt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package spp.probe.services.instrument
1818

1919
import net.bytebuddy.pool.TypePool
2020
import org.apache.skywalking.apm.agent.core.context.util.ThrowableTransformer
21+
import org.apache.skywalking.apm.agent.core.logging.api.LogManager
2122
import org.springframework.expression.ParseException
2223
import org.springframework.expression.spel.SpelCompilerMode
2324
import org.springframework.expression.spel.SpelParserConfiguration
@@ -39,6 +40,7 @@ import java.util.stream.Collectors
3940

4041
object LiveInstrumentService {
4142

43+
private val log = LogManager.getLogger(LiveInstrumentService::class.java)
4244
private val instruments: MutableMap<String?, ActiveLiveInstrument> = ConcurrentHashMap()
4345
private val applyingInstruments: MutableMap<String?, ActiveLiveInstrument> = ConcurrentHashMap()
4446
private val parser = SpelExpressionParser(
@@ -52,6 +54,7 @@ object LiveInstrumentService {
5254
init {
5355
timer.schedule(object : TimerTask() {
5456
override fun run() {
57+
if (log.isDebugEnable) log.debug("Running LiveInstrumentScheduler")
5558
val removeInstruments: MutableList<ActiveLiveInstrument> = ArrayList()
5659
instruments.values.forEach {
5760
if (it.instrument.expiresAt != null
@@ -67,13 +70,23 @@ object LiveInstrumentService {
6770
removeInstruments.add(it)
6871
}
6972
}
73+
74+
if (log.isDebugEnable) log.debug("Found {} expired instruments", removeInstruments.size)
7075
removeInstruments.forEach { _removeInstrument(it.instrument, null) }
7176
}
7277
}, 5000, 5000)
7378
}
7479

7580
var liveInstrumentApplier = object : LiveInstrumentApplier {
7681
override fun apply(inst: Instrumentation, instrument: ActiveLiveInstrument) {
82+
if (log.isInfoEnable) {
83+
if (instrument.isRemoval) {
84+
log.info("Attempting to remove live instrument: {}", instrument.instrument)
85+
} else {
86+
log.info("Attempting to apply live instrument: {}", instrument.instrument)
87+
}
88+
}
89+
7790
val className = if (instrument.instrument.location.source.contains("(")) {
7891
instrument.instrument.location.source.substringBefore("(").substringBeforeLast(".")
7992
} else {
@@ -83,17 +96,24 @@ object LiveInstrumentService {
8396
for (classLoader in poolMap.keys) {
8497
try {
8598
clazz = Class.forName(className, true, classLoader)
99+
if (log.isInfoEnable) log.info("Found {} in class loader {}", clazz, classLoader)
86100
} catch (ignored: ClassNotFoundException) {
87101
}
88102
}
89103
if (poolMap.isEmpty()) {
90104
try {
91105
clazz = Class.forName(className)
106+
if (log.isInfoEnable) log.info("Found {} in system class loader", clazz)
92107
} catch (ignored: ClassNotFoundException) {
93108
}
94109
}
95110
if (clazz == null) {
111+
if (log.isDebugEnable) log.debug("{} not found", className)
96112
if (instrument.instrument.applyImmediately) {
113+
log.warn(
114+
"Unable to find {}. Live instrument {} cannot be applied immediately",
115+
className, instrument.instrument
116+
)
97117
throw LiveInstrumentException(LiveInstrumentException.ErrorType.CLASS_NOT_FOUND, className)
98118
.toEventBusException()
99119
} else if (!instrument.isRemoval) {
@@ -114,20 +134,27 @@ object LiveInstrumentService {
114134
inst.addTransformer(transformer, true)
115135
inst.retransformClasses(clazz)
116136
instrument.isLive = true
117-
if (!instrument.isRemoval) {
137+
138+
if (instrument.isRemoval) {
139+
if (log.isInfoEnable) log.info("Successfully removed live instrument: {}", instrument.instrument)
140+
} else {
141+
if (log.isInfoEnable) log.info("Successfully applied live instrument {}", instrument.instrument)
118142
instrumentEventConsumer!!.accept(
119143
ProcessorAddress.LIVE_INSTRUMENT_APPLIED,
120144
ModelSerializer.INSTANCE.toJson(instrument.instrument)
121145
)
122146
}
123147
} catch (ex: Throwable) {
148+
log.warn(ex, "Failed to apply live instrument: {}", instrument)
149+
124150
//remove and re-transform
125151
_removeInstrument(instrument.instrument, ex)
126152
applyingInstruments.remove(instrument.instrument.id)
127153
inst.addTransformer(transformer, true)
128154
try {
129155
inst.retransformClasses(clazz)
130156
} catch (e: UnmodifiableClassException) {
157+
log.warn(e, "Failed to re-transform class: {}", clazz)
131158
throw RuntimeException(e)
132159
}
133160
} finally {
@@ -176,6 +203,7 @@ object LiveInstrumentService {
176203
val expression = parser.parseExpression(liveInstrument.condition!!)
177204
ActiveLiveInstrument(liveInstrument, expression)
178205
} catch (ex: ParseException) {
206+
log.warn(ex, "Failed to parse condition: {}", liveInstrument.condition)
179207
throw LiveInstrumentException(LiveInstrumentException.ErrorType.CONDITIONAL_FAILED, ex.message)
180208
.toEventBusException()
181209
}
@@ -217,6 +245,12 @@ object LiveInstrumentService {
217245
}
218246

219247
fun _removeInstrument(instrument: LiveInstrument, ex: Throwable?) {
248+
if (ex != null) {
249+
log.warn(ex, "Removing erroneous live instrument: {}", instrument)
250+
} else {
251+
log.info("Removing live instrument: {}", instrument)
252+
}
253+
220254
removeInstrument(instrument.location.source, instrument.location.line, instrument.id)
221255
val map: MutableMap<String, Any?> = HashMap()
222256
map["instrument"] = ModelSerializer.INSTANCE.toJson(instrument)
@@ -287,6 +321,7 @@ object LiveInstrumentService {
287321
false
288322
}
289323
} catch (e: Throwable) {
324+
log.warn(e, "Failed to evaluate condition: {}", instrument.instrument.condition)
290325
ContextReceiver.clear(instrumentId)
291326
_removeInstrument(instrument.instrument, e)
292327
false

services/src/main/kotlin/spp/probe/services/instrument/LiveInstrumentTransformer.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import net.bytebuddy.jar.asm.Label
2020
import net.bytebuddy.jar.asm.MethodVisitor
2121
import net.bytebuddy.jar.asm.Opcodes
2222
import net.bytebuddy.jar.asm.Type
23+
import org.apache.skywalking.apm.agent.core.logging.api.LogManager
2324
import spp.probe.services.common.ProbeMemory
2425
import spp.probe.services.common.model.ClassMetadata
2526
import spp.probe.services.common.transform.LiveTransformer
@@ -38,6 +39,7 @@ class LiveInstrumentTransformer(
3839
) : MethodVisitor(Opcodes.ASM7, mv) {
3940

4041
companion object {
42+
private val log = LogManager.getLogger(LiveInstrumentTransformer::class.java)
4143
private val THROWABLE_INTERNAL_NAME = Type.getInternalName(Throwable::class.java)
4244
const val REMOTE_CLASS_LOCATION = "spp/probe/control/LiveInstrumentRemote"
4345
private const val REMOTE_CHECK_DESC = "(Ljava/lang/String;)Z"
@@ -82,6 +84,7 @@ class LiveInstrumentTransformer(
8284
descArgs.delete(0, 2)
8385
}
8486
} else {
87+
log.warn("Invalid descriptor: $desc")
8588
throw IllegalArgumentException("Invalid descriptor: $desc")
8689
}
8790
}
@@ -91,13 +94,18 @@ class LiveInstrumentTransformer(
9194
if (activeSpans.size == 1) {
9295
liveInstrument = activeSpans[0].instrument as LiveSpan
9396
} else if (activeSpans.size > 1) {
97+
log.warn("Multiple live spans found for $qualifiedMethodName")
9498
TODO()
9599
}
96100
}
97101

98102
override fun visitLineNumber(line: Int, start: Label) {
99103
mv.visitLineNumber(line, start)
100104
for (instrument in LiveInstrumentService.getInstruments(className.replace("/", "."), line)) {
105+
if (log.isInfoEnable) {
106+
log.info("Injecting live instrument {} on line {} of {}", instrument.instrument, line, className)
107+
}
108+
101109
val instrumentLabel = Label()
102110
isInstrumentEnabled(instrument.instrument.id!!, instrumentLabel)
103111
when (instrument.instrument) {
@@ -106,6 +114,7 @@ class LiveInstrumentTransformer(
106114
isHit(instrument.instrument.id!!, instrumentLabel)
107115
putBreakpoint(instrument.instrument.id!!, className.replace("/", "."), line)
108116
}
117+
109118
is LiveLog -> {
110119
val log = instrument.instrument
111120
if (log.logArguments.isNotEmpty() || instrument.expression != null) {
@@ -114,6 +123,7 @@ class LiveInstrumentTransformer(
114123
isHit(log.id!!, instrumentLabel)
115124
putLog(log)
116125
}
126+
117127
is LiveMeter -> {
118128
val meter = instrument.instrument
119129
if (instrument.expression != null) {

0 commit comments

Comments
 (0)