Search in sources :

Example 1 with SpanSet

use of com.amazon.dataprepper.plugins.processor.oteltrace.model.SpanSet in project data-prepper by opensearch-project.

the class OTelTraceRawProcessor method processChildSpan.

/**
 * Attempts to populate the traceGroup of the child span by fetching from a cache. If the traceGroup is not in the cache,
 * the child span is kept in memory to be populated when its corresponding root span arrives.
 *
 * @param childSpan
 * @return Optional containing childSpan if its traceGroup is in memory, otherwise an empty Optional
 */
private Optional<Span> processChildSpan(final Span childSpan) {
    final String childSpanTraceId = childSpan.getTraceId();
    final TraceGroup traceGroup = traceIdTraceGroupCache.getIfPresent(childSpanTraceId);
    if (traceGroup != null) {
        fillInTraceGroupInfo(childSpan, traceGroup);
        return Optional.of(childSpan);
    } else {
        traceIdSpanSetMap.compute(childSpanTraceId, (traceId, spanSet) -> {
            if (spanSet == null) {
                spanSet = new SpanSet();
            }
            spanSet.addSpan(childSpan);
            return spanSet;
        });
        return Optional.empty();
    }
}
Also used : SpanSet(com.amazon.dataprepper.plugins.processor.oteltrace.model.SpanSet) TraceGroup(com.amazon.dataprepper.plugins.processor.oteltrace.model.TraceGroup)

Example 2 with SpanSet

use of com.amazon.dataprepper.plugins.processor.oteltrace.model.SpanSet in project data-prepper by opensearch-project.

the class OTelTraceRawProcessor 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<Span> getTracesToFlushByGarbageCollection() {
    final List<Span> 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, SpanSet>> entryIterator = traceIdSpanSetMap.entrySet().iterator();
                while (entryIterator.hasNext()) {
                    final Map.Entry<String, SpanSet> entry = entryIterator.next();
                    final String traceId = entry.getKey();
                    final TraceGroup traceGroup = traceIdTraceGroupCache.getIfPresent(traceId);
                    final SpanSet spanSet = entry.getValue();
                    final long traceTime = spanSet.getTimeSeen();
                    if (now - traceTime >= traceFlushInterval || isShuttingDown) {
                        final Set<Span> spans = spanSet.getSpans();
                        if (traceGroup != null) {
                            spans.forEach(span -> {
                                fillInTraceGroupInfo(span, traceGroup);
                                recordsToFlush.add(span);
                            });
                        } else {
                            spans.forEach(span -> {
                                recordsToFlush.add(span);
                                LOG.warn("Missing trace group for SpanId: {}", span.getSpanId());
                            });
                        }
                        entryIterator.remove();
                    }
                }
                if (recordsToFlush.size() > 0) {
                    LOG.info("Flushing {} records due to GC", recordsToFlush.size());
                }
            } finally {
                traceFlushLock.unlock();
            }
        }
    }
    return recordsToFlush;
}
Also used : Span(com.amazon.dataprepper.model.trace.Span) LinkedList(java.util.LinkedList) SpanSet(com.amazon.dataprepper.plugins.processor.oteltrace.model.SpanSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) TraceGroup(com.amazon.dataprepper.plugins.processor.oteltrace.model.TraceGroup)

Example 3 with SpanSet

use of com.amazon.dataprepper.plugins.processor.oteltrace.model.SpanSet in project data-prepper by opensearch-project.

the class OTelTraceRawProcessor 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<Span> processRootSpan(final Span parentSpan) {
    final TraceGroup traceGroup = new TraceGroup.TraceGroupBuilder().setFromSpan(parentSpan).build();
    final String parentSpanTraceId = parentSpan.getTraceId();
    traceIdTraceGroupCache.put(parentSpanTraceId, traceGroup);
    final List<Span> recordsToFlush = new LinkedList<>();
    recordsToFlush.add(parentSpan);
    final SpanSet spanSet = traceIdSpanSetMap.get(parentSpanTraceId);
    if (spanSet != null) {
        for (final Span span : spanSet.getSpans()) {
            fillInTraceGroupInfo(span, traceGroup);
            recordsToFlush.add(span);
        }
        traceIdSpanSetMap.remove(parentSpanTraceId);
    }
    return recordsToFlush;
}
Also used : SpanSet(com.amazon.dataprepper.plugins.processor.oteltrace.model.SpanSet) Span(com.amazon.dataprepper.model.trace.Span) TraceGroup(com.amazon.dataprepper.plugins.processor.oteltrace.model.TraceGroup) LinkedList(java.util.LinkedList)

Aggregations

SpanSet (com.amazon.dataprepper.plugins.processor.oteltrace.model.SpanSet)3 TraceGroup (com.amazon.dataprepper.plugins.processor.oteltrace.model.TraceGroup)3 Span (com.amazon.dataprepper.model.trace.Span)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1