use of com.amazonaws.xray.listeners.SegmentListener in project aws-xray-sdk-java by aws.
the class LambdaSegmentContext method endSubsegment.
@Override
public void endSubsegment(AWSXRayRecorder recorder) {
Entity current = getTraceEntity();
if (current instanceof Subsegment) {
if (logger.isDebugEnabled()) {
if (current.getName().isEmpty() && !current.getParentSegment().isSampled()) {
logger.debug("Ending no-op subsegment");
} else {
logger.debug("Ending subsegment named: " + current.getName());
}
}
Subsegment currentSubsegment = (Subsegment) current;
List<SegmentListener> segmentListeners = recorder.getSegmentListeners();
segmentListeners.stream().filter(Objects::nonNull).forEach(listener -> listener.beforeEndSubsegment(currentSubsegment));
currentSubsegment.end();
if (recorder.getStreamingStrategy().requiresStreaming(currentSubsegment.getParentSegment())) {
recorder.getStreamingStrategy().streamSome(currentSubsegment.getParentSegment(), recorder.getEmitter());
}
segmentListeners.stream().filter(Objects::nonNull).forEach(listener -> listener.afterEndSubsegment(currentSubsegment));
Entity parentEntity = current.getParent();
if (parentEntity instanceof FacadeSegment) {
if (((FacadeSegment) parentEntity).isSampled()) {
current.getCreator().getEmitter().sendSubsegment((Subsegment) current);
}
clearTraceEntity();
} else {
setTraceEntity(current.getParent());
}
} else {
recorder.getContextMissingStrategy().contextMissing("Failed to end subsegment: subsegment cannot be found.", SubsegmentNotFoundException.class);
}
}
use of com.amazonaws.xray.listeners.SegmentListener in project aws-xray-sdk-java by aws.
the class ThreadLocalSegmentContext method endSubsegment.
@Override
public void endSubsegment(AWSXRayRecorder recorder) {
Entity current = getTraceEntity();
if (current instanceof Subsegment) {
if (logger.isDebugEnabled()) {
if (current.getName().isEmpty() && !current.getParentSegment().isSampled()) {
logger.debug("Ending no-op subsegment");
} else {
logger.debug("Ending subsegment named: " + current.getName());
}
}
Subsegment currentSubsegment = (Subsegment) current;
List<SegmentListener> segmentListeners = recorder.getSegmentListeners();
segmentListeners.stream().filter(Objects::nonNull).forEach(listener -> listener.beforeEndSubsegment(currentSubsegment));
if (currentSubsegment.end()) {
recorder.sendSegment(currentSubsegment.getParentSegment());
} else {
if (recorder.getStreamingStrategy().requiresStreaming(currentSubsegment.getParentSegment())) {
recorder.getStreamingStrategy().streamSome(currentSubsegment.getParentSegment(), recorder.getEmitter());
}
segmentListeners.stream().filter(Objects::nonNull).forEach(listener -> listener.afterEndSubsegment(currentSubsegment));
setTraceEntity(current.getParent());
}
} else {
recorder.getContextMissingStrategy().contextMissing("Failed to end subsegment: subsegment cannot be found.", SubsegmentNotFoundException.class);
}
}
use of com.amazonaws.xray.listeners.SegmentListener in project aws-xray-sdk-java by aws.
the class ThreadLocalSegmentContext method beginSubsegment.
@Override
public Subsegment beginSubsegment(AWSXRayRecorder recorder, String name) {
Entity current = getTraceEntity();
if (current == null) {
recorder.getContextMissingStrategy().contextMissing("Failed to begin subsegment named '" + name + "': segment cannot be found.", SegmentNotFoundException.class);
return Subsegment.noOp(recorder, false);
}
if (logger.isDebugEnabled()) {
logger.debug("Beginning subsegment named: " + name);
}
Segment parentSegment = current.getParentSegment();
Subsegment subsegment = parentSegment.isRecording() ? new SubsegmentImpl(recorder, name, parentSegment) : Subsegment.noOp(parentSegment, recorder);
subsegment.setParent(current);
current.addSubsegment(subsegment);
setTraceEntity(subsegment);
List<SegmentListener> segmentListeners = recorder.getSegmentListeners();
segmentListeners.stream().filter(Objects::nonNull).forEach(listener -> listener.onBeginSubsegment(subsegment));
return subsegment;
}
use of com.amazonaws.xray.listeners.SegmentListener in project aws-xray-sdk-java by aws.
the class LambdaSegmentContext method beginSubsegment.
@Override
public Subsegment beginSubsegment(AWSXRayRecorder recorder, String name) {
if (logger.isDebugEnabled()) {
logger.debug("Beginning subsegment named: " + name);
}
Entity entity = getTraceEntity();
if (entity == null) {
// First subsgment of a subsegment branch.
Segment parentSegment = newFacadeSegment(recorder, name);
Subsegment subsegment = parentSegment.isRecording() ? new SubsegmentImpl(recorder, name, parentSegment) : Subsegment.noOp(parentSegment, recorder);
subsegment.setParent(parentSegment);
// Enable FacadeSegment to keep track of its subsegments for subtree streaming
parentSegment.addSubsegment(subsegment);
setTraceEntity(subsegment);
return subsegment;
} else {
// Continuation of a subsegment branch.
Subsegment parentSubsegment = (Subsegment) entity;
// Ensure customers have not leaked subsegments across invocations
TraceID environmentRootTraceId = LambdaSegmentContext.getTraceHeaderFromEnvironment().getRootTraceId();
if (environmentRootTraceId != null && !environmentRootTraceId.equals(parentSubsegment.getParentSegment().getTraceId())) {
clearTraceEntity();
return beginSubsegment(recorder, name);
}
Segment parentSegment = parentSubsegment.getParentSegment();
Subsegment subsegment = parentSegment.isRecording() ? new SubsegmentImpl(recorder, name, parentSegment) : Subsegment.noOp(parentSegment, recorder);
subsegment.setParent(parentSubsegment);
parentSubsegment.addSubsegment(subsegment);
setTraceEntity(subsegment);
List<SegmentListener> segmentListeners = recorder.getSegmentListeners();
segmentListeners.stream().filter(Objects::nonNull).forEach(listener -> listener.onBeginSubsegment(subsegment));
return subsegment;
}
}
Aggregations