mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-27 17:35:49 +00:00
Refactor AOP for repository
This commit is contained in:
parent
f2377209e8
commit
5ee05f6e3a
1 changed files with 52 additions and 0 deletions
|
@ -0,0 +1,52 @@
|
|||
package org.springframework.samples.petclinic.system;
|
||||
|
||||
import brave.Span;
|
||||
import brave.Span.Kind;
|
||||
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.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class AddSpansToRepository {
|
||||
|
||||
@Value("${petclinic.db.type:local}")
|
||||
String dbType;
|
||||
|
||||
@Value("${petclinic.db.instance:localDB}")
|
||||
String dbInstance;
|
||||
|
||||
final Tracer tracer;
|
||||
|
||||
public AddSpansToRepository(Tracer tracer) {
|
||||
this.tracer = tracer;
|
||||
}
|
||||
|
||||
// @Around("execution(* org.springframework.data.repository.Repository+.*(..)))")
|
||||
@Around("execution(* org.springframework.samples.petclinic.*.*Repository+.*(..))")
|
||||
public Object AddSpan(ProceedingJoinPoint joinpoint) throws Throwable {
|
||||
String joinPointName = joinpoint.getSignature().toString();
|
||||
Span newSpan = this.tracer.nextSpan().name(joinPointName).start();
|
||||
|
||||
newSpan.kind(Kind.CLIENT);
|
||||
newSpan.tag("component", "java-jdbc");
|
||||
newSpan.tag("db.type", dbType);
|
||||
newSpan.tag("db.instance", dbInstance);
|
||||
|
||||
try {
|
||||
return joinpoint.proceed();
|
||||
}
|
||||
catch (RuntimeException | Error e) {
|
||||
newSpan.error(e);
|
||||
throw e;
|
||||
}
|
||||
finally {
|
||||
newSpan.finish();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue