Search in sources :

Example 6 with SegmentContext

use of com.amazonaws.xray.contexts.SegmentContext in project aws-xray-sdk-java by aws.

the class AWSXRayRecorder method getCurrentSubsegment.

/**
 * @throws SegmentNotFoundException
 *             if {@code contextMissingStrategy} throws exceptions and the segment context cannot be found
 * @throws SubsegmentNotFoundException
 *             if {@code contextMissingStrategy} throws exceptions and the current segment has no subsegments in progress
 * @return the current subsegment, or {@code null} if {@code contextMissingStrategy} suppresses exceptions and the segment context cannot be found or the segment has no subsegments in progress
 */
public Subsegment getCurrentSubsegment() {
    SegmentContext context = getSegmentContext();
    if (null == context) {
        return null;
    }
    Entity current = context.getTraceEntity();
    if (null == current) {
        contextMissingStrategy.contextMissing("No segment in progress.", SegmentNotFoundException.class);
    } else if (current instanceof Subsegment) {
        return (Subsegment) current;
    } else {
        contextMissingStrategy.contextMissing("No subsegment in progress.", SubsegmentNotFoundException.class);
    }
    return null;
}
Also used : SegmentContext(com.amazonaws.xray.contexts.SegmentContext) SubsegmentNotFoundException(com.amazonaws.xray.exceptions.SubsegmentNotFoundException)

Example 7 with SegmentContext

use of com.amazonaws.xray.contexts.SegmentContext in project aws-xray-sdk-java by aws.

the class AWSXRayRecorder method endSegment.

/**
 * Ends a segment.
 *
 * @throws SegmentNotFoundException
 *             if {@code contextMissingStrategy} throws exceptions and no segment is currently in progress
 */
public void endSegment() {
    SegmentContext context = getSegmentContext();
    if (null != context) {
        context.endSegment(this);
    }
    Entity current;
    if ((current = getTraceEntity()) != null) {
        Segment segment = current.getParentSegment();
        logger.debug("Ending segment named '" + segment.getName() + "'.");
        if (segment.end()) {
            sendSegment(segment);
        } else {
            logger.debug("Not emitting segment named '" + segment.getName() + "' as it parents in-progress subsegments.");
        }
        clearTraceEntity();
    } else {
        getContextMissingStrategy().contextMissing("Failed to end segment: segment cannot be found.", SegmentNotFoundException.class);
    }
}
Also used : SegmentContext(com.amazonaws.xray.contexts.SegmentContext)

Example 8 with SegmentContext

use of com.amazonaws.xray.contexts.SegmentContext in project aws-xray-sdk-java by aws.

the class AWSXRayRecorder method getCurrentSegmentOptional.

/**
 * @return the current segment, or {@code Optional.empty()} if there is no segment
 */
public Optional<Segment> getCurrentSegmentOptional() {
    // explicitly do not throw context missing exceptions from optional-returning methods
    SegmentContext context = segmentContextResolverChain.resolve();
    if (null == context) {
        return Optional.empty();
    }
    Entity current = context.getTraceEntity();
    if (current instanceof Segment) {
        return Optional.of((Segment) current);
    } else if (current instanceof Subsegment) {
        return Optional.of(current.getParentSegment());
    } else {
        return Optional.empty();
    }
}
Also used : SegmentContext(com.amazonaws.xray.contexts.SegmentContext)

Example 9 with SegmentContext

use of com.amazonaws.xray.contexts.SegmentContext in project aws-xray-sdk-java by aws.

the class AWSXRayRecorder method currentEntityId.

/**
 * @throws SegmentNotFoundException
 *             if {@code contextMissingStrategy} throws exceptions and no segment or subsegment is currently in progress
 * @return the ID of the {@code Segment} or {@code Subsegment} currently in progress, or {@code null} if {@code contextMissingStrategy} suppresses exceptions and no segment or subsegment is currently in progress
 */
public String currentEntityId() {
    SegmentContext context = getSegmentContext();
    if (null == context) {
        return null;
    }
    Entity current = context.getTraceEntity();
    if (null != current) {
        return current.getId();
    } else {
        contextMissingStrategy.contextMissing("Failed to get current entity ID: segment or subsegment cannot be found.", SegmentNotFoundException.class);
        return null;
    }
}
Also used : SegmentContext(com.amazonaws.xray.contexts.SegmentContext)

Aggregations

SegmentContext (com.amazonaws.xray.contexts.SegmentContext)9 SubsegmentNotFoundException (com.amazonaws.xray.exceptions.SubsegmentNotFoundException)1