Search in sources :

Example 1 with SegmentListener

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);
    }
}
Also used : Entity(com.amazonaws.xray.entities.Entity) SegmentListener(com.amazonaws.xray.listeners.SegmentListener) Subsegment(com.amazonaws.xray.entities.Subsegment) FacadeSegment(com.amazonaws.xray.entities.FacadeSegment)

Example 2 with SegmentListener

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);
    }
}
Also used : Entity(com.amazonaws.xray.entities.Entity) SegmentListener(com.amazonaws.xray.listeners.SegmentListener) Subsegment(com.amazonaws.xray.entities.Subsegment)

Example 3 with SegmentListener

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;
}
Also used : Entity(com.amazonaws.xray.entities.Entity) SegmentListener(com.amazonaws.xray.listeners.SegmentListener) SubsegmentImpl(com.amazonaws.xray.entities.SubsegmentImpl) Subsegment(com.amazonaws.xray.entities.Subsegment) Segment(com.amazonaws.xray.entities.Segment)

Example 4 with SegmentListener

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;
    }
}
Also used : Entity(com.amazonaws.xray.entities.Entity) SegmentListener(com.amazonaws.xray.listeners.SegmentListener) SubsegmentImpl(com.amazonaws.xray.entities.SubsegmentImpl) TraceID(com.amazonaws.xray.entities.TraceID) Subsegment(com.amazonaws.xray.entities.Subsegment) FacadeSegment(com.amazonaws.xray.entities.FacadeSegment) Segment(com.amazonaws.xray.entities.Segment)

Aggregations

Entity (com.amazonaws.xray.entities.Entity)4 Subsegment (com.amazonaws.xray.entities.Subsegment)4 SegmentListener (com.amazonaws.xray.listeners.SegmentListener)4 FacadeSegment (com.amazonaws.xray.entities.FacadeSegment)2 Segment (com.amazonaws.xray.entities.Segment)2 SubsegmentImpl (com.amazonaws.xray.entities.SubsegmentImpl)2 TraceID (com.amazonaws.xray.entities.TraceID)1