@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; }
protected EntityImpl(AWSXRayRecorder creator, String name) { StringValidator.throwIfNullOrBlank(name, "(Sub)segment name cannot be null or blank."); validateNotNull(creator); this.creator = creator; this.name = name; this.subsegments = new ArrayList<>(); this.subsegmentsLock = new ReentrantLock(); this.cause = new Cause(); this.http = new ConcurrentHashMap<>(); this.aws = new ConcurrentHashMap<>(); this.sql = new ConcurrentHashMap<>(); this.annotations = new ConcurrentHashMap<>(); this.metadata = new ConcurrentHashMap<>(); this.startTime = Instant.now().toEpochMilli() / 1000.0d; this.id = Entity.generateId(); this.inProgress = true; this.referenceCount = new LongAdder(); this.totalSize = new LongAdder(); }
private ObjectNode getStreamSerializeObjectNode() { ObjectNode obj = (ObjectNode) mapper.valueToTree(this); obj.put("type", "subsegment"); obj.put("parent_id", getParent().getId()); obj.put("trace_id", parentSegment.getTraceId().toString()); return obj; }
/** * {@inheritDoc} * * Performs a depth first search to find completed subsegment subtrees. Serializes these subsegments, streams them to the daemon, and removes them from their parents. * * @see StreamingStrategy#streamSome(Entity,Emitter) */ public void streamSome(Entity entity, Emitter emitter) { if (entity.getSubsegmentsLock().tryLock()) { try { //copy before traversal to avoid ConcurrentModificationExceptions new ArrayList<Subsegment>(entity.getSubsegments()).forEach( (subsegment) -> { if (subsegment.isInProgress() || !subsegment.getSubsegments().isEmpty()) { streamSome(subsegment, emitter); } else { emitter.sendSubsegment(subsegment); subsegment.setEmitted(true); entity.removeSubsegment(subsegment); } }); } finally { entity.getSubsegmentsLock().unlock(); } } } }
@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."); } } }
@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; if (currentSubsegment.end()) { recorder.sendSegment(currentSubsegment.getParentSegment()); } else { if (recorder.getStreamingStrategy().requiresStreaming(currentSubsegment.getParentSegment())) { recorder.getStreamingStrategy().streamSome(currentSubsegment.getParentSegment(), recorder.getEmitter()); } setTraceEntity(current.getParent()); } } else { recorder.getContextMissingStrategy().contextMissing("Failed to end subsegment: subsegment cannot be found.", SubsegmentNotFoundException.class); } } }
/** * @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(); } }
private void processEvent(AsyncEvent event) throws IOException { AWSXRayRecorder recorder = getRecorder(); Entity prior = recorder.getTraceEntity(); try { Entity entity = (Entity) event.getSuppliedRequest().getAttribute(ENTITY_ATTRIBUTE_KEY); recorder.setTraceEntity(entity); if (null != event.getThrowable()) { entity.addException(event.getThrowable()); } filter.postFilter(event.getSuppliedRequest(), event.getSuppliedResponse()); } finally { recorder.setTraceEntity(prior); } }
private Segment beginSegment(Segment segment) { SegmentContext context = getSegmentContext(); if (null == context) { return null; } Entity current; if ((current = getTraceEntity()) != null) { logger.error("Beginning new segment while another segment exists in the segment context. Overwriting current segment named '" + current.getName() + "' to start new segment named '" + segment.getName() + "'."); } segment.setAws(getAwsRuntimeContext()); if (null != getOrigin()){ segment.setOrigin(getOrigin()); } segment.putAllService(getServiceRuntimeContext()); setTraceEntity(segment); return context.beginSegment(this, segment); }
@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."); } } }
/** * {@inheritDoc} * * Performs a depth first search to find completed subsegment subtrees. Serializes these subsegments, streams them to the daemon, and removes them from their parents. * * @see StreamingStrategy#streamSome(Entity,Emitter) */ public void streamSome(Entity entity, Emitter emitter) { if (entity.getSubsegmentsLock().tryLock()) { try { //copy before traversal to avoid ConcurrentModificationExceptions new ArrayList<Subsegment>(entity.getSubsegments()).forEach( (subsegment) -> { if (subsegment.isInProgress() || !subsegment.getSubsegments().isEmpty()) { streamSome(subsegment, emitter); } else { emitter.sendSubsegment(subsegment); subsegment.setEmitted(true); entity.removeSubsegment(subsegment); } }); } finally { entity.getSubsegmentsLock().unlock(); } } } }
@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; if (currentSubsegment.end()) { recorder.sendSegment(currentSubsegment.getParentSegment()); } else { if (recorder.getStreamingStrategy().requiresStreaming(currentSubsegment.getParentSegment())) { recorder.getStreamingStrategy().streamSome(currentSubsegment.getParentSegment(), recorder.getEmitter()); } setTraceEntity(current.getParent()); } } else { recorder.getContextMissingStrategy().contextMissing("Failed to end subsegment: subsegment cannot be found.", SubsegmentNotFoundException.class); } } }
/** * @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(); } }
private void processEvent(AsyncEvent event) throws IOException { AWSXRayRecorder recorder = getRecorder(); Entity prior = recorder.getTraceEntity(); try { Entity entity = (Entity) event.getSuppliedRequest().getAttribute(ENTITY_ATTRIBUTE_KEY); recorder.setTraceEntity(entity); if (null != event.getThrowable()) { entity.addException(event.getThrowable()); } filter.postFilter(event.getSuppliedRequest(), event.getSuppliedResponse()); } finally { recorder.setTraceEntity(prior); } }
private Segment beginSegment(Segment segment) { SegmentContext context = getSegmentContext(); if (null == context) { return null; } Entity current; if ((current = getTraceEntity()) != null) { logger.error("Beginning new segment while another segment exists in the segment context. Overwriting current segment named '" + current.getName() + "' to start new segment named '" + segment.getName() + "'."); } segment.setAws(getAwsRuntimeContext()); if (null != getOrigin()){ segment.setOrigin(getOrigin()); } segment.putAllService(getServiceRuntimeContext()); setTraceEntity(segment); return context.beginSegment(this, segment); }
@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; }
/** * * @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; } }
private ObjectNode getStreamSerializeObjectNode() { ObjectNode obj = (ObjectNode) mapper.valueToTree(this); obj.put("type", "subsegment"); obj.put("parent_id", getParent().getId()); obj.put("trace_id", parentSegment.getTraceId().toString()); return obj; }
protected EntityImpl(AWSXRayRecorder creator, String name) { StringValidator.throwIfNullOrBlank(name, "(Sub)segment name cannot be null or blank."); validateNotNull(creator); this.creator = creator; this.name = name; this.subsegments = new ArrayList<>(); this.subsegmentsLock = new ReentrantLock(); this.cause = new Cause(); this.http = new ConcurrentHashMap<>(); this.aws = new ConcurrentHashMap<>(); this.sql = new ConcurrentHashMap<>(); this.annotations = new ConcurrentHashMap<>(); this.metadata = new ConcurrentHashMap<>(); this.startTime = Instant.now().toEpochMilli() / 1000.0d; this.id = Entity.generateId(); this.inProgress = true; this.referenceCount = new LongAdder(); this.totalSize = new LongAdder(); }
/** * * @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; } }