Search in sources :

Example 1 with RawSpan

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

the class OTelTraceRawPrepper method convertRawSpansToJsonRecords.

private List<Record<String>> convertRawSpansToJsonRecords(final List<RawSpan> rawSpans) {
    final List<Record<String>> records = new LinkedList<>();
    for (RawSpan rawSpan : rawSpans) {
        String rawSpanJson;
        try {
            rawSpanJson = rawSpan.toJson();
        } catch (JsonProcessingException e) {
            LOG.error("Unable to process invalid Span {}:", rawSpan, e);
            spanErrorsCounter.increment();
            totalProcessingErrorsCounter.increment();
            continue;
        }
        records.add(new Record<>(rawSpanJson));
    }
    return records;
}
Also used : Record(com.amazon.dataprepper.model.record.Record) RawSpan(com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpan) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) LinkedList(java.util.LinkedList)

Example 2 with RawSpan

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

the class OTelTraceRawPrepper method doExecute.

/**
 * execute the prepper logic which could potentially modify the incoming record. The level to which the record has
 * been modified depends on the implementation
 *
 * @param records Input records that will be modified/processed
 * @return Record  modified output records
 */
@Override
public Collection<Record<String>> doExecute(Collection<Record<ExportTraceServiceRequest>> records) {
    final List<RawSpan> rawSpans = new LinkedList<>();
    for (Record<ExportTraceServiceRequest> ets : records) {
        for (ResourceSpans rs : ets.getData().getResourceSpansList()) {
            try {
                final String serviceName = OTelProtoHelper.getServiceName(rs.getResource()).orElse(null);
                final Map<String, Object> resourceAttributes = OTelProtoHelper.getResourceAttributes(rs.getResource());
                for (InstrumentationLibrarySpans is : rs.getInstrumentationLibrarySpansList()) {
                    for (Span sp : is.getSpansList()) {
                        final RawSpan rawSpan = new RawSpanBuilder().setFromSpan(sp, is.getInstrumentationLibrary(), serviceName, resourceAttributes).build();
                        processRawSpan(rawSpan, rawSpans);
                    }
                }
            } catch (Exception ex) {
                LOG.error("Unable to process invalid ResourceSpan {} :", rs, ex);
                resourceSpanErrorsCounter.increment();
                totalProcessingErrorsCounter.increment();
            }
        }
    }
    rawSpans.addAll(getTracesToFlushByGarbageCollection());
    return convertRawSpansToJsonRecords(rawSpans);
}
Also used : ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) RawSpan(com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpan) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) RawSpan(com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpan) Span(io.opentelemetry.proto.trace.v1.Span) LinkedList(java.util.LinkedList) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InstrumentationLibrarySpans(io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans) RawSpanBuilder(com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpanBuilder)

Example 3 with RawSpan

use of com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpan 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;
}
Also used : RawSpan(com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpan) LinkedList(java.util.LinkedList) TraceGroup(com.amazon.dataprepper.plugins.prepper.oteltrace.model.TraceGroup) RawSpanSet(com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpanSet)

Example 4 with RawSpan

use of com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpan 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;
}
Also used : RawSpan(com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpan) LinkedList(java.util.LinkedList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RawSpanSet(com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpanSet) TraceGroup(com.amazon.dataprepper.plugins.prepper.oteltrace.model.TraceGroup)

Aggregations

RawSpan (com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpan)4 LinkedList (java.util.LinkedList)4 RawSpanSet (com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpanSet)2 TraceGroup (com.amazon.dataprepper.plugins.prepper.oteltrace.model.TraceGroup)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Record (com.amazon.dataprepper.model.record.Record)1 RawSpanBuilder (com.amazon.dataprepper.plugins.prepper.oteltrace.model.RawSpanBuilder)1 ExportTraceServiceRequest (io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest)1 InstrumentationLibrarySpans (io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans)1 ResourceSpans (io.opentelemetry.proto.trace.v1.ResourceSpans)1 Span (io.opentelemetry.proto.trace.v1.Span)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1