Search in sources :

Example 1 with TraceGroup

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

the class EndToEndRawSpanTest method getExpectedEsDocumentSource.

private Map<String, Object> getExpectedEsDocumentSource(final Span span, final String serviceName) {
    final Map<String, Object> esDocSource = new HashMap<>();
    final String traceId = Hex.toHexString(span.getTraceId().toByteArray());
    esDocSource.put("traceId", traceId);
    esDocSource.put("spanId", Hex.toHexString(span.getSpanId().toByteArray()));
    esDocSource.put("parentSpanId", Hex.toHexString(span.getParentSpanId().toByteArray()));
    esDocSource.put("name", span.getName());
    esDocSource.put("kind", span.getKind().name());
    esDocSource.put("status.code", span.getStatus().getCodeValue());
    esDocSource.put("serviceName", serviceName);
    final TraceGroup traceGroup = TEST_TRACEID_TO_TRACE_GROUP.get(traceId);
    esDocSource.put(TraceGroup.TRACE_GROUP_NAME_FIELD, traceGroup.getTraceGroup());
    esDocSource.put(TraceGroup.TRACE_GROUP_END_TIME_FIELD, traceGroup.getTraceGroupFields().getEndTime());
    esDocSource.put(TraceGroup.TRACE_GROUP_DURATION_IN_NANOS_FIELD, traceGroup.getTraceGroupFields().getDurationInNanos());
    esDocSource.put(TraceGroup.TRACE_GROUP_STATUS_CODE_FIELD, traceGroup.getTraceGroupFields().getStatusCode());
    return esDocSource;
}
Also used : HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) TraceGroup(com.amazon.dataprepper.plugins.processor.oteltracegroup.model.TraceGroup)

Example 2 with TraceGroup

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

the class OTelTraceGroupProcessor method doExecute.

@Override
public Collection<Record<Span>> doExecute(final Collection<Record<Span>> rawSpanRecords) {
    final List<Record<Span>> recordsOut = new LinkedList<>();
    final Set<Record<Span>> recordsMissingTraceGroupInfo = new HashSet<>();
    final Set<String> traceIdsToLookUp = new HashSet<>();
    for (Record<Span> record : rawSpanRecords) {
        final Span span = record.getData();
        final String traceGroup = span.getTraceGroup();
        final String traceId = span.getTraceId();
        if (Strings.isNullOrEmpty(traceGroup)) {
            traceIdsToLookUp.add(traceId);
            recordsMissingTraceGroupInfo.add(record);
            recordsInMissingTraceGroupCounter.increment();
        } else {
            recordsOut.add(record);
        }
    }
    final Map<String, TraceGroup> traceIdToTraceGroup = searchTraceGroupByTraceIds(traceIdsToLookUp);
    for (final Record<Span> record : recordsMissingTraceGroupInfo) {
        final Span span = record.getData();
        final String traceId = span.getTraceId();
        final TraceGroup traceGroup = traceIdToTraceGroup.get(traceId);
        if (traceGroup != null) {
            try {
                fillInTraceGroupInfo(span, traceGroup);
                recordsOut.add(record);
                recordsOutFixedTraceGroupCounter.increment();
            } catch (Exception e) {
                recordsOut.add(record);
                recordsOutMissingTraceGroupCounter.increment();
                LOG.error("Failed to process the span: [{}]", record.getData(), e);
            }
        } else {
            recordsOut.add(record);
            recordsOutMissingTraceGroupCounter.increment();
            final String spanId = span.getSpanId();
            LOG.warn("Failed to find traceGroup for spanId: {} due to traceGroup missing for traceId: {}", spanId, traceId);
        }
    }
    return recordsOut;
}
Also used : Record(com.amazon.dataprepper.model.record.Record) Span(com.amazon.dataprepper.model.trace.Span) LinkedList(java.util.LinkedList) TraceGroup(com.amazon.dataprepper.plugins.processor.oteltracegroup.model.TraceGroup) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 3 with TraceGroup

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

the class OTelTraceGroupProcessor method fromSearchHitToMapEntry.

private Optional<Map.Entry<String, TraceGroup>> fromSearchHitToMapEntry(final SearchHit searchHit) {
    final DocumentField traceIdDocField = searchHit.field(OTelTraceGroupProcessorConfig.TRACE_ID_FIELD);
    final DocumentField traceGroupNameDocField = searchHit.field(TraceGroup.TRACE_GROUP_NAME_FIELD);
    final DocumentField traceGroupEndTimeDocField = searchHit.field(TraceGroup.TRACE_GROUP_END_TIME_FIELD);
    final DocumentField traceGroupDurationInNanosDocField = searchHit.field(TraceGroup.TRACE_GROUP_DURATION_IN_NANOS_FIELD);
    final DocumentField traceGroupStatusCodeDocField = searchHit.field(TraceGroup.TRACE_GROUP_STATUS_CODE_FIELD);
    if (Stream.of(traceIdDocField, traceGroupNameDocField, traceGroupEndTimeDocField, traceGroupDurationInNanosDocField, traceGroupStatusCodeDocField).allMatch(Objects::nonNull)) {
        final String traceId = traceIdDocField.getValue();
        final String traceGroupName = traceGroupNameDocField.getValue();
        final String traceGroupEndTime = normalizeDateTime(traceGroupEndTimeDocField.getValue());
        final Number traceGroupDurationInNanos = traceGroupDurationInNanosDocField.getValue();
        final Number traceGroupStatusCode = traceGroupStatusCodeDocField.getValue();
        final TraceGroupFields traceGroupFields = DefaultTraceGroupFields.builder().withEndTime(traceGroupEndTime).withDurationInNanos(traceGroupDurationInNanos.longValue()).withStatusCode(traceGroupStatusCode.intValue()).build();
        final TraceGroup traceGroup = new TraceGroup.TraceGroupBuilder().setTraceGroup(traceGroupName).setTraceGroupFields(traceGroupFields).build();
        return Optional.of(new AbstractMap.SimpleEntry<>(traceId, traceGroup));
    }
    return Optional.empty();
}
Also used : AbstractMap(java.util.AbstractMap) DocumentField(org.opensearch.common.document.DocumentField) Objects(java.util.Objects) DefaultTraceGroupFields(com.amazon.dataprepper.model.trace.DefaultTraceGroupFields) TraceGroupFields(com.amazon.dataprepper.model.trace.TraceGroupFields) TraceGroup(com.amazon.dataprepper.plugins.processor.oteltracegroup.model.TraceGroup)

Example 4 with TraceGroup

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

the class OTelTraceGroupProcessor method searchTraceGroupByTraceIds.

private Map<String, TraceGroup> searchTraceGroupByTraceIds(final Collection<String> traceIds) {
    final Map<String, TraceGroup> traceIdToTraceGroup = new HashMap<>();
    final SearchRequest searchRequest = createSearchRequest(traceIds);
    try {
        final SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        final SearchHit[] searchHits = searchResponse.getHits().getHits();
        Arrays.asList(searchHits).forEach(searchHit -> {
            final Optional<Map.Entry<String, TraceGroup>> optionalStringTraceGroupEntry = fromSearchHitToMapEntry(searchHit);
            optionalStringTraceGroupEntry.ifPresent(entry -> traceIdToTraceGroup.put(entry.getKey(), entry.getValue()));
        });
    } catch (Exception e) {
        // TODO: retry for status code 429 of OpenSearchException?
        LOG.error("Search request for traceGroup failed for traceIds: {} due to {}", traceIds, e.getMessage());
    }
    return traceIdToTraceGroup;
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) HashMap(java.util.HashMap) TraceGroup(com.amazon.dataprepper.plugins.processor.oteltracegroup.model.TraceGroup) IOException(java.io.IOException) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

TraceGroup (com.amazon.dataprepper.plugins.processor.oteltracegroup.model.TraceGroup)4 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Record (com.amazon.dataprepper.model.record.Record)1 DefaultTraceGroupFields (com.amazon.dataprepper.model.trace.DefaultTraceGroupFields)1 Span (com.amazon.dataprepper.model.trace.Span)1 TraceGroupFields (com.amazon.dataprepper.model.trace.TraceGroupFields)1 ByteString (com.google.protobuf.ByteString)1 AbstractMap (java.util.AbstractMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Objects (java.util.Objects)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 DocumentField (org.opensearch.common.document.DocumentField)1 SearchHit (org.opensearch.search.SearchHit)1