Search in sources :

Example 1 with FacadeSegment

use of com.amazonaws.xray.entities.FacadeSegment 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()) {
            logger.debug("Ending subsegment named: " + current.getName());
        }
        Subsegment currentSubsegment = (Subsegment) current;
        currentSubsegment.end();
        if (recorder.getStreamingStrategy().requiresStreaming(currentSubsegment.getParentSegment())) {
            recorder.getStreamingStrategy().streamSome(currentSubsegment.getParentSegment(), recorder.getEmitter());
        }
        Entity parentEntity = current.getParent();
        if (parentEntity instanceof FacadeSegment) {
            if (((FacadeSegment) parentEntity).isSampled()) {
                current.getCreator().getEmitter().sendSubsegment((Subsegment) current);
            }
            clearTraceEntity();
        } else {
            setTraceEntity(current.getParent());
        }
    } else {
        throw new SubsegmentNotFoundException("Failed to end a subsegment: subsegment cannot be found.");
    }
}
Also used : Entity(com.amazonaws.xray.entities.Entity) SubsegmentNotFoundException(com.amazonaws.xray.exceptions.SubsegmentNotFoundException) Subsegment(com.amazonaws.xray.entities.Subsegment) FacadeSegment(com.amazonaws.xray.entities.FacadeSegment)

Example 2 with FacadeSegment

use of com.amazonaws.xray.entities.FacadeSegment 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);
    }
    if (null == getTraceEntity()) {
        // First subsgment of a subsegment branch.
        Segment parentSegment = null;
        if (LambdaSegmentContext.isInitializing(LambdaSegmentContext.getTraceHeaderFromEnvironment())) {
            logger.warn(LAMBDA_TRACE_HEADER_KEY + " is missing a trace ID, parent ID, or sampling decision. Subsegment " + name + " discarded.");
            parentSegment = new FacadeSegment(recorder, new TraceID(), "", SampleDecision.NOT_SAMPLED);
        } else {
            parentSegment = LambdaSegmentContext.newFacadeSegment(recorder);
        }
        Subsegment subsegment = new SubsegmentImpl(recorder, name, parentSegment);
        subsegment.setParent(parentSegment);
        setTraceEntity(subsegment);
        return subsegment;
    } else {
        // Continuation of a subsegment branch.
        Subsegment parentSubsegment = (Subsegment) getTraceEntity();
        // Ensure customers have not leaked subsegments across invocations
        TraceID environmentRootTraceId = LambdaSegmentContext.getTraceHeaderFromEnvironment().getRootTraceId();
        if (null != environmentRootTraceId && !environmentRootTraceId.equals(parentSubsegment.getParentSegment().getTraceId())) {
            clearTraceEntity();
            return beginSubsegment(recorder, name);
        }
        Subsegment subsegment = new SubsegmentImpl(recorder, name, parentSubsegment.getParentSegment());
        subsegment.setParent(parentSubsegment);
        parentSubsegment.addSubsegment(subsegment);
        setTraceEntity(subsegment);
        return subsegment;
    }
}
Also used : TraceID(com.amazonaws.xray.entities.TraceID) SubsegmentImpl(com.amazonaws.xray.entities.SubsegmentImpl) Subsegment(com.amazonaws.xray.entities.Subsegment) FacadeSegment(com.amazonaws.xray.entities.FacadeSegment) Segment(com.amazonaws.xray.entities.Segment) FacadeSegment(com.amazonaws.xray.entities.FacadeSegment)

Aggregations

FacadeSegment (com.amazonaws.xray.entities.FacadeSegment)2 Subsegment (com.amazonaws.xray.entities.Subsegment)2 Entity (com.amazonaws.xray.entities.Entity)1 Segment (com.amazonaws.xray.entities.Segment)1 SubsegmentImpl (com.amazonaws.xray.entities.SubsegmentImpl)1 TraceID (com.amazonaws.xray.entities.TraceID)1 SubsegmentNotFoundException (com.amazonaws.xray.exceptions.SubsegmentNotFoundException)1