Search in sources :

Example 1 with SpanContextualizer

use of com.splunk.opentelemetry.profiler.context.SpanContextualizer in project splunk-otel-java by signalfx.

the class TLABProcessorTest method tlabProcessTest.

private void tlabProcessTest(Long tlabSize) {
    Instant now = Instant.now();
    AtomicReference<LogData> seenLogData = new AtomicReference<>();
    LogProcessor consumer = seenLogData::set;
    String stackAsString = "\"mockingbird\" #606\n" + "   java.lang.Thread.State: UNKNOWN\n" + "i am a serialized stack believe me";
    StackSerializer serializer = mock(StackSerializer.class);
    LogDataCommonAttributes commonAttrs = new LogDataCommonAttributes(new EventPeriods(x -> null));
    Clock clock = new MockClock(now);
    RecordedEvent event = createMockEvent(serializer, now, tlabSize);
    Config config = mock(Config.class);
    when(config.getBoolean(CONFIG_KEY_TLAB_ENABLED, DEFAULT_MEMORY_ENABLED)).thenReturn(true);
    SpanContext spanContext = SpanContext.create(TraceId.fromLongs(123, 456), SpanId.fromLong(123), TraceFlags.getSampled(), TraceState.getDefault());
    SpanContextualizer spanContextualizer = mock(SpanContextualizer.class);
    when(spanContextualizer.link(THREAD_ID)).thenReturn(new SpanLinkage(spanContext, THREAD_ID));
    TLABProcessor processor = TLABProcessor.builder(config).stackSerializer(serializer).logProcessor(consumer).commonAttributes(commonAttrs).resource(Resource.getDefault()).spanContextualizer(spanContextualizer).build();
    processor.accept(event);
    assertEquals(stackAsString, seenLogData.get().getBody().asString());
    assertEquals(TimeUnit.SECONDS.toNanos(now.getEpochSecond()) + clock.nanoTime(), seenLogData.get().getEpochNanos());
    assertEquals("otel.profiling", seenLogData.get().getAttributes().get(SOURCE_TYPE));
    assertEquals("tee-lab", seenLogData.get().getAttributes().get(SOURCE_EVENT_NAME));
    assertEquals(ONE_MB, seenLogData.get().getAttributes().get(ALLOCATION_SIZE_KEY));
    assertEquals(spanContext, seenLogData.get().getSpanContext());
}
Also used : Clock(io.opentelemetry.sdk.common.Clock) TraceFlags(io.opentelemetry.api.trace.TraceFlags) LogProcessor(io.opentelemetry.sdk.logs.LogProcessor) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) SpanId(io.opentelemetry.api.trace.SpanId) Resource(io.opentelemetry.sdk.resources.Resource) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) SOURCE_EVENT_NAME(com.splunk.opentelemetry.profiler.ProfilingSemanticAttributes.SOURCE_EVENT_NAME) SpanLinkage(com.splunk.opentelemetry.profiler.context.SpanLinkage) EventPeriods(com.splunk.opentelemetry.profiler.events.EventPeriods) AtomicReference(java.util.concurrent.atomic.AtomicReference) Answer(org.mockito.stubbing.Answer) TraceState(io.opentelemetry.api.trace.TraceState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ALLOCATION_SIZE_KEY(com.splunk.opentelemetry.profiler.TLABProcessor.ALLOCATION_SIZE_KEY) CONFIG_KEY_MEMORY_SAMPLER_INTERVAL(com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_MEMORY_SAMPLER_INTERVAL) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EventType(jdk.jfr.EventType) LogData(io.opentelemetry.sdk.logs.data.LogData) CONFIG_KEY_TLAB_ENABLED(com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_TLAB_ENABLED) SpanContext(io.opentelemetry.api.trace.SpanContext) DEFAULT_MEMORY_ENABLED(com.splunk.opentelemetry.profiler.Configuration.DEFAULT_MEMORY_ENABLED) Mockito.when(org.mockito.Mockito.when) DEFAULT_MEMORY_SAMPLING_INTERVAL(com.splunk.opentelemetry.profiler.Configuration.DEFAULT_MEMORY_SAMPLING_INTERVAL) Instant(java.time.Instant) RecordedStackTrace(jdk.jfr.consumer.RecordedStackTrace) SpanContextualizer(com.splunk.opentelemetry.profiler.context.SpanContextualizer) Config(io.opentelemetry.instrumentation.api.config.Config) TimeUnit(java.util.concurrent.TimeUnit) RecordedEvent(jdk.jfr.consumer.RecordedEvent) Test(org.junit.jupiter.api.Test) SOURCE_TYPE(com.splunk.opentelemetry.profiler.ProfilingSemanticAttributes.SOURCE_TYPE) RecordedThread(jdk.jfr.consumer.RecordedThread) StackSerializer(com.splunk.opentelemetry.profiler.util.StackSerializer) AttributeKey(io.opentelemetry.api.common.AttributeKey) TraceId(io.opentelemetry.api.trace.TraceId) Mockito.mock(org.mockito.Mockito.mock) LogData(io.opentelemetry.sdk.logs.data.LogData) EventPeriods(com.splunk.opentelemetry.profiler.events.EventPeriods) SpanContext(io.opentelemetry.api.trace.SpanContext) StackSerializer(com.splunk.opentelemetry.profiler.util.StackSerializer) Config(io.opentelemetry.instrumentation.api.config.Config) SpanLinkage(com.splunk.opentelemetry.profiler.context.SpanLinkage) Instant(java.time.Instant) RecordedEvent(jdk.jfr.consumer.RecordedEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogProcessor(io.opentelemetry.sdk.logs.LogProcessor) Clock(io.opentelemetry.sdk.common.Clock) SpanContextualizer(com.splunk.opentelemetry.profiler.context.SpanContextualizer)

Example 2 with SpanContextualizer

use of com.splunk.opentelemetry.profiler.context.SpanContextualizer in project splunk-otel-java by signalfx.

the class TLABProcessorTest method testSampling.

@Test
void testSampling() {
    int samplerInterval = 5;
    AtomicReference<LogData> seenLogData = new AtomicReference<>();
    LogProcessor consumer = seenLogData::set;
    StackSerializer serializer = mock(StackSerializer.class);
    LogDataCommonAttributes commonAttrs = new LogDataCommonAttributes(new EventPeriods(x -> null));
    SpanContextualizer spanContextualizer = mock(SpanContextualizer.class);
    when(spanContextualizer.link(anyLong())).thenReturn(SpanLinkage.NONE);
    Config config = mock(Config.class);
    when(config.getBoolean(CONFIG_KEY_TLAB_ENABLED, DEFAULT_MEMORY_ENABLED)).thenReturn(true);
    when(config.getInt(CONFIG_KEY_MEMORY_SAMPLER_INTERVAL, DEFAULT_MEMORY_SAMPLING_INTERVAL)).thenReturn(samplerInterval);
    TLABProcessor processor = TLABProcessor.builder(config).stackSerializer(serializer).logProcessor(consumer).commonAttributes(commonAttrs).resource(Resource.getDefault()).spanContextualizer(spanContextualizer).build();
    RecordedEvent event = createMockEvent(serializer, Instant.now(), null);
    for (int i = 0; i < samplerInterval + 2; i++) {
        processor.accept(event);
        if (i % samplerInterval == 0) {
            assertEquals("otel.profiling", seenLogData.get().getAttributes().get(SOURCE_TYPE));
            assertEquals("tee-lab", seenLogData.get().getAttributes().get(SOURCE_EVENT_NAME));
            assertEquals("Systematic sampler", seenLogData.get().getAttributes().get(SAMPLER_NAME_KEY));
            assertEquals(Long.valueOf(samplerInterval), seenLogData.get().getAttributes().get(SAMPLER_INTERVAL_KEY));
        } else {
            assertNull(seenLogData.get());
        }
        seenLogData.set(null);
    }
}
Also used : Clock(io.opentelemetry.sdk.common.Clock) TraceFlags(io.opentelemetry.api.trace.TraceFlags) LogProcessor(io.opentelemetry.sdk.logs.LogProcessor) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) SpanId(io.opentelemetry.api.trace.SpanId) Resource(io.opentelemetry.sdk.resources.Resource) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) SOURCE_EVENT_NAME(com.splunk.opentelemetry.profiler.ProfilingSemanticAttributes.SOURCE_EVENT_NAME) SpanLinkage(com.splunk.opentelemetry.profiler.context.SpanLinkage) EventPeriods(com.splunk.opentelemetry.profiler.events.EventPeriods) AtomicReference(java.util.concurrent.atomic.AtomicReference) Answer(org.mockito.stubbing.Answer) TraceState(io.opentelemetry.api.trace.TraceState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ALLOCATION_SIZE_KEY(com.splunk.opentelemetry.profiler.TLABProcessor.ALLOCATION_SIZE_KEY) CONFIG_KEY_MEMORY_SAMPLER_INTERVAL(com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_MEMORY_SAMPLER_INTERVAL) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EventType(jdk.jfr.EventType) LogData(io.opentelemetry.sdk.logs.data.LogData) CONFIG_KEY_TLAB_ENABLED(com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_TLAB_ENABLED) SpanContext(io.opentelemetry.api.trace.SpanContext) DEFAULT_MEMORY_ENABLED(com.splunk.opentelemetry.profiler.Configuration.DEFAULT_MEMORY_ENABLED) Mockito.when(org.mockito.Mockito.when) DEFAULT_MEMORY_SAMPLING_INTERVAL(com.splunk.opentelemetry.profiler.Configuration.DEFAULT_MEMORY_SAMPLING_INTERVAL) Instant(java.time.Instant) RecordedStackTrace(jdk.jfr.consumer.RecordedStackTrace) SpanContextualizer(com.splunk.opentelemetry.profiler.context.SpanContextualizer) Config(io.opentelemetry.instrumentation.api.config.Config) TimeUnit(java.util.concurrent.TimeUnit) RecordedEvent(jdk.jfr.consumer.RecordedEvent) Test(org.junit.jupiter.api.Test) SOURCE_TYPE(com.splunk.opentelemetry.profiler.ProfilingSemanticAttributes.SOURCE_TYPE) RecordedThread(jdk.jfr.consumer.RecordedThread) StackSerializer(com.splunk.opentelemetry.profiler.util.StackSerializer) AttributeKey(io.opentelemetry.api.common.AttributeKey) TraceId(io.opentelemetry.api.trace.TraceId) Mockito.mock(org.mockito.Mockito.mock) LogData(io.opentelemetry.sdk.logs.data.LogData) EventPeriods(com.splunk.opentelemetry.profiler.events.EventPeriods) StackSerializer(com.splunk.opentelemetry.profiler.util.StackSerializer) Config(io.opentelemetry.instrumentation.api.config.Config) RecordedEvent(jdk.jfr.consumer.RecordedEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogProcessor(io.opentelemetry.sdk.logs.LogProcessor) SpanContextualizer(com.splunk.opentelemetry.profiler.context.SpanContextualizer) Test(org.junit.jupiter.api.Test)

Example 3 with SpanContextualizer

use of com.splunk.opentelemetry.profiler.context.SpanContextualizer in project splunk-otel-java by signalfx.

the class ThreadDumpProcessorTest method testFilterStacksWithoutSpans.

@Test
void testFilterStacksWithoutSpans() {
    SpanContextualizer contextualizer = new SpanContextualizer();
    sampleThreadsFromDump.forEach(it -> contextualizer.updateContext(threadContextStartEvent(it.threadId)));
    String threadDump = readDumpFromResource("thread-dump1.txt");
    List<StackToSpanLinkage> results = collectResults(contextualizer, threadDump, true);
    assertEquals(sampleThreadsFromDump.size(), results.size());
    sampleThreadsFromDump.forEach(sample -> assertThat(results).anyMatch(stack -> stack.getRawStack().contains(sample.threadName)));
}
Also used : TraceFlags(io.opentelemetry.api.trace.TraceFlags) Arrays(java.util.Arrays) SpanContext(io.opentelemetry.api.trace.SpanContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StackToSpanLinkage(com.splunk.opentelemetry.profiler.context.StackToSpanLinkage) Mockito.when(org.mockito.Mockito.when) ArrayList(java.util.ArrayList) SpanContextualizer(com.splunk.opentelemetry.profiler.context.SpanContextualizer) Consumer(java.util.function.Consumer) RecordedEvent(jdk.jfr.consumer.RecordedEvent) Test(org.junit.jupiter.api.Test) TraceState(io.opentelemetry.api.trace.TraceState) List(java.util.List) ThreadDumpRegionTest.readDumpFromResource(com.splunk.opentelemetry.profiler.ThreadDumpRegionTest.readDumpFromResource) Stream(java.util.stream.Stream) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RecordedThread(jdk.jfr.consumer.RecordedThread) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ContextAttached(com.splunk.opentelemetry.profiler.events.ContextAttached) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EventType(jdk.jfr.EventType) Mockito.mock(org.mockito.Mockito.mock) StackToSpanLinkage(com.splunk.opentelemetry.profiler.context.StackToSpanLinkage) SpanContextualizer(com.splunk.opentelemetry.profiler.context.SpanContextualizer) Test(org.junit.jupiter.api.Test)

Example 4 with SpanContextualizer

use of com.splunk.opentelemetry.profiler.context.SpanContextualizer in project splunk-otel-java by signalfx.

the class ThreadDumpProcessorTest method testProcessEvent.

@Test
void testProcessEvent() {
    SpanContextualizer contextualizer = new SpanContextualizer();
    long idOfThreadRunningTheSpan = 3L;
    SpanContext expectedContext = SpanContext.create(traceId, spanId, TraceFlags.fromByte(traceFlags), TraceState.getDefault());
    contextualizer.updateContext(threadContextStartEvent(idOfThreadRunningTheSpan));
    String threadDump = readDumpFromResource("thread-dump2.txt");
    List<StackToSpanLinkage> results = collectResults(contextualizer, threadDump, false);
    assertEquals(3, results.size());
    assertFalse(results.get(0).hasSpanInfo());
    assertTrue(results.get(0).getRawStack().contains("at java.lang.ref.Reference$ReferenceHandler"));
    assertTrue(results.get(1).hasSpanInfo());
    assertEquals(expectedContext, results.get(1).getSpanContext());
    assertEquals(idOfThreadRunningTheSpan, results.get(1).getSpanStartThread());
    assertTrue(results.get(1).getRawStack().contains("AwesomeThinger.overHereDoingSpanThings"));
    assertFalse(results.get(2).hasSpanInfo());
    assertTrue(results.get(2).getRawStack().contains("0x0000000625152778"));
}
Also used : StackToSpanLinkage(com.splunk.opentelemetry.profiler.context.StackToSpanLinkage) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContextualizer(com.splunk.opentelemetry.profiler.context.SpanContextualizer) Test(org.junit.jupiter.api.Test)

Example 5 with SpanContextualizer

use of com.splunk.opentelemetry.profiler.context.SpanContextualizer in project splunk-otel-java by signalfx.

the class ThreadDumpProcessorTest method testFilterInternalStacks.

@Test
void testFilterInternalStacks() {
    SpanContextualizer contextualizer = new SpanContextualizer();
    String threadDump = readDumpFromResource("thread-dump1.txt");
    List<StackToSpanLinkage> results = collectResults(contextualizer, threadDump, false);
    assertEquals(28, results.size());
    Stream.of(StackTraceFilter.UNWANTED_PREFIXES).forEach(prefix -> {
        assertThat(results).noneMatch(stack -> stack.getRawStack().contains(prefix));
    });
}
Also used : StackToSpanLinkage(com.splunk.opentelemetry.profiler.context.StackToSpanLinkage) SpanContextualizer(com.splunk.opentelemetry.profiler.context.SpanContextualizer) Test(org.junit.jupiter.api.Test)

Aggregations

SpanContextualizer (com.splunk.opentelemetry.profiler.context.SpanContextualizer)8 StackToSpanLinkage (com.splunk.opentelemetry.profiler.context.StackToSpanLinkage)5 RecordedEvent (jdk.jfr.consumer.RecordedEvent)5 Test (org.junit.jupiter.api.Test)5 SpanContext (io.opentelemetry.api.trace.SpanContext)4 EventPeriods (com.splunk.opentelemetry.profiler.events.EventPeriods)3 TraceFlags (io.opentelemetry.api.trace.TraceFlags)3 TraceState (io.opentelemetry.api.trace.TraceState)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Consumer (java.util.function.Consumer)3 EventType (jdk.jfr.EventType)3 RecordedThread (jdk.jfr.consumer.RecordedThread)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.when (org.mockito.Mockito.when)3 CONFIG_KEY_MEMORY_SAMPLER_INTERVAL (com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_MEMORY_SAMPLER_INTERVAL)2 CONFIG_KEY_TLAB_ENABLED (com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_TLAB_ENABLED)2 DEFAULT_MEMORY_ENABLED (com.splunk.opentelemetry.profiler.Configuration.DEFAULT_MEMORY_ENABLED)2