/** * @return the current segment, or {@code Optional.empty()} if there is no segment */ public Optional<Segment> getCurrentSegmentOptional() { SegmentContext context = segmentContextResolverChain.resolve(); // explicitly do not throw context missing exceptions from optional-returning methods 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(); } }
/** * @return the current segment, or {@code Optional.empty()} if there is no segment */ public Optional<Segment> getCurrentSegmentOptional() { SegmentContext context = segmentContextResolverChain.resolve(); // explicitly do not throw context missing exceptions from optional-returning methods 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(); } }
/** * * @throws SegmentNotFoundException * if {@code contextMissingStrategy} throws exceptions and no segment or subsegment is currently in progress * @return the trace ID of the {@code Segment} currently in progress, or {@code null} if {@code contextMissingStrategy} suppresses exceptions and no segment or subsegment is currently in progress */ public TraceID currentTraceId() { SegmentContext context = getSegmentContext(); if (null == context) { return null; } Entity current = context.getTraceEntity(); if (null != current) { return current.getParentSegment().getTraceId(); } else { contextMissingStrategy.contextMissing("Failed to get current trace ID: segment cannot be found.", SegmentNotFoundException.class); return null; } }
/** * * @throws SegmentNotFoundException * if {@code contextMissingStrategy} throws exceptions and no segment or subsegment is currently in progress * @return the trace ID of the {@code Segment} currently in progress, or {@code null} if {@code contextMissingStrategy} suppresses exceptions and no segment or subsegment is currently in progress */ public TraceID currentTraceId() { SegmentContext context = getSegmentContext(); if (null == context) { return null; } Entity current = context.getTraceEntity(); if (null != current) { return current.getParentSegment().getTraceId(); } else { contextMissingStrategy.contextMissing("Failed to get current trace ID: segment cannot be found.", SegmentNotFoundException.class); return null; } }
@Override public Subsegment beginSubsegment(AWSXRayRecorder recorder, String name) { Entity current = getTraceEntity(); if (null == current) { recorder.getContextMissingStrategy().contextMissing("Failed to begin subsegment named '" + name + "': segment cannot be found.", SegmentNotFoundException.class); return null; } if (logger.isDebugEnabled()) { logger.debug("Beginning subsegment named: " + name); } Segment parentSegment = getTraceEntity().getParentSegment(); Subsegment subsegment = new SubsegmentImpl(recorder, name, parentSegment); subsegment.setParent(current); current.addSubsegment(subsegment); setTraceEntity(subsegment); return subsegment; }
@Override public Subsegment beginSubsegment(AWSXRayRecorder recorder, String name) { Entity current = getTraceEntity(); if (null == current) { recorder.getContextMissingStrategy().contextMissing("Failed to begin subsegment named '" + name + "': segment cannot be found.", SegmentNotFoundException.class); return null; } if (logger.isDebugEnabled()) { logger.debug("Beginning subsegment named: " + name); } Segment parentSegment = getTraceEntity().getParentSegment(); Subsegment subsegment = new SubsegmentImpl(recorder, name, parentSegment); subsegment.setParent(current); current.addSubsegment(subsegment); setTraceEntity(subsegment); return subsegment; }
/** * 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); } }
/** * 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); } }