我在 Spring Boot (2.7.12) 应用程序中有以下服务类
@Service
class GetAllUsers(val userRepository: UserRepository) {
operator fun invoke(): List<User> {
return userRepository.findAll()
}
}
我正在使用
DD_TRACE_METHODS
env var 来追踪它
DD_TRACE_METHOD: "com.application.user.GetAllUsers[invoke]"
但是,当我进行 API 调用时,跟踪中仅显示 Controller 和 Repository 方法 (
UsersController.getAll --> UserRepository.findAll
)。
甚至奇怪。如果我在
invoke
方法中添加一个方法并将其添加到要跟踪的方法列表中,DataDog 将在跟踪中显示该类和调用方法。示例:
@Service
class GetAllUsers(val userRepository: UserRepository) {
operator fun invoke(): List<User> {
hello()
return userRepository.findAll()
}
fun hello(){
//Does nothing
}
}
DD_TRACE_METHOD: "com.application.user.GetAllUsers[invoke, hello]"
踪迹:
UsersController.getAll --> GetAllUsers.invoke --> UserRepository.findAll
如何在不创建该方法的情况下正确显示调用方法的痕迹?
您面临的问题是由于 Spring Boot 和 DataDog 跟踪协同工作的方式造成的。默认情况下,Spring Boot 不会为
invoke
方法创建单独的跨度,因为它被视为内部实现细节。
要在跟踪中包含
invoke
方法而不添加像 hello()
这样的额外方法,您可以尝试以下操作:
dd.trace.methods= com.application.user.GetAllUsers[invoke]
dd.trace.spring.annotation.service.enabled=true
这将启用对
invoke
方法的跟踪并将其包含在跨度中。
使用
@Trace
注释:将 @Trace
包中的 io.opentracing.spring.annotation
注释添加到您的 GetAllUsers
类中:
@Service
@Trace
class GetAllUsers(val userRepository: UserRepository) {
operator fun invoke(): List<User> {
return userRepository.findAll()
}
}
这将显式启用对
GetAllUsers
类及其方法的跟踪,包括 invoke
方法。
应用这些更改后,您应该会看到跟踪中包含
GetAllUsers.invoke
方法,而无需添加像 hello()
这样的额外方法。
希望我能提供帮助 - 如果您对我的回答满意或不满意,请告诉我。谢谢。