• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.contrib.spring.aop;
18 
19 import io.opencensus.trace.Tracer;
20 import java.lang.reflect.Method;
21 import org.aspectj.lang.ProceedingJoinPoint;
22 import org.aspectj.lang.annotation.Around;
23 import org.aspectj.lang.annotation.Aspect;
24 import org.aspectj.lang.reflect.MethodSignature;
25 import org.springframework.beans.factory.annotation.Configurable;
26 
27 /**
28  * CensusSpringAspect handles logic for the `@Traced` annotation.
29  *
30  * @since 0.16.0
31  */
32 @Aspect
33 @Configurable
34 public final class CensusSpringAspect {
35   private final Tracer tracer;
36 
37   /**
38    * Creates a {@code CensusSpringAspect} with the given tracer.
39    *
40    * @param tracer the tracer responsible for building new spans
41    * @since 0.16.0
42    */
CensusSpringAspect(Tracer tracer)43   public CensusSpringAspect(Tracer tracer) {
44     this.tracer = tracer;
45   }
46 
47   /**
48    * trace handles methods executed with the `@Traced` annotation. A new span will be created with
49    * an optionally customizable span name.
50    *
51    * @param call the join point to execute
52    * @return the result of the invocation
53    * @throws Throwable if the underlying target throws an exception
54    * @since 0.16.0
55    */
56   @Around("@annotation(io.opencensus.contrib.spring.aop.Traced)")
trace(ProceedingJoinPoint call)57   public Object trace(ProceedingJoinPoint call) throws Throwable {
58     MethodSignature signature = (MethodSignature) call.getSignature();
59     Method method = signature.getMethod();
60 
61     Traced annotation = method.getAnnotation(Traced.class);
62     if (annotation == null) {
63       return call.proceed();
64     }
65     String spanName = annotation.name();
66     if (spanName.isEmpty()) {
67       spanName = method.getName();
68     }
69 
70     return Handler.proceed(call, tracer, spanName);
71   }
72 }
73