mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
Add AOP to capture SQL
This commit is contained in:
parent
2cf6a34add
commit
1620b71165
1 changed files with 47 additions and 0 deletions
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue