Skip to content

Commit

Permalink
Merge pull request aws#115 from dekelpilli/decouple-spring-jpa
Browse files Browse the repository at this point in the history
Added interceptor without spring data dependency
  • Loading branch information
shengxil authored Jan 16, 2020
2 parents e653fbc + 8813491 commit cc6d6b9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,71 +1,16 @@
package com.amazonaws.xray.spring.aop;

import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Segment;
import com.amazonaws.xray.entities.Subsegment;
import com.amazonaws.xray.exceptions.SegmentNotFoundException;
import com.amazonaws.xray.strategy.ContextMissingStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;

import java.util.Map;
import java.util.Optional;

import static com.amazonaws.xray.AWSXRay.getCurrentSegmentOptional;

public abstract class AbstractXRayInterceptor {
public abstract class AbstractXRayInterceptor extends BaseAbstractXRayInterceptor {

private static final Log logger = LogFactory.getLog(AbstractXRayInterceptor.class);

private static ContextMissingStrategy getContextMissingStrategy() {
return AWSXRay.getGlobalRecorder().getContextMissingStrategy();
}

private static Segment getCurrentSegment() {
Optional<Segment> segment = getCurrentSegmentOptional();
if (segment.isPresent()) {
return segment.get();
}
ContextMissingStrategy contextMissingStrategy = getContextMissingStrategy();
contextMissingStrategy.contextMissing("No segment in progress.", SegmentNotFoundException.class);
return null;
}

/**
* @param pjp the proceeding join point
* @return the result of the method being wrapped
* @throws Throwable
*/
@Around("xrayTracedClasses() || xrayEnabledClasses()")
public Object traceAroundMethods(ProceedingJoinPoint pjp) throws Throwable {
return this.processXRayTrace(pjp);
}

protected Object processXRayTrace(ProceedingJoinPoint pjp) throws Throwable {
try {
Subsegment subsegment = AWSXRay.beginSubsegment(pjp.getSignature().getName());
if(subsegment != null) {
subsegment.setMetadata(generateMetadata(pjp, subsegment));
}
return XRayInterceptorUtils.conditionalProceed(pjp);
} catch (Exception e) {
AWSXRay.getCurrentSegment().addException(e);
throw e;
} finally {
logger.trace("Ending Subsegment");
AWSXRay.endSubsegment();
}
}

protected abstract void xrayEnabledClasses();

@Pointcut("execution(* XRayTraced+.*(..))")
protected void xrayTracedClasses() {
}

@Pointcut("execution(public !void org.springframework.data.repository.Repository+.*(..))")
protected void springRepositories() {
}
Expand Down Expand Up @@ -93,9 +38,4 @@ public Object traceAroundRepositoryMethods(ProceedingJoinPoint pjp) throws Throw
return XRayInterceptorUtils.conditionalProceed(pjp);
}
}

protected Map<String, Map<String, Object>> generateMetadata(ProceedingJoinPoint pjp, Subsegment subsegment) {
return XRayInterceptorUtils.generateMetadata(pjp, subsegment);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.amazonaws.xray.spring.aop;

import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Segment;
import com.amazonaws.xray.entities.Subsegment;
import com.amazonaws.xray.exceptions.SegmentNotFoundException;
import com.amazonaws.xray.strategy.ContextMissingStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;

import java.util.Map;
import java.util.Optional;

import static com.amazonaws.xray.AWSXRay.getCurrentSegmentOptional;


/**
* Allows for use of this library without Spring Data JPA being in the classpath.
* For projects using Spring Data JPA, consider using {@link AbstractXRayInterceptor} instead.
*/
public abstract class BaseAbstractXRayInterceptor {

private static final Log logger = LogFactory.getLog(BaseAbstractXRayInterceptor.class);

private static ContextMissingStrategy getContextMissingStrategy() {
return AWSXRay.getGlobalRecorder().getContextMissingStrategy();
}

private static Segment getCurrentSegment() {
Optional<Segment> segment = getCurrentSegmentOptional();
if (segment.isPresent()) {
return segment.get();
}
ContextMissingStrategy contextMissingStrategy = getContextMissingStrategy();
contextMissingStrategy.contextMissing("No segment in progress.", SegmentNotFoundException.class);
return null;
}

/**
* @param pjp the proceeding join point
* @return the result of the method being wrapped
* @throws Throwable
*/
@Around("xrayTracedClasses() || xrayEnabledClasses()")
public Object traceAroundMethods(ProceedingJoinPoint pjp) throws Throwable {
return this.processXRayTrace(pjp);
}

protected Object processXRayTrace(ProceedingJoinPoint pjp) throws Throwable {
try {
Subsegment subsegment = AWSXRay.beginSubsegment(pjp.getSignature().getName());
if(subsegment != null) {
subsegment.setMetadata(generateMetadata(pjp, subsegment));
}
return XRayInterceptorUtils.conditionalProceed(pjp);
} catch (Exception e) {
AWSXRay.getCurrentSegment().addException(e);
throw e;
} finally {
logger.trace("Ending Subsegment");
AWSXRay.endSubsegment();
}
}

protected abstract void xrayEnabledClasses();

@Pointcut("execution(* XRayTraced+.*(..))")
protected void xrayTracedClasses() {
}

protected Map<String, Map<String, Object>> generateMetadata(ProceedingJoinPoint pjp, Subsegment subsegment) {
return XRayInterceptorUtils.generateMetadata(pjp, subsegment);
}
}

0 comments on commit cc6d6b9

Please sign in to comment.