Skip to content

Commit

Permalink
Spring Framework Integration (#1)
Browse files Browse the repository at this point in the history
* Adding Spring modules for X-Ray.

* Adding POM description

* Fixing issues highlighted from PR #1

* Removing GPG maven plugin override

* Reformatting code

* Homologizing logging declarations

* abstracting spring version to property and removing SLF4J dependency

* referencing parent version for module version

* removing redundant groupId from POM

* adding newline at EOF

* Incorporating feedback from PR #1

* Adding newline at EOF

* updating POMs

* Adding module into main pom

* Removing beginSegment() per comments on PR #1
  • Loading branch information
andrewpowell authored and jamesdbowman committed Dec 19, 2017
1 parent c821d01 commit 01d52ae
Show file tree
Hide file tree
Showing 18 changed files with 334 additions and 75 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
.project
.classpath
.DS_Store

*.iml
/.idea
4 changes: 2 additions & 2 deletions aws-xray-recorder-sdk-apache-http/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mapven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.amazonaws</groupId>
Expand Down
4 changes: 2 additions & 2 deletions aws-xray-recorder-sdk-aws-sdk-instrumentor/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mapven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.amazonaws</groupId>
Expand Down
8 changes: 4 additions & 4 deletions aws-xray-recorder-sdk-aws-sdk/pom.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mapven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-pom</artifactId>
<version>1.2.2</version>
<version>1.2.2</version>
</parent>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-aws-sdk</artifactId>
<version>1.2.2</version>
<version>1.2.2</version>
<name>AWS X-Ray Recorder SDK for Java - AWS SDK Handler</name>
<build>
<plugins>
Expand Down
10 changes: 8 additions & 2 deletions aws-xray-recorder-sdk-bom/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mapven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.amazonaws</groupId>
Expand Down Expand Up @@ -49,6 +49,12 @@
<optional>false</optional>
<version>${awsxrayrecordersdk.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-spring</artifactId>
<optional>false</optional>
<version>${awsxrayrecordersdk.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
4 changes: 2 additions & 2 deletions aws-xray-recorder-sdk-core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mapven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.amazonaws</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package com.amazonaws.xray;

import com.amazonaws.xray.contexts.LambdaSegmentContextResolver;
import com.amazonaws.xray.contexts.SegmentContext;
import com.amazonaws.xray.contexts.SegmentContextResolverChain;
import com.amazonaws.xray.contexts.ThreadLocalSegmentContextResolver;
import com.amazonaws.xray.emitters.DefaultEmitter;
import com.amazonaws.xray.emitters.Emitter;
import com.amazonaws.xray.entities.*;
import com.amazonaws.xray.exceptions.SegmentNotFoundException;
import com.amazonaws.xray.exceptions.SubsegmentNotFoundException;
import com.amazonaws.xray.strategy.*;
import com.amazonaws.xray.strategy.sampling.DefaultSamplingStrategy;
import com.amazonaws.xray.strategy.sampling.SamplingStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
Expand All @@ -12,34 +27,6 @@
import java.util.function.Function;
import java.util.function.Supplier;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amazonaws.xray.contexts.LambdaSegmentContextResolver;
import com.amazonaws.xray.contexts.SegmentContext;
import com.amazonaws.xray.contexts.SegmentContextResolverChain;
import com.amazonaws.xray.contexts.ThreadLocalSegmentContextResolver;
import com.amazonaws.xray.emitters.DefaultEmitter;
import com.amazonaws.xray.emitters.Emitter;
import com.amazonaws.xray.entities.DummySegment;
import com.amazonaws.xray.entities.Entity;
import com.amazonaws.xray.entities.Segment;
import com.amazonaws.xray.entities.SegmentImpl;
import com.amazonaws.xray.entities.Subsegment;
import com.amazonaws.xray.entities.TraceID;
import com.amazonaws.xray.exceptions.SegmentNotFoundException;
import com.amazonaws.xray.exceptions.SubsegmentNotFoundException;
import com.amazonaws.xray.strategy.ContextMissingStrategy;
import com.amazonaws.xray.strategy.DefaultContextMissingStrategy;
import com.amazonaws.xray.strategy.DefaultPrioritizationStrategy;
import com.amazonaws.xray.strategy.DefaultStreamingStrategy;
import com.amazonaws.xray.strategy.DefaultThrowableSerializationStrategy;
import com.amazonaws.xray.strategy.PrioritizationStrategy;
import com.amazonaws.xray.strategy.StreamingStrategy;
import com.amazonaws.xray.strategy.ThrowableSerializationStrategy;
import com.amazonaws.xray.strategy.sampling.DefaultSamplingStrategy;
import com.amazonaws.xray.strategy.sampling.SamplingStrategy;

public class AWSXRayRecorder {

private static final Log logger = LogFactory.getLog(AWSXRayRecorder.class);
Expand Down Expand Up @@ -466,7 +453,7 @@ public Optional<Segment> getCurrentSegmentOptional() {
if (current instanceof Segment) {
return Optional.of((Segment) current);
} else if (current instanceof Subsegment) {
return Optional.of(((Subsegment) current).getParentSegment());
return Optional.of(current.getParentSegment());
} else {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
package com.amazonaws.xray;

import java.util.List;

import org.json.JSONException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

import com.amazonaws.xray.contexts.LambdaSegmentContext;
import com.amazonaws.xray.contexts.LambdaSegmentContextResolver;
import com.amazonaws.xray.emitters.Emitter;
Expand All @@ -35,6 +15,21 @@
import com.amazonaws.xray.strategy.RuntimeErrorContextMissingStrategy;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.json.JSONException;
import org.junit.*;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

import java.util.List;

@FixMethodOrder(MethodSorters.JVM)
@PrepareForTest({LambdaSegmentContext.class, LambdaSegmentContextResolver.class})
Expand Down Expand Up @@ -306,7 +301,7 @@ public void testSubsegmentNotEmittedWithoutExceptionInLambdaInitContext() {
}

@Test
public void testSubsegmentWithChildEmittedTogetherInLambdaContext() throws JSONException {
public void testSubsegmentWithChildEmittedTogetherInLambdaContext() {
TraceHeader header = TraceHeader.fromString(TRACE_HEADER);

PowerMockito.stub(PowerMockito.method(LambdaSegmentContext.class, "getTraceHeaderFromEnvironment")).toReturn(header);
Expand All @@ -329,7 +324,7 @@ public void testSubsegmentWithChildEmittedTogetherInLambdaContext() throws JSONE
}

@Test
public void testSubsequentSubsegmentBranchesEmittedInLambdaContext() throws JSONException {
public void testSubsequentSubsegmentBranchesEmittedInLambdaContext() {
TraceHeader header = TraceHeader.fromString(TRACE_HEADER);

PowerMockito.stub(PowerMockito.method(LambdaSegmentContext.class, "getTraceHeaderFromEnvironment")).toReturn(header);
Expand Down
88 changes: 88 additions & 0 deletions aws-xray-recorder-sdk-spring/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-pom</artifactId>
<version>1.2.2</version>
</parent>

<artifactId>aws-xray-recorder-sdk-spring</artifactId>
<version>${awsxrayrecordersdk.version}</version>
<name>AWS X-Ray Recorder SDK for Java - Spring Framework Interceptors</name>

<properties>
<spring.version>4.3.12.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-core</artifactId>
<version>${awsxrayrecordersdk.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.11</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.0.0.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<configuration>
<javadocDirectory>${basedir}/docs</javadocDirectory>
<docfilessubdirs>true</docfilessubdirs>
<overview>${basedir}/docs/overview.html</overview>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerVersion>1.8</compilerVersion>
<fork>true</fork>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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.Optional;

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

public abstract class AbstractXRayInterceptor {

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());
subsegment.setMetadata(XRayInterceptorUtils.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() {
}

/**
* @param pjp the proceeding join point
* @return the result of the method being wrapped
* @throws Throwable
*/
@Around("springRepositories()")
public Object traceAroundRepositoryMethods(ProceedingJoinPoint pjp) throws Throwable {
logger.trace("Advising repository");
boolean hasClassAnnotation = false;

for (Class<?> i : pjp.getTarget().getClass().getInterfaces()) {
if (i.getAnnotation(XRayEnabled.class) != null) {
hasClassAnnotation = true;
break;
}
}

if (hasClassAnnotation) {
return this.processXRayTrace(pjp);
} else {
return XRayInterceptorUtils.conditionalProceed(pjp);
}
}

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

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface XRayEnabled {


}
Loading

0 comments on commit 01d52ae

Please sign in to comment.