Skip to content

Commit

Permalink
Support log correlation during entity propagation
Browse files Browse the repository at this point in the history
* Supports manual cross-thread tracing
* Tested in demo app

Signed-off-by: Michael Lee <[email protected]>
  • Loading branch information
c1tadel committed Feb 4, 2020
1 parent 5f252fe commit 837fb6a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.amazonaws.xray.contexts;

import java.util.Objects;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amazonaws.xray.AWSXRayRecorder;
import com.amazonaws.xray.ThreadLocalStorage;
import com.amazonaws.xray.entities.Entity;
Expand All @@ -25,10 +25,20 @@ default Entity getTraceEntity() {
}

default void setTraceEntity(Entity entity) {
if(entity != null && entity.getCreator() != null) {
entity.getCreator().getSegmentListeners().stream().filter(Objects::nonNull).forEach(l -> {
l.onSetEntity(ThreadLocalStorage.get(), entity);
});
}
ThreadLocalStorage.set(entity);
}

default void clearTraceEntity() {
Entity oldEntity = ThreadLocalStorage.get();
if(oldEntity != null && oldEntity.getCreator() != null)
oldEntity.getCreator().getSegmentListeners().stream().filter(Objects::nonNull).forEach(l -> {
l.onClearEntity(oldEntity);
});
ThreadLocalStorage.clear();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.amazonaws.xray.listeners;

import com.amazonaws.xray.entities.Entity;
import com.amazonaws.xray.entities.Segment;

/**
Expand Down Expand Up @@ -41,4 +42,25 @@ default void beforeEndSegment(Segment segment) {
default void afterEndSegment(Segment segment) {

}

/**
* onSetEntity is invoked when the SegmentContext is being updated with a new entity.
* Both the new entity and the previous entity (or null if unset) are passed.
*
* @param previousEntity
* @param newEntity
*/
default void onSetEntity(Entity previousEntity, Entity newEntity) {

}

/**
* onClearEntity is invoked just before the SegmentContext is cleared.
*
* @param previousEntity
*/
default void onClearEntity(Entity previousEntity) {

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.amazonaws.xray.log4j;

import com.amazonaws.xray.entities.Segment;
import com.amazonaws.xray.entities.Entity;
import com.amazonaws.xray.listeners.SegmentListener;
import org.apache.logging.log4j.ThreadContext;

Expand All @@ -17,24 +17,25 @@ public class Log4JSegmentListener implements SegmentListener {
/**
* Maps the AWS-XRAY-TRACE-ID key to the trace ID of the segment that's just been created in the Log4J ThreadContext.
*
* @param segment
* @param oldEntity the previous entity or null
* @param newEntity the new entity
* The segment that has just begun
*/
@Override
public void onBeginSegment(Segment segment) {
if (segment != null) {
ThreadContext.put(TRACE_ID_KEY, TRACE_ID_KEY + ": " + segment.getTraceId().toString());
public void onSetEntity(Entity oldEntity, Entity newEntity) {
if (newEntity != null && newEntity.getTraceId() != null) {
ThreadContext.put(TRACE_ID_KEY, TRACE_ID_KEY + ": " + newEntity.getTraceId().toString());
}
}

/**
* Removes the AWS-XRAY-TRACE-ID key from the ThreadContext upon the completion of each segment.
*
* @param segment
* @param entity
* The segment that has just ended
*/
@Override
public void beforeEndSegment(Segment segment) {
public void onClearEntity(Entity entity) {
ThreadContext.remove(TRACE_ID_KEY);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.amazonaws.xray.slf4j;

import com.amazonaws.xray.entities.Segment;
import com.amazonaws.xray.entities.Entity;
import com.amazonaws.xray.listeners.SegmentListener;
import org.slf4j.MDC;

Expand All @@ -18,26 +18,25 @@ public class SLF4JSegmentListener implements SegmentListener {
/**
* Maps the AWS-XRAY-TRACE-ID key to the trace ID of the segment that's just been created in the MDC.
*
* @param segment
* @param oldEntity the previous entity or null
* @param newEntity the new entity
* The segment that has just begun
*/
@Override
public void onBeginSegment(Segment segment) {

// TODO: Check if we are in a Lambda environment (dealing w/ facade segment), and grab the Trace ID from the Header instead if so
if (segment != null) {
MDC.put(TRACE_ID_KEY, TRACE_ID_KEY + ": " + segment.getTraceId().toString());
public void onSetEntity(Entity oldEntity, Entity newEntity) {
if (newEntity != null && newEntity.getTraceId() != null) {
MDC.put(TRACE_ID_KEY, TRACE_ID_KEY + ": " + newEntity.getTraceId().toString());
}
}

/**
* Removes the AWS-XRAY-TRACE-ID key from the MDC upon the completion of each segment.
*
* @param segment
* @param entity
* The segment that has just ended
*/
@Override
public void beforeEndSegment(Segment segment) {
public void onClearEntity(Entity entity) {
MDC.remove(TRACE_ID_KEY);
}
}

0 comments on commit 837fb6a

Please sign in to comment.