Search in sources :

Example 1 with Span

use of com.amazon.dataprepper.model.trace.Span in project data-prepper by opensearch-project.

the class PeerForwarderTest method testSingleRemoteIpForwardRequestEncodeError.

@Test
public void testSingleRemoteIpForwardRequestEncodeError() throws NoSuchFieldException, IllegalAccessException, DecoderException, UnsupportedEncodingException {
    final List<String> testIps = generateTestIps(2);
    final String peerIp = testIps.get(1);
    when(peerClientPool.getClient(peerIp)).thenReturn(client);
    MetricsTestUtil.initMetrics();
    final PeerForwarder testPeerForwarder = generatePeerForwarder(testIps, 3);
    when(oTelProtoEncoder.convertToResourceSpans(any(Span.class))).thenThrow(new DecoderException());
    reflectivelySetEncoder(testPeerForwarder, oTelProtoEncoder);
    final List<Record<Object>> exportedRecords = testPeerForwarder.doExecute(TEST_SPANS_B.stream().map(span -> new Record<Object>(span)).collect(Collectors.toList()));
    verifyNoInteractions(client);
    Assert.assertEquals(3, exportedRecords.size());
    final List<Span> exportedSpans = exportedRecords.stream().map(record -> (Span) record.getData()).collect(Collectors.toList());
    assertTrue(exportedSpans.containsAll(TEST_SPANS_B));
    assertTrue(TEST_SPANS_B.containsAll(exportedSpans));
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Channel(io.grpc.Channel) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) After(org.junit.After) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Resource(io.opentelemetry.proto.resource.v1.Resource) Record(com.amazon.dataprepper.model.record.Record) DefaultTraceGroupFields(com.amazon.dataprepper.model.trace.DefaultTraceGroupFields) Span(com.amazon.dataprepper.model.trace.Span) DecoderException(org.apache.commons.codec.DecoderException) UUID(java.util.UUID) TraceServiceGrpc(io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) MockedStatic(org.mockito.MockedStatic) List(java.util.List) JacksonSpan(com.amazon.dataprepper.model.trace.JacksonSpan) ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) OTelProtoCodec(com.amazon.dataprepper.plugins.otel.codec.OTelProtoCodec) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Mock(org.mockito.Mock) Mockito.mockStatic(org.mockito.Mockito.mockStatic) RunWith(org.junit.runner.RunWith) InstrumentationLibrarySpans(io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) Hex(org.apache.commons.codec.binary.Hex) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) OTelProtoCodec.convertUnixNanosToISO8601(com.amazon.dataprepper.plugins.otel.codec.OTelProtoCodec.convertUnixNanosToISO8601) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) Measurement(io.micrometer.core.instrument.Measurement) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) InstrumentationLibrary(io.opentelemetry.proto.common.v1.InstrumentationLibrary) StringJoiner(java.util.StringJoiner) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) DecoderException(org.apache.commons.codec.DecoderException) Record(com.amazon.dataprepper.model.record.Record) ByteString(com.google.protobuf.ByteString) Span(com.amazon.dataprepper.model.trace.Span) JacksonSpan(com.amazon.dataprepper.model.trace.JacksonSpan) Test(org.junit.Test)

Example 2 with Span

use of com.amazon.dataprepper.model.trace.Span in project data-prepper by opensearch-project.

the class PeerForwarderTest method testSingleRemoteIpForwardRequestFutureError.

@Test
public void testSingleRemoteIpForwardRequestFutureError() throws ExecutionException, InterruptedException {
    try (final MockedStatic<CompletableFuture> completableFutureMockedStatic = mockStatic(CompletableFuture.class)) {
        completableFutureMockedStatic.when(() -> CompletableFuture.supplyAsync(ArgumentMatchers.<Supplier<ExportTraceServiceRequest>>any(), any(ExecutorService.class))).thenReturn(completableFuture);
        when(completableFuture.get()).thenThrow(new InterruptedException());
        final List<String> testIps = generateTestIps(2);
        final Channel channel = mock(Channel.class);
        final String peerIp = testIps.get(1);
        final String fullPeerIp = String.format("%s:21890", peerIp);
        when(channel.authority()).thenReturn(fullPeerIp);
        when(peerClientPool.getClient(peerIp)).thenReturn(client);
        when(client.getChannel()).thenReturn(channel);
        MetricsTestUtil.initMetrics();
        final PeerForwarder testPeerForwarder = generatePeerForwarder(testIps, 3);
        final List<Record<Object>> exportedRecords = testPeerForwarder.doExecute(TEST_SPANS_B.stream().map(span -> new Record<Object>(span)).collect(Collectors.toList()));
        verify(completableFuture, times(1)).get();
        Assert.assertEquals(3, exportedRecords.size());
        final List<Span> exportedSpans = exportedRecords.stream().map(record -> (Span) record.getData()).collect(Collectors.toList());
        assertTrue(exportedSpans.containsAll(TEST_SPANS_B));
        assertTrue(TEST_SPANS_B.containsAll(exportedSpans));
    }
}
Also used : ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Channel(io.grpc.Channel) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) After(org.junit.After) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Resource(io.opentelemetry.proto.resource.v1.Resource) Record(com.amazon.dataprepper.model.record.Record) DefaultTraceGroupFields(com.amazon.dataprepper.model.trace.DefaultTraceGroupFields) Span(com.amazon.dataprepper.model.trace.Span) DecoderException(org.apache.commons.codec.DecoderException) UUID(java.util.UUID) TraceServiceGrpc(io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) MockedStatic(org.mockito.MockedStatic) List(java.util.List) JacksonSpan(com.amazon.dataprepper.model.trace.JacksonSpan) ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) OTelProtoCodec(com.amazon.dataprepper.plugins.otel.codec.OTelProtoCodec) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Mock(org.mockito.Mock) Mockito.mockStatic(org.mockito.Mockito.mockStatic) RunWith(org.junit.runner.RunWith) InstrumentationLibrarySpans(io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) Hex(org.apache.commons.codec.binary.Hex) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) OTelProtoCodec.convertUnixNanosToISO8601(com.amazon.dataprepper.plugins.otel.codec.OTelProtoCodec.convertUnixNanosToISO8601) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) Measurement(io.micrometer.core.instrument.Measurement) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) InstrumentationLibrary(io.opentelemetry.proto.common.v1.InstrumentationLibrary) StringJoiner(java.util.StringJoiner) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Channel(io.grpc.Channel) ByteString(com.google.protobuf.ByteString) Span(com.amazon.dataprepper.model.trace.Span) JacksonSpan(com.amazon.dataprepper.model.trace.JacksonSpan) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutorService(java.util.concurrent.ExecutorService) Record(com.amazon.dataprepper.model.record.Record) Test(org.junit.Test)

Example 3 with Span

use of com.amazon.dataprepper.model.trace.Span in project data-prepper by opensearch-project.

the class PeerForwarderTest method testSingleRemoteIpBothLocalAndForwardedRequestWithEventRecordData.

@Test
public void testSingleRemoteIpBothLocalAndForwardedRequestWithEventRecordData() throws DecoderException {
    final List<String> testIps = generateTestIps(2);
    final Channel channel = mock(Channel.class);
    final String peerIp = testIps.get(1);
    when(channel.authority()).thenReturn(String.format("%s:21890", peerIp));
    when(peerClientPool.getClient(peerIp)).thenReturn(client);
    when(client.getChannel()).thenReturn(channel);
    final Map<String, List<ExportTraceServiceRequest>> requestsByIp = testIps.stream().collect(Collectors.toMap(ip -> ip, ip -> new ArrayList<>()));
    doAnswer(invocation -> {
        final ExportTraceServiceRequest exportTraceServiceRequest = invocation.getArgument(0);
        requestsByIp.get(peerIp).add(exportTraceServiceRequest);
        return null;
    }).when(client).export(any(ExportTraceServiceRequest.class));
    MetricsTestUtil.initMetrics();
    final PeerForwarder testPeerForwarder = generatePeerForwarder(testIps, 3);
    final List<Record<Object>> exportedRecords = testPeerForwarder.doExecute(TEST_SPANS_ALL.stream().map(span -> new Record<Object>(span)).collect(Collectors.toList()));
    final List<Span> expectedLocalSpans = Arrays.asList(SPAN_1, SPAN_2, SPAN_3);
    Assert.assertEquals(3, exportedRecords.size());
    final List<Span> localSpans = exportedRecords.stream().map(record -> (Span) record.getData()).collect(Collectors.toList());
    assertTrue(localSpans.containsAll(expectedLocalSpans));
    assertTrue(expectedLocalSpans.containsAll(localSpans));
    Assert.assertEquals(1, requestsByIp.get(peerIp).size());
    final ExportTraceServiceRequest forwardedRequest = requestsByIp.get(peerIp).get(0);
    final List<ResourceSpans> forwardedResourceSpans = forwardedRequest.getResourceSpansList();
    assertEquals(3, forwardedResourceSpans.size());
    forwardedResourceSpans.forEach(rs -> {
        assertEquals(TEST_SERVICE_B, extractServiceName(rs));
        assertEquals(1, rs.getInstrumentationLibrarySpansCount());
        final InstrumentationLibrarySpans ils = rs.getInstrumentationLibrarySpans(0);
        assertEquals(1, ils.getSpansCount());
        final io.opentelemetry.proto.trace.v1.Span sp = ils.getSpans(0);
        assertEquals(TEST_TRACE_ID_2, Hex.encodeHexString(sp.getTraceId().toByteArray()));
    });
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Channel(io.grpc.Channel) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) After(org.junit.After) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Resource(io.opentelemetry.proto.resource.v1.Resource) Record(com.amazon.dataprepper.model.record.Record) DefaultTraceGroupFields(com.amazon.dataprepper.model.trace.DefaultTraceGroupFields) Span(com.amazon.dataprepper.model.trace.Span) DecoderException(org.apache.commons.codec.DecoderException) UUID(java.util.UUID) TraceServiceGrpc(io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) MockedStatic(org.mockito.MockedStatic) List(java.util.List) JacksonSpan(com.amazon.dataprepper.model.trace.JacksonSpan) ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) OTelProtoCodec(com.amazon.dataprepper.plugins.otel.codec.OTelProtoCodec) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Mock(org.mockito.Mock) Mockito.mockStatic(org.mockito.Mockito.mockStatic) RunWith(org.junit.runner.RunWith) InstrumentationLibrarySpans(io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) Hex(org.apache.commons.codec.binary.Hex) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) OTelProtoCodec.convertUnixNanosToISO8601(com.amazon.dataprepper.plugins.otel.codec.OTelProtoCodec.convertUnixNanosToISO8601) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) Measurement(io.micrometer.core.instrument.Measurement) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) InstrumentationLibrary(io.opentelemetry.proto.common.v1.InstrumentationLibrary) StringJoiner(java.util.StringJoiner) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) Channel(io.grpc.Channel) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) Span(com.amazon.dataprepper.model.trace.Span) JacksonSpan(com.amazon.dataprepper.model.trace.JacksonSpan) InstrumentationLibrarySpans(io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans) List(java.util.List) ArrayList(java.util.ArrayList) Record(com.amazon.dataprepper.model.record.Record) Test(org.junit.Test)

Example 4 with Span

use of com.amazon.dataprepper.model.trace.Span in project data-prepper by opensearch-project.

the class ServiceMapStatefulPrepperTest method testPrepareForShutdownWithEventRecordData.

@Test
public void testPrepareForShutdownWithEventRecordData() {
    final File path = new File(ServiceMapPrepperConfig.DEFAULT_DB_PATH);
    final ServiceMapStatefulPrepper serviceMapStateful = new ServiceMapStatefulPrepper(100, path, Clock.systemUTC(), 1, PLUGIN_SETTING);
    final byte[] rootSpanId1Bytes = ServiceMapTestUtils.getRandomBytes(8);
    final byte[] traceId1Bytes = ServiceMapTestUtils.getRandomBytes(16);
    final String rootSpanId1 = Hex.encodeHexString(rootSpanId1Bytes);
    final String traceId1 = Hex.encodeHexString(traceId1Bytes);
    final String traceGroup1 = "reset_password";
    final Span frontendSpans1 = ServiceMapTestUtils.getSpan(FRONTEND_SERVICE, traceGroup1, rootSpanId1, "", traceId1, io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_CLIENT);
    final Span authenticationSpansServer = ServiceMapTestUtils.getSpan(AUTHENTICATION_SERVICE, "reset", Hex.encodeHexString(ServiceMapTestUtils.getRandomBytes(8)), frontendSpans1.getSpanId(), traceId1, io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_SERVER);
    serviceMapStateful.execute(Arrays.asList(new Record<>(frontendSpans1), new Record<>(authenticationSpansServer)));
    assertFalse(serviceMapStateful.isReadyForShutdown());
    serviceMapStateful.prepareForShutdown();
    serviceMapStateful.execute(Collections.emptyList());
    assertTrue(serviceMapStateful.isReadyForShutdown());
    serviceMapStateful.shutdown();
}
Also used : Record(com.amazon.dataprepper.model.record.Record) File(java.io.File) Span(com.amazon.dataprepper.model.trace.Span) Test(org.junit.Test)

Example 5 with Span

use of com.amazon.dataprepper.model.trace.Span 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)

Aggregations

Span (com.amazon.dataprepper.model.trace.Span)15 Record (com.amazon.dataprepper.model.record.Record)10 JacksonSpan (com.amazon.dataprepper.model.trace.JacksonSpan)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Map (java.util.Map)8 Test (org.junit.Test)8 DefaultTraceGroupFields (com.amazon.dataprepper.model.trace.DefaultTraceGroupFields)7 ExportTraceServiceRequest (io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest)7 ResourceSpans (io.opentelemetry.proto.trace.v1.ResourceSpans)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 ExecutorService (java.util.concurrent.ExecutorService)7 Collectors (java.util.stream.Collectors)7 DecoderException (org.apache.commons.codec.DecoderException)7 PluginSetting (com.amazon.dataprepper.model.configuration.PluginSetting)6 OTelProtoCodec (com.amazon.dataprepper.plugins.otel.codec.OTelProtoCodec)6 ByteString (com.google.protobuf.ByteString)6 Measurement (io.micrometer.core.instrument.Measurement)6 TraceServiceGrpc (io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc)6 InstrumentationLibrary (io.opentelemetry.proto.common.v1.InstrumentationLibrary)6