feat(trace): support create advice for method with no return value

This commit is contained in:
2026-04-13 21:57:10 +08:00
parent fece67135f
commit e65d3302c6

View File

@@ -30,7 +30,7 @@ object LogAdviceProvider : Configurable, ConfigRegistration<AdviceLoggingConfig>
inputType: Class<I>, inputType: Class<I>,
outputType: Class<O>, outputType: Class<O>,
meta: Map<String, Any> = emptyMap(), meta: Map<String, Any> = emptyMap(),
invoker: (I) -> O invoker: (I) -> O?
): LogAdvice<I, O> { ): LogAdvice<I, O> {
return LogAdvice( return LogAdvice(
adviceTarget = adviceTarget, adviceTarget = adviceTarget,
@@ -61,7 +61,7 @@ object LogAdviceProvider : Configurable, ConfigRegistration<AdviceLoggingConfig>
class LogAdvice<I, O> internal constructor( class LogAdvice<I, O> internal constructor(
val adviceTarget: String, val adviceTarget: String,
private val invoker: (I) -> O, private val invoker: (I) -> O?,
private val adviceMeta: AdviceMeta private val adviceMeta: AdviceMeta
) { ) {
@@ -69,7 +69,7 @@ class LogAdvice<I, O> internal constructor(
private val log = LoggerFactory.getLogger(LogAdvice::class.java) private val log = LoggerFactory.getLogger(LogAdvice::class.java)
} }
fun invoke(input: I): Result<O> { fun invoke(input: I): Result<O?> {
val startAt = ZonedDateTime.now() val startAt = ZonedDateTime.now()
return try { return try {
logEnter(input) logEnter(input)
@@ -92,7 +92,7 @@ class LogAdvice<I, O> internal constructor(
} }
} }
private fun logOutput(output: O) { private fun logOutput(output: O?) {
when (LogAdviceProvider.logLevel) { when (LogAdviceProvider.logLevel) {
AdviceLoggingConfig.LogLevel.NONE -> return AdviceLoggingConfig.LogLevel.NONE -> return
AdviceLoggingConfig.LogLevel.ABSTRACT -> log.info("${adviceMeta.adviceTarget} ended.") AdviceLoggingConfig.LogLevel.ABSTRACT -> log.info("${adviceMeta.adviceTarget} ended.")
@@ -100,7 +100,7 @@ class LogAdvice<I, O> internal constructor(
try { try {
log.info("${adviceMeta.adviceTarget} ended with output: ${JSONObject.toJSONString(output)}") log.info("${adviceMeta.adviceTarget} ended with output: ${JSONObject.toJSONString(output)}")
} catch (_: Exception) { } catch (_: Exception) {
log.info("${adviceMeta.adviceTarget} ended with output: ${output.toString()}, which cannot be printed as json string.") log.info("${adviceMeta.adviceTarget} ended with output: ${output ?: "null"}, which cannot be printed as json string.")
} }
} }
} }
@@ -120,17 +120,9 @@ class LogAdvice<I, O> internal constructor(
} }
} }
private fun createResult(input: I, output: O, startAt: ZonedDateTime) { private fun createResult(input: I, output: O?, startAt: ZonedDateTime) {
val inputSerialized = try { val inputSerialized = serializeRequired(input)
JSONObject.toJSONString(input) val outputSerialized = serializeNullable(output)
} catch (_: JSONException) {
input.toString()
}
val outputSerialized = try {
JSONObject.toJSONString(output)
} catch (_: JSONException) {
output.toString()
}
LogAdviceProvider.record( LogAdviceProvider.record(
AdviceResult.Normal( AdviceResult.Normal(
adviceTarget, adviceTarget,
@@ -143,11 +135,7 @@ class LogAdvice<I, O> internal constructor(
} }
private fun createUnexpectedResult(input: I, throwable: Throwable, startAt: ZonedDateTime) { /* 落盘 */ private fun createUnexpectedResult(input: I, throwable: Throwable, startAt: ZonedDateTime) { /* 落盘 */
val inputSerialized = try { val inputSerialized = serializeRequired(input)
JSONObject.toJSONString(input)
} catch (_: JSONException) {
input.toString()
}
LogAdviceProvider.record( LogAdviceProvider.record(
AdviceResult.Unexpected( AdviceResult.Unexpected(
adviceTarget, adviceTarget,
@@ -159,6 +147,26 @@ class LogAdvice<I, O> internal constructor(
) )
) )
} }
private fun serializeRequired(value: Any?): String {
return try {
JSONObject.toJSONString(value)
} catch (_: JSONException) {
value?.toString() ?: "null"
}
}
private fun serializeNullable(value: Any?): String? {
return if (value == null) {
null
} else {
try {
JSONObject.toJSONString(value)
} catch (_: JSONException) {
value.toString()
}
}
}
} }
data class AdviceMeta( data class AdviceMeta(
@@ -191,7 +199,7 @@ sealed class AdviceResult {
override val input: String, override val input: String,
override val startAt: ZonedDateTime, override val startAt: ZonedDateTime,
override val adviceMeta: AdviceMeta, override val adviceMeta: AdviceMeta,
val output: String, val output: String?,
) : AdviceResult() { ) : AdviceResult() {
override val type: Type = Type.NORMAL override val type: Type = Type.NORMAL
} }