我有一个从spring-hateoas返回VndError的@ControllerAdvice。VndError指定了一个logRef字段,我应该将Spring Cloud Sleuth中的跟踪ID放在这个字段中。有没有正式的方法可以做到这一点,或者我应该直接在我的@ControllerAdvice @ExceptionHandler方法中从MDC中检索它?
示例控制器建议:
@ControllerAdvice
class ExceptionHandler {
@Autowired
private lateinit var tracer: Tracer
@ExceptionHandler(LocalException::class)
fun handleLocalException(e: LocalException): ResponseEntity<VndErrors.VndError> {
val traceId = MDC.get("X-B3-TraceId") // <-- Is this the correct way of getting trace id from Sleuth?
val traceId2 = tracer.currentSpan().context().traceIdString() // <-- Or something like this?
val error = VndErrors.VndError(traceId, e.message)
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error)
}
}GitHub示例:https://github.com/hughwphamill/spring-traceid-logref
发布于 2018-05-31 19:54:14
在文档中,我们已经描述了如何访问跟踪上下文。通过tracer接口获取跟踪id。
https://stackoverflow.com/questions/50588253
复制相似问题