use of com.amazon.dataprepper.plugins.prepper.oteltrace.model.TraceGroup in project data-prepper by opensearch-project.
the class OTelTraceRawPrepper method processRootSpan.
/**
* Retrieves all child spans from memory and returns them as a set with the root span.
* Also adds an entry to the traceID cache so that later child spans can be tagged,
* in the case where a child span is processed AFTER the root span.
*
* @param parentSpan
* @return List containing root span, along with any child spans that have already been processed.
*/
private List<RawSpan> processRootSpan(final RawSpan parentSpan) {
traceIdTraceGroupCache.put(parentSpan.getTraceId(), parentSpan.getTraceGroup());
final List<RawSpan> recordsToFlush = new LinkedList<>();
recordsToFlush.add(parentSpan);
final TraceGroup traceGroup = parentSpan.getTraceGroup();
final String parentSpanTraceId = parentSpan.getTraceId();
final RawSpanSet rawSpanSet = traceIdRawSpanSetMap.get(parentSpanTraceId);
if (rawSpanSet != null) {
for (final RawSpan rawSpan : rawSpanSet.getRawSpans()) {
rawSpan.setTraceGroup(traceGroup);
recordsToFlush.add(rawSpan);
}
traceIdRawSpanSetMap.remove(parentSpanTraceId);
}
return recordsToFlush;
}
use of com.amazon.dataprepper.plugins.prepper.oteltrace.model.TraceGroup in project data-prepper by opensearch-project.
the class OTelTraceRawPrepper method getTracesToFlushByGarbageCollection.
/**
* Periodically flush spans from memory. Typically all spans of a trace are written
* once the trace's root span arrives, however some child spans my arrive after the root span.
* This method ensures "orphaned" child spans are eventually flushed from memory.
* @return List of RawSpans to be sent down the pipeline
*/
private List<RawSpan> getTracesToFlushByGarbageCollection() {
final List<RawSpan> recordsToFlush = new LinkedList<>();
if (shouldGarbageCollect()) {
final boolean isLockAcquired = traceFlushLock.tryLock();
if (isLockAcquired) {
try {
final long now = System.currentTimeMillis();
lastTraceFlushTime = now;
final Iterator<Map.Entry<String, RawSpanSet>> entryIterator = traceIdRawSpanSetMap.entrySet().iterator();
while (entryIterator.hasNext()) {
final Map.Entry<String, RawSpanSet> entry = entryIterator.next();
final String traceId = entry.getKey();
final TraceGroup traceGroup = traceIdTraceGroupCache.getIfPresent(traceId);
final RawSpanSet rawSpanSet = entry.getValue();
final long traceTime = rawSpanSet.getTimeSeen();
if (now - traceTime >= traceFlushInterval || isShuttingDown) {
final Set<RawSpan> rawSpans = rawSpanSet.getRawSpans();
if (traceGroup != null) {
rawSpans.forEach(rawSpan -> {
rawSpan.setTraceGroup(traceGroup);
recordsToFlush.add(rawSpan);
});
} else {
rawSpans.forEach(rawSpan -> {
recordsToFlush.add(rawSpan);
LOG.warn("Missing trace group for SpanId: {}", rawSpan.getSpanId());
});
}
entryIterator.remove();
}
}
if (recordsToFlush.size() > 0) {
LOG.info("Flushing {} records due to GC", recordsToFlush.size());
}
} finally {
traceFlushLock.unlock();
}
}
}
return recordsToFlush;
}
Aggregations