@@ -38,11 +38,12 @@ import java.util.*
38
38
import java.util.concurrent.ConcurrentHashMap
39
39
import java.util.concurrent.Executors
40
40
41
+ @Suppress(" unused" )
41
42
object ContextReceiver {
42
43
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 ()
46
47
private val logReport = ServiceManager .INSTANCE .findService(LogReportServiceClient ::class .java)
47
48
private val executor = Executors .newFixedThreadPool(5 )
48
49
@@ -61,46 +62,41 @@ object ContextReceiver {
61
62
}
62
63
63
64
@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)
66
67
}
67
68
68
69
@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)
71
72
}
72
73
73
74
@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)
76
77
}
77
78
78
79
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 ?> >>
81
82
) {
82
- if (value != null ) {
83
- variableMap.computeIfAbsent(instrumentId) { HashMap () }[key] = value
84
- }
83
+ variableMap.computeIfAbsent(instrumentId) { HashMap () }[key] = Pair (type, value)
85
84
}
86
85
87
86
@JvmStatic
88
87
fun putBreakpoint (breakpointId : String , source : String? , line : Int , throwable : Throwable ) = executor.submit {
89
88
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 ?>) ->
92
90
activeSpan.tag(
93
91
StringTag (" spp.local-variable:$breakpointId :$key " ), encodeObject(key, value)
94
92
)
95
93
}
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 ?>) ->
98
95
activeSpan.tag(
99
96
StringTag (" spp.field:$breakpointId :$key " ), encodeObject(key, value)
100
97
)
101
98
}
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 ?>) ->
104
100
activeSpan.tag(
105
101
StringTag (" spp.static-field:$breakpointId :$key " ), encodeObject(key, value)
106
102
)
@@ -118,9 +114,9 @@ object ContextReceiver {
118
114
119
115
@JvmStatic
120
116
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)
124
120
val logTags = LogTags .newBuilder()
125
121
.addData(
126
122
KeyStringValuePair .newBuilder()
@@ -179,15 +175,18 @@ object ContextReceiver {
179
175
" count_" + meterId.replace(" -" , " _" )
180
176
).mode(CounterMode .valueOf(liveMeter.meta.getOrDefault(" metric.mode" , " INCREMENT" ) as String ))
181
177
.build()
178
+
182
179
MeterType .GAUGE -> return @computeIfAbsent MeterFactory .gauge(
183
180
" gauge_" + meterId.replace(" -" , " _" )
184
181
) { liveMeter.metricValue.value.toDouble() }
185
182
.build()
183
+
186
184
MeterType .HISTOGRAM -> return @computeIfAbsent MeterFactory .histogram(
187
185
" histogram_" + meterId.replace(" -" , " _" )
188
186
)
189
187
.steps(listOf (0.0 )) // todo: dynamic
190
188
.build()
189
+
191
190
else -> throw UnsupportedOperationException (" Unsupported meter type: ${liveMeter.meterType} " )
192
191
}
193
192
}
@@ -197,12 +196,14 @@ object ContextReceiver {
197
196
} else {
198
197
throw UnsupportedOperationException (" todo" ) // todo: this
199
198
}
199
+
200
200
MeterType .GAUGE -> {}
201
201
MeterType .HISTOGRAM -> if (liveMeter.metricValue.valueType == MetricValueType .NUMBER ) {
202
202
(baseMeter as Histogram ).addValue(liveMeter.metricValue.value.toDouble())
203
203
} else {
204
204
throw UnsupportedOperationException (" todo" ) // todo: this
205
205
}
206
+
206
207
else -> throw UnsupportedOperationException (" Unsupported meter type: ${liveMeter.meterType} " )
207
208
}
208
209
}
@@ -222,7 +223,11 @@ object ContextReceiver {
222
223
ContextManager .stopSpan(activeSpan)
223
224
}
224
225
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
+
226
231
return try {
227
232
String .format(
228
233
" {\" @class\" :\" %s\" ,\" @id\" :\" %s\" ,\" $varName \" :%s}" ,
0 commit comments