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;
}
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);
}
}
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();
}
}
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;
}
}
Aggregations