Skip to content

Instantly share code, notes, and snippets.

@soudmaijer
Created August 7, 2020 08:29
Show Gist options
  • Save soudmaijer/89a41bfcbfe99d330f11fc797b7f6397 to your computer and use it in GitHub Desktop.
Save soudmaijer/89a41bfcbfe99d330f11fc797b7f6397 to your computer and use it in GitHub Desktop.
import brave.Tracer
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
@Aspect
@Component
class TracingAspect {
@Autowired
lateinit var tracer: Tracer
@Around("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)")
@Throws(Throwable::class)
fun aroundHystrixCommand(joinPoint: ProceedingJoinPoint): Any? {
return newSpan(joinPoint)
}
@Around("@within(org.springframework.stereotype.Service) && execution(public * *(..))")
@Throws(Throwable::class)
fun aroundServiceMethods(joinPoint: ProceedingJoinPoint): Any? {
return newSpan(joinPoint)
}
@Around("@within(org.springframework.web.bind.annotation.RestController) && execution(public * *(..))")
@Throws(Throwable::class)
fun aroundRestControllerMethods(joinPoint: ProceedingJoinPoint): Any? {
return newSpan(joinPoint)
}
fun newSpan(joinPoint: ProceedingJoinPoint): Any? {
val newSpan = tracer.nextSpan().name(joinPoint.signature.declaringType.simpleName +"-"+ joinPoint.signature.name).start()
try {
return joinPoint.proceed();
} finally {
newSpan.finish()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment