Спасибо за комментарии к моему вопросу, как и предполагалось, я реализовал его с помощью АОП.Вот мое решение, оно, возможно, будет кому-нибудь полезно.
Добавить зависимость AOP к pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Я создал пользовательский атрибут TrackLatency.java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TrackLatency {
String value() default "Track Latency Span";
}
Чем я добавил аспект, который добавляет span для каждого метода свызов атрибута
@Aspect
@Configuration
@RequiredArgsConstructor
public class CreateNewSpanMethodAspect {
private final Tracer tracer;
@Around("@annotation(classpath.TrackLatency)")
public Object around(ProceedingJoinPoint call) throws Throwable {
Span span = tracer.createSpan(getTrackLatencyAnnotationValue(call));
try{
return call.proceed();
} finally {
tracer.close(span);
}
}
private static String getTrackLatencyAnnotationValue(ProceedingJoinPoint call){
MethodSignature signature = (MethodSignature) call.getSignature();
Method method = signature.getMethod();
TrackLatency trackLatencyAnnotation = method.getAnnotation(TrackLatency.class);
return trackLatencyAnnotation.value();
}
}
Использование:
@TrackLatency("dbCall: myDbCall")
public List<QuestionAnswerRow> myDbCall(....) {
// no changes in my repository
}