Skip to content

Commit 31c283e

Browse files
authored
1 parent 7cba69c commit 31c283e

File tree

4 files changed

+50
-38
lines changed

4 files changed

+50
-38
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import java.lang.reflect.Method
3737
import java.util.*
3838
import java.util.function.BiConsumer
3939

40+
@Suppress("unused")
4041
class LiveInstrumentRemote : AbstractVerticle() {
4142

4243
private var applyInstrument: Method? = null
@@ -68,15 +69,15 @@ class LiveInstrumentRemote : AbstractVerticle() {
6869
)
6970
putLocalVariable = contextClass.getMethod(
7071
"putLocalVariable",
71-
String::class.java, String::class.java, Any::class.java
72+
String::class.java, String::class.java, Any::class.java, String::class.java
7273
)
7374
putField = contextClass.getMethod(
7475
"putField",
75-
String::class.java, String::class.java, Any::class.java
76+
String::class.java, String::class.java, Any::class.java, String::class.java
7677
)
7778
putStaticField = contextClass.getMethod(
7879
"putStaticField",
79-
String::class.java, String::class.java, Any::class.java
80+
String::class.java, String::class.java, Any::class.java, String::class.java
8081
)
8182
putBreakpoint = contextClass.getMethod(
8283
"putBreakpoint",
@@ -253,27 +254,27 @@ class LiveInstrumentRemote : AbstractVerticle() {
253254
}
254255

255256
@JvmStatic
256-
fun putLocalVariable(breakpointId: String, key: String, value: Any?) {
257+
fun putLocalVariable(breakpointId: String, key: String, value: Any?, type: String) {
257258
try {
258-
putLocalVariable!!.invoke(null, breakpointId, key, value)
259+
putLocalVariable!!.invoke(null, breakpointId, key, value, type)
259260
} catch (e: Exception) {
260261
e.printStackTrace()
261262
}
262263
}
263264

264265
@JvmStatic
265-
fun putField(breakpointId: String, key: String, value: Any?) {
266+
fun putField(breakpointId: String, key: String, value: Any?, type: String?) {
266267
try {
267-
putField!!.invoke(null, breakpointId, key, value)
268+
putField!!.invoke(null, breakpointId, key, value, type)
268269
} catch (e: Exception) {
269270
e.printStackTrace()
270271
}
271272
}
272273

273274
@JvmStatic
274-
fun putStaticField(breakpointId: String, key: String, value: Any?) {
275+
fun putStaticField(breakpointId: String, key: String, value: Any?, type: String) {
275276
try {
276-
putStaticField!!.invoke(null, breakpointId, key, value)
277+
putStaticField!!.invoke(null, breakpointId, key, value, type)
277278
} catch (e: Exception) {
278279
e.printStackTrace()
279280
}

services/src/main/kotlin/spp/probe/services/common/ContextMap.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ package spp.probe.services.common
1818

1919
class ContextMap {
2020

21-
var localVariables: Map<String, Any>? = null
22-
var fields: Map<String, Any>? = null
23-
var staticFields: Map<String, Any>? = null
21+
var localVariables: Map<String, Any?>? = null
22+
var fields: Map<String, Any?>? = null
23+
var staticFields: Map<String, Any?>? = null
2424

2525
override fun toString(): String {
2626
return "ContextMap{" +

services/src/main/kotlin/spp/probe/services/common/ContextReceiver.kt

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ import java.util.*
3838
import java.util.concurrent.ConcurrentHashMap
3939
import java.util.concurrent.Executors
4040

41+
@Suppress("unused")
4142
object ContextReceiver {
4243

43-
private val localVariables: MutableMap<String?, MutableMap<String, Any>> = ConcurrentHashMap()
44-
private val fields: MutableMap<String?, MutableMap<String, Any>> = ConcurrentHashMap()
45-
private val staticFields: MutableMap<String?, MutableMap<String, Any>> = ConcurrentHashMap()
44+
private val localVariables: MutableMap<String?, MutableMap<String, Pair<String, Any?>>> = ConcurrentHashMap()
45+
private val fields: MutableMap<String?, MutableMap<String, Pair<String, Any?>>> = ConcurrentHashMap()
46+
private val staticFields: MutableMap<String?, MutableMap<String, Pair<String, Any?>>> = ConcurrentHashMap()
4647
private val logReport = ServiceManager.INSTANCE.findService(LogReportServiceClient::class.java)
4748
private val executor = Executors.newFixedThreadPool(5)
4849

@@ -61,46 +62,41 @@ object ContextReceiver {
6162
}
6263

6364
@JvmStatic
64-
fun putLocalVariable(instrumentId: String, key: String, value: Any?) {
65-
addInstrumentVariable(instrumentId, key, value, localVariables)
65+
fun putLocalVariable(instrumentId: String, key: String, value: Any?, type: String) {
66+
addInstrumentVariable(instrumentId, key, value, type, localVariables)
6667
}
6768

6869
@JvmStatic
69-
fun putField(instrumentId: String, key: String, value: Any?) {
70-
addInstrumentVariable(instrumentId, key, value, fields)
70+
fun putField(instrumentId: String, key: String, value: Any?, type: String) {
71+
addInstrumentVariable(instrumentId, key, value, type, fields)
7172
}
7273

7374
@JvmStatic
74-
fun putStaticField(instrumentId: String, key: String, value: Any?) {
75-
addInstrumentVariable(instrumentId, key, value, staticFields)
75+
fun putStaticField(instrumentId: String, key: String, value: Any?, type: String) {
76+
addInstrumentVariable(instrumentId, key, value, type, staticFields)
7677
}
7778

7879
private fun addInstrumentVariable(
79-
instrumentId: String, key: String, value: Any?,
80-
variableMap: MutableMap<String?, MutableMap<String, Any>>
80+
instrumentId: String, key: String, value: Any?, type: String,
81+
variableMap: MutableMap<String?, MutableMap<String, Pair<String, Any?>>>
8182
) {
82-
if (value != null) {
83-
variableMap.computeIfAbsent(instrumentId) { HashMap() }[key] = value
84-
}
83+
variableMap.computeIfAbsent(instrumentId) { HashMap() }[key] = Pair(type, value)
8584
}
8685

8786
@JvmStatic
8887
fun putBreakpoint(breakpointId: String, source: String?, line: Int, throwable: Throwable) = executor.submit {
8988
val activeSpan = ContextManager.createLocalSpan(throwable.stackTrace[0].toString())
90-
val localVars: Map<String, Any>? = localVariables.remove(breakpointId)
91-
localVars?.forEach { (key: String, value: Any) ->
89+
localVariables.remove(breakpointId)?.forEach { (key: String, value: Pair<String, Any?>) ->
9290
activeSpan.tag(
9391
StringTag("spp.local-variable:$breakpointId:$key"), encodeObject(key, value)
9492
)
9593
}
96-
val localFields: Map<String, Any>? = fields.remove(breakpointId)
97-
localFields?.forEach { (key: String, value: Any) ->
94+
fields.remove(breakpointId)?.forEach { (key: String, value: Pair<String, Any?>) ->
9895
activeSpan.tag(
9996
StringTag("spp.field:$breakpointId:$key"), encodeObject(key, value)
10097
)
10198
}
102-
val localStaticFields: Map<String, Any>? = staticFields.remove(breakpointId)
103-
localStaticFields?.forEach { (key: String, value: Any) ->
99+
staticFields.remove(breakpointId)?.forEach { (key: String, value: Pair<String, Any?>) ->
104100
activeSpan.tag(
105101
StringTag("spp.static-field:$breakpointId:$key"), encodeObject(key, value)
106102
)
@@ -118,9 +114,9 @@ object ContextReceiver {
118114

119115
@JvmStatic
120116
fun putLog(logId: String?, logFormat: String?, vararg logArguments: String) = executor.submit {
121-
val localVars: Map<String, Any>? = localVariables.remove(logId)
122-
val localFields: Map<String, Any>? = fields.remove(logId)
123-
val localStaticFields: Map<String, Any>? = staticFields.remove(logId)
117+
val localVars: Map<String, Any?>? = localVariables.remove(logId)
118+
val localFields: Map<String, Any?>? = fields.remove(logId)
119+
val localStaticFields: Map<String, Any?>? = staticFields.remove(logId)
124120
val logTags = LogTags.newBuilder()
125121
.addData(
126122
KeyStringValuePair.newBuilder()
@@ -179,15 +175,18 @@ object ContextReceiver {
179175
"count_" + meterId.replace("-", "_")
180176
).mode(CounterMode.valueOf(liveMeter.meta.getOrDefault("metric.mode", "INCREMENT") as String))
181177
.build()
178+
182179
MeterType.GAUGE -> return@computeIfAbsent MeterFactory.gauge(
183180
"gauge_" + meterId.replace("-", "_")
184181
) { liveMeter.metricValue.value.toDouble() }
185182
.build()
183+
186184
MeterType.HISTOGRAM -> return@computeIfAbsent MeterFactory.histogram(
187185
"histogram_" + meterId.replace("-", "_")
188186
)
189187
.steps(listOf(0.0)) //todo: dynamic
190188
.build()
189+
191190
else -> throw UnsupportedOperationException("Unsupported meter type: ${liveMeter.meterType}")
192191
}
193192
}
@@ -197,12 +196,14 @@ object ContextReceiver {
197196
} else {
198197
throw UnsupportedOperationException("todo") //todo: this
199198
}
199+
200200
MeterType.GAUGE -> {}
201201
MeterType.HISTOGRAM -> if (liveMeter.metricValue.valueType == MetricValueType.NUMBER) {
202202
(baseMeter as Histogram).addValue(liveMeter.metricValue.value.toDouble())
203203
} else {
204204
throw UnsupportedOperationException("todo") //todo: this
205205
}
206+
206207
else -> throw UnsupportedOperationException("Unsupported meter type: ${liveMeter.meterType}")
207208
}
208209
}
@@ -222,7 +223,11 @@ object ContextReceiver {
222223
ContextManager.stopSpan(activeSpan)
223224
}
224225

225-
private fun encodeObject(varName: String, value: Any): String? {
226+
private fun encodeObject(varName: String, varData: Pair<String, Any?>): String? {
227+
val value = varData.second ?: return String.format(
228+
"{\"@class\":\"%s\",\"@null\":true,\"$varName\":%s}", varData.first, null
229+
)
230+
226231
return try {
227232
String.format(
228233
"{\"@class\":\"%s\",\"@id\":\"%s\",\"$varName\":%s}",

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class LiveInstrumentTransformer(
4141
private val THROWABLE_INTERNAL_NAME = Type.getInternalName(Throwable::class.java)
4242
const val REMOTE_CLASS_LOCATION = "spp/probe/control/LiveInstrumentRemote"
4343
private const val REMOTE_CHECK_DESC = "(Ljava/lang/String;)Z"
44-
private const val REMOTE_SAVE_VAR_DESC = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"
44+
private const val REMOTE_SAVE_VAR_DESC = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)V"
4545
private const val PUT_LOG_DESC = "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V"
4646

4747
fun isXRETURN(opcode: Int): Boolean {
@@ -155,10 +155,12 @@ class LiveInstrumentTransformer(
155155
private fun addLocals(instrumentId: String, line: Int) {
156156
for (local in classMetadata.variables[methodUniqueName].orEmpty()) {
157157
if (line >= local.start && line < local.end) {
158+
val type = Type.getType(local.desc)
158159
mv.visitLdcInsn(instrumentId)
159160
mv.visitLdcInsn(local.name)
160-
mv.visitVarInsn(Type.getType(local.desc).getOpcode(Opcodes.ILOAD), local.index)
161+
mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), local.index)
161162
LiveTransformer.boxIfNecessary(mv, local.desc)
163+
mv.visitLdcInsn(type.className)
162164
mv.visitMethodInsn(
163165
Opcodes.INVOKESTATIC, REMOTE_CLASS_LOCATION,
164166
"putLocalVariable", REMOTE_SAVE_VAR_DESC, false
@@ -169,10 +171,12 @@ class LiveInstrumentTransformer(
169171

170172
private fun addStaticFields(instrumentId: String) {
171173
for (staticField in classMetadata.staticFields) {
174+
val type = Type.getType(staticField.desc)
172175
mv.visitLdcInsn(instrumentId)
173176
mv.visitLdcInsn(staticField.name)
174177
mv.visitFieldInsn(Opcodes.GETSTATIC, className, staticField.name, staticField.desc)
175178
LiveTransformer.boxIfNecessary(mv, staticField.desc)
179+
mv.visitLdcInsn(type.className)
176180
mv.visitMethodInsn(
177181
Opcodes.INVOKESTATIC, REMOTE_CLASS_LOCATION,
178182
"putStaticField", REMOTE_SAVE_VAR_DESC, false
@@ -183,11 +187,13 @@ class LiveInstrumentTransformer(
183187
private fun addFields(instrumentId: String) {
184188
if (access and Opcodes.ACC_STATIC == 0) {
185189
for (field in classMetadata.fields) {
190+
val type = Type.getType(field.desc)
186191
mv.visitLdcInsn(instrumentId)
187192
mv.visitLdcInsn(field.name)
188193
mv.visitVarInsn(Opcodes.ALOAD, 0)
189194
mv.visitFieldInsn(Opcodes.GETFIELD, className, field.name, field.desc)
190195
LiveTransformer.boxIfNecessary(mv, field.desc)
196+
mv.visitLdcInsn(type.className)
191197
mv.visitMethodInsn(
192198
Opcodes.INVOKESTATIC, REMOTE_CLASS_LOCATION,
193199
"putField", REMOTE_SAVE_VAR_DESC, false

0 commit comments

Comments
 (0)