我有一个方法需要检测来调用 New Relic:设置一个段,运行业务逻辑并结束该段。有没有办法在 Kotlin 中做到这一点(如在 Spring AOP 中)?
fun saveCustomer() {
val segment = NewRelic.getAgent().transaction.startSegment("save customer")
// business logic here
segment.end()
}
我尝试隔离
newRelic
依赖,现在可以在我的整个应用程序中重用它:fun saveCustomer() {
newRelic.executeWithSegment { // this starts/ends the segment and calls the function block
// business logic here
}
}
然而,这使得单元测试
saveCustomer
更难,因为在 mock newRelic.executeWithSegment
之后(我必须这样做;否则在测试中会联系 New Relic),代码块(业务逻辑)不再执行 - 所以测试失败。有没有办法满足这些要求? (可能带有注释或使用 Kotlin 委托(delegate)模式,甚至是一些轻量级库;不确定。)
最佳答案
您可以使用 AspectJ。
关于kotlin - 如何在 Kotlin 中进行方法检测 - 但保持方法可测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57915364/