Search in sources :

Example 31 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project splunk-otel-java by signalfx.

the class LogDataCreatorTest method testCreate.

@Test
void testCreate() {
    Instant time = Instant.now();
    String stack = "the.stack";
    SpanContext spanContext = SpanContext.create("deadbeefdeadbeefdeadbeefdeadbeef", "0123012301230123", TraceFlags.getSampled(), TraceState.getDefault());
    Context context = Context.root().with(Span.wrap(spanContext));
    long threadId = 987L;
    String eventName = "GoodEventHere";
    EventPeriods periods = mock(EventPeriods.class);
    when(periods.getDuration(eventName)).thenReturn(Duration.ofMillis(606));
    SpanLinkage linkage = new SpanLinkage(spanContext, threadId);
    Resource resource = Resource.getDefault();
    Attributes attributes = Attributes.of(SOURCE_EVENT_NAME, eventName, SOURCE_EVENT_PERIOD, 606L, SOURCE_TYPE, "otel.profiling");
    LogData expected = LogDataBuilder.create(resource, INSTRUMENTATION_LIBRARY_INFO).setContext(context).setBody(stack).setEpoch(time).setAttributes(attributes).build();
    StackToSpanLinkage linkedSpan = new StackToSpanLinkage(time, "the.stack", eventName, linkage);
    LogDataCreator creator = new LogDataCreator(new LogDataCommonAttributes(periods), Resource.getDefault());
    LogData result = creator.apply(linkedSpan);
    assertEquals(expected, result);
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) EventPeriods(com.splunk.opentelemetry.profiler.events.EventPeriods) LogData(io.opentelemetry.sdk.logs.data.LogData) StackToSpanLinkage(com.splunk.opentelemetry.profiler.context.StackToSpanLinkage) SpanContext(io.opentelemetry.api.trace.SpanContext) StackToSpanLinkage(com.splunk.opentelemetry.profiler.context.StackToSpanLinkage) SpanLinkage(com.splunk.opentelemetry.profiler.context.SpanLinkage) Instant(java.time.Instant) Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 32 with SpanContext

use of io.opentelemetry.api.trace.SpanContext 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 33 with SpanContext

use of io.opentelemetry.api.trace.SpanContext 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 34 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project splunk-otel-java by signalfx.

the class JfrContextStorage method generateEvent.

private void generateEvent(Span span) {
    SpanContext context = span.getSpanContext();
    ContextAttached event = newEvent.apply(context);
    event.begin();
    if (event.shouldCommit()) {
        event.commit();
    }
}
Also used : ContextAttached(com.splunk.opentelemetry.profiler.events.ContextAttached) SpanContext(io.opentelemetry.api.trace.SpanContext)

Example 35 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.

the class SdkSpanTest method testAsSpanData.

@Test
void testAsSpanData() {
    String name = "GreatSpan";
    SpanKind kind = SpanKind.SERVER;
    String traceId = this.traceId;
    String spanId = this.spanId;
    String parentSpanId = this.parentSpanId;
    SpanLimits spanLimits = SpanLimits.getDefault();
    SpanProcessor spanProcessor = NoopSpanProcessor.getInstance();
    TestClock clock = TestClock.create();
    Resource resource = this.resource;
    Attributes attributes = TestUtils.generateRandomAttributes();
    AttributesMap attributesWithCapacity = new AttributesMap(32, Integer.MAX_VALUE);
    attributes.forEach((key, value) -> attributesWithCapacity.put((AttributeKey) key, value));
    Attributes event1Attributes = TestUtils.generateRandomAttributes();
    Attributes event2Attributes = TestUtils.generateRandomAttributes();
    SpanContext context = SpanContext.create(traceId, spanId, TraceFlags.getDefault(), TraceState.getDefault());
    LinkData link1 = LinkData.create(context, TestUtils.generateRandomAttributes());
    SdkSpan readableSpan = SdkSpan.startSpan(context, name, instrumentationLibraryInfo, kind, parentSpanId != null ? Span.wrap(SpanContext.create(traceId, parentSpanId, TraceFlags.getDefault(), TraceState.getDefault())) : Span.getInvalid(), Context.root(), spanLimits, spanProcessor, clock, resource, attributesWithCapacity, Collections.singletonList(link1), 1, 0);
    long startEpochNanos = clock.now();
    clock.advance(Duration.ofMillis(4));
    long firstEventEpochNanos = clock.now();
    readableSpan.addEvent("event1", event1Attributes);
    clock.advance(Duration.ofMillis(6));
    long secondEventTimeNanos = clock.now();
    readableSpan.addEvent("event2", event2Attributes);
    clock.advance(Duration.ofMillis(100));
    readableSpan.end();
    long endEpochNanos = clock.now();
    List<EventData> events = Arrays.asList(EventData.create(firstEventEpochNanos, "event1", event1Attributes, event1Attributes.size()), EventData.create(secondEventTimeNanos, "event2", event2Attributes, event2Attributes.size()));
    SpanData result = readableSpan.toSpanData();
    verifySpanData(result, attributesWithCapacity, events, Collections.singletonList(link1), name, startEpochNanos, endEpochNanos, StatusData.unset(), /* hasEnded= */
    true);
    assertThat(result.getTotalRecordedLinks()).isEqualTo(1);
    assertThat(result.getSpanContext().isSampled()).isEqualTo(false);
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) LinkData(io.opentelemetry.sdk.trace.data.LinkData) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Resource(io.opentelemetry.sdk.resources.Resource) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes) SpanKind(io.opentelemetry.api.trace.SpanKind) EventData(io.opentelemetry.sdk.trace.data.EventData) TestClock(io.opentelemetry.sdk.testing.time.TestClock) AttributeKey(io.opentelemetry.api.common.AttributeKey) Test(org.junit.jupiter.api.Test)

Aggregations

SpanContext (io.opentelemetry.api.trace.SpanContext)62 Test (org.junit.jupiter.api.Test)33 Span (io.opentelemetry.api.trace.Span)31 Context (io.opentelemetry.context.Context)31 SpanKind (io.opentelemetry.api.trace.SpanKind)13 TraceState (io.opentelemetry.api.trace.TraceState)12 W3CTraceContextPropagator (io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator)12 Collectors (java.util.stream.Collectors)12 Attributes (io.opentelemetry.api.common.Attributes)11 TraceFlags (io.opentelemetry.api.trace.TraceFlags)10 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)10 Instant (java.time.Instant)10 SpanId (io.opentelemetry.api.trace.SpanId)9 TraceId (io.opentelemetry.api.trace.TraceId)9 LinkData (io.opentelemetry.sdk.trace.data.LinkData)9 Mockito.when (org.mockito.Mockito.when)9 AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)8 ContextKey (io.opentelemetry.context.ContextKey)7 TextMapGetter (io.opentelemetry.context.propagation.TextMapGetter)7 InstrumentationVersion (io.opentelemetry.instrumentation.api.InstrumentationVersion)7