Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed implementation of TracingStatement when no segment is present. #137

Merged
merged 3 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,24 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl

Subsegment subsegment = createSubsegment();
if (subsegment == null) {
// don't trace if failed to create subsegment
return method.invoke(delegate, args);
try {
chamsco-aws marked this conversation as resolved.
Show resolved Hide resolved
// don't trace if failed to create subsegment
return method.invoke(delegate, args);
} catch (Throwable t) {
if (t instanceof InvocationTargetException) {
// the reflection may wrap the actual error with an InvocationTargetException.
// we want to use the root cause to make the instrumentation seamless
InvocationTargetException ite = (InvocationTargetException) t;
if (ite.getTargetException() != null) {
throw ite.getTargetException();
}
if (ite.getCause() != null) {
throw ite.getCause();
}
}

throw t;
}
}

logger.debug("Invoking statement execution with X-Ray tracing.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -25,6 +26,8 @@
import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Namespace;
import com.amazonaws.xray.entities.Subsegment;
import com.amazonaws.xray.strategy.ContextMissingStrategy;
import com.amazonaws.xray.strategy.IgnoreErrorContextMissingStrategy;

@RunWith(MockitoJUnitRunner.class)
public class TracingStatementTest {
Expand Down Expand Up @@ -169,7 +172,7 @@ public void testCalledStatementExecuteUpdate() throws Exception {
}

@Test
public void testCaptureException() throws Exception {
public void testCaptureRuntimeException() throws Exception {
Throwable exception = new RuntimeException("foo");
when(delegate.execute(SQL)).thenThrow(exception);
try {
Expand All @@ -183,6 +186,59 @@ public void testCaptureException() throws Exception {
}
}

@Test
public void testCaptureSqlException() throws Exception {
Throwable exception = new SQLException("foo");
when(delegate.execute(SQL)).thenThrow(exception);
try {
statement.execute(SQL);
fail("Expected exception is not thrown");
} catch (Throwable th) {
assertEquals(exception, th);
} finally {
assertEquals(exception, AWSXRay.getCurrentSegment().getSubsegments().get(0).getCause().getExceptions().get(0).getThrowable());
assertSubsegment();
}
}



@Test
public void testCaptureRuntimeExceptionWithoutSegment() throws Exception {
ContextMissingStrategy oldStrategy = AWSXRay.getGlobalRecorder().getContextMissingStrategy();
AWSXRay.getGlobalRecorder().setContextMissingStrategy(new IgnoreErrorContextMissingStrategy());
try {
Throwable exception = new RuntimeException("foo");
when(delegate.execute(SQL)).thenThrow(exception);
try {
statement.execute(SQL);
fail("Expected exception is not thrown");
} catch (Throwable th) {
assertEquals(exception, th);
}
} finally {
AWSXRay.getGlobalRecorder().setContextMissingStrategy(oldStrategy);
}
}

@Test
public void testCaptureSqlExceptionWithoutSegment() throws Exception {
ContextMissingStrategy oldStrategy = AWSXRay.getGlobalRecorder().getContextMissingStrategy();
AWSXRay.getGlobalRecorder().setContextMissingStrategy(new IgnoreErrorContextMissingStrategy());
try {
Throwable exception = new SQLException("foo");
when(delegate.execute(SQL)).thenThrow(exception);
try {
statement.execute(SQL);
fail("Expected exception is not thrown");
} catch (Throwable th) {
assertEquals(exception, th);
}
} finally {
AWSXRay.getGlobalRecorder().setContextMissingStrategy(oldStrategy);
}
}

@Test
public void testNonTracedMethod() throws Exception {
statement.close();
Expand Down