Skip to content

Commit

Permalink
Add a method to run a block of code with a segment mounted as the cur… (
Browse files Browse the repository at this point in the history
#240)

* Add a method to run a block of code with a segment mounted as the current entity.

* Deprecate setTraceEntity
  • Loading branch information
Anuraag Agrawal authored Nov 24, 2020
1 parent f66c655 commit ad6b333
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazonaws.xray;

import com.amazonaws.xray.contexts.SegmentContextExecutors;
import com.amazonaws.xray.entities.Entity;
import com.amazonaws.xray.entities.Segment;
import com.amazonaws.xray.entities.Subsegment;
Expand Down Expand Up @@ -186,6 +187,11 @@ public static void clearThreadLocal() {
globalRecorder.clearThreadLocal();
}

/**
* @deprecated Use {@link Entity#run(Runnable)} or methods in {@link SegmentContextExecutors} instead of directly setting
* the trace entity so it can be restored correctly.
*/
@Deprecated
public static void setTraceEntity(Entity entity) {
globalRecorder.setTraceEntity(entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.amazonaws.xray.contexts.LambdaSegmentContextResolver;
import com.amazonaws.xray.contexts.SegmentContext;
import com.amazonaws.xray.contexts.SegmentContextExecutors;
import com.amazonaws.xray.contexts.SegmentContextResolverChain;
import com.amazonaws.xray.contexts.ThreadLocalSegmentContextResolver;
import com.amazonaws.xray.emitters.Emitter;
Expand Down Expand Up @@ -753,7 +754,11 @@ private SegmentContext getSegmentContext() {
*
* @param entity
* the trace entity to set
*
* @deprecated Use {@link Entity#run(Runnable)} or methods in {@link SegmentContextExecutors} instead of directly setting
* the trace entity so it can be restored correctly.
*/
@Deprecated
public void setTraceEntity(@Nullable Entity entity) {
SegmentContext context = getSegmentContext();
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.AWSXRayRecorder;
import com.amazonaws.xray.entities.Entity;
import com.amazonaws.xray.entities.Segment;
import java.util.concurrent.Executor;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -80,13 +79,7 @@ private SegmentContextExecutor(AWSXRayRecorder recorder, Segment segment) {

@Override
public void execute(Runnable command) {
Entity previous = recorder.getTraceEntity();
recorder.setTraceEntity(segment);
try {
command.run();
} finally {
recorder.setTraceEntity(previous);
}
segment.run(command, recorder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ static String generateId() {
return AWSXRay.getGlobalRecorder().getIdGenerator().newEntityId();
}

/**
* Immediately runs the provided {@link Runnable} with this {@link Segment} as the current entity.
*/
default void run(Runnable runnable) {
run(runnable, getCreator());
}

/**
* Immediately runs the provided {@link Runnable} with this {@link Segment} as the current entity.
*/
default void run(Runnable runnable, AWSXRayRecorder recorder) {
Entity previous = recorder.getTraceEntity();
recorder.setTraceEntity(this);
try {
runnable.run();
} finally {
recorder.setTraceEntity(previous);
}
}

String getName();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,13 @@ private AWSXRayRecorder getRecorder() {

private void processEvent(AsyncEvent event) throws IOException {
AWSXRayRecorder recorder = getRecorder();
Entity prior = recorder.getTraceEntity();
try {
Entity entity = (Entity) event.getSuppliedRequest().getAttribute(ENTITY_ATTRIBUTE_KEY);
recorder.setTraceEntity(entity);
Entity entity = (Entity) event.getSuppliedRequest().getAttribute(ENTITY_ATTRIBUTE_KEY);
entity.run(() -> {
if (event.getThrowable() != null) {
entity.addException(event.getThrowable());
}
filter.postFilter(event.getSuppliedRequest(), event.getSuppliedResponse());
} finally {
recorder.setTraceEntity(prior);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

Expand All @@ -52,13 +53,13 @@ public class SegmentContextExecutorsTest {
@Mock
private volatile AWSXRayRecorder recorder;

@Mock
@Spy
private volatile Segment current;

@Mock
@Spy
private volatile Segment manual;

@Mock
@Spy
private volatile Segment previous;

@BeforeClass
Expand Down
2 changes: 1 addition & 1 deletion dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ val DEPENDENCY_SETS = listOf(
),
DependencySet(
"org.mockito",
"2.28.2",
"3.6.0",
listOf("mockito-all", "mockito-core", "mockito-junit-jupiter")
)
)
Expand Down

0 comments on commit ad6b333

Please sign in to comment.