From 1620b711657c4e5fa7e337d34abe984ee755f808 Mon Sep 17 00:00:00 2001 From: machih Date: Mon, 30 Nov 2020 15:55:10 +0900 Subject: [PATCH] Add AOP to capture SQL --- .../samples/petclinic/PetClinicAspect.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/org/springframework/samples/petclinic/PetClinicAspect.java diff --git a/src/main/java/org/springframework/samples/petclinic/PetClinicAspect.java b/src/main/java/org/springframework/samples/petclinic/PetClinicAspect.java new file mode 100644 index 000000000..d61d28471 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/PetClinicAspect.java @@ -0,0 +1,47 @@ +package org.springframework.samples.petclinic; + +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.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import brave.Span; +import brave.Span.Kind; +import brave.Tracer; + +@Aspect +@Component +public class PetClinicAspect { + + @Value("${petclinic.db.type:local}") + String dbType; + + @Value("${petclinic.db.instance:localDB}") + String dbInstance; + + @Autowired + Tracer tracer; + + // @Around("execution(* org.springframework.data.repository.Repository+.*(..)))") + @Around("execution(* org.springframework.samples.petclinic.*.*Repository+.*(..)))") + public Object AddSpan(ProceedingJoinPoint joinpoint) throws Throwable { + Span newSpan = this.tracer.nextSpan().name(joinpoint.getSignature().toString()).start(); + + try { + newSpan.tag("component", "java-jdbc"); + newSpan.kind(Kind.CLIENT); + newSpan.tag("db.type", dbType); + newSpan.tag("db.instance", dbInstance); + Object result = joinpoint.proceed(); + + return result; + } + finally { + newSpan.finish(); + } + + } + +} \ No newline at end of file