Search in sources :

Example 51 with SpanContext

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

the class InstrumenterTest method server_error.

@Test
void server_error() {
    Instrumenter<Map<String, String>, Map<String, String>> instrumenter = Instrumenter.<Map<String, String>, Map<String, String>>builder(otelTesting.getOpenTelemetry(), "test", unused -> "span").addAttributesExtractors(new AttributesExtractor1(), new AttributesExtractor2()).newServerInstrumenter(new MapGetter());
    Context context = instrumenter.start(Context.root(), REQUEST);
    SpanContext spanContext = Span.fromContext(context).getSpanContext();
    assertThat(spanContext.isValid()).isTrue();
    assertThat(SpanKey.SERVER.fromContextOrNull(context).getSpanContext()).isEqualTo(spanContext);
    instrumenter.end(context, REQUEST, RESPONSE, new IllegalStateException("test"));
    otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasStatus(StatusData.error())));
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) NetServerAttributesGetter(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter) TraceFlags(io.opentelemetry.api.trace.TraceFlags) OpenTelemetryExtension(io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension) SpanId(io.opentelemetry.api.trace.SpanId) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) Mock(org.mockito.Mock) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) StatusData(io.opentelemetry.sdk.trace.data.StatusData) Attributes(io.opentelemetry.api.common.Attributes) HashMap(java.util.HashMap) InstrumentationVersion(io.opentelemetry.instrumentation.api.InstrumentationVersion) AtomicReference(java.util.concurrent.atomic.AtomicReference) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) TraceState(io.opentelemetry.api.trace.TraceState) SpanKey(io.opentelemetry.instrumentation.api.internal.SpanKey) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Map(java.util.Map) DbClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesExtractor) HttpServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor) HttpClientAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor) Nullable(javax.annotation.Nullable) OpenTelemetryAssertions.attributeEntry(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry) ContextKey(io.opentelemetry.context.ContextKey) Context(io.opentelemetry.context.Context) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) MessageOperation(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) SpanKind(io.opentelemetry.api.trace.SpanKind) Collectors(java.util.stream.Collectors) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test) NetServerAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesExtractor) Stream(java.util.stream.Stream) OpenTelemetryAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) TraceId(io.opentelemetry.api.trace.TraceId) MessagingAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.messaging.MessagingAttributesExtractor) LinkData(io.opentelemetry.sdk.trace.data.LinkData) RpcAttributesExtractor(io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesExtractor) Collections(java.util.Collections) SpanContext(io.opentelemetry.api.trace.SpanContext) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 52 with SpanContext

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

the class CamelPropagationUtilTest method shouldExtractHttpParentForHttpEndpoint.

@Test
public void shouldExtractHttpParentForHttpEndpoint() throws Exception {
    // given
    Endpoint endpoint = new HttpEndpoint("", new HttpComponent(), URI.create(""));
    Map<String, Object> exchangeHeaders = Collections.singletonMap("uber-trace-id", "1f7f8dab3f0043b1b9cf0a75caf57510:a13825abcb764bd3:0:1");
    // when
    Context parent = CamelPropagationUtil.extractParent(exchangeHeaders, endpoint);
    // then
    Span parentSpan = Span.fromContext(parent);
    SpanContext parentSpanContext = parentSpan.getSpanContext();
    assertThat(parentSpanContext.getTraceId()).isEqualTo("1f7f8dab3f0043b1b9cf0a75caf57510");
    assertThat(parentSpanContext.getSpanId()).isEqualTo("a13825abcb764bd3");
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext) HttpEndpoint(org.apache.camel.component.http.HttpEndpoint) SqsEndpoint(org.apache.camel.component.aws.sqs.SqsEndpoint) Endpoint(org.apache.camel.Endpoint) HttpComponent(org.apache.camel.component.http.HttpComponent) HttpEndpoint(org.apache.camel.component.http.HttpEndpoint) Span(io.opentelemetry.api.trace.Span) Test(org.junit.jupiter.api.Test)

Example 53 with SpanContext

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

the class JfrContextStorageTest method testAttachLifecycle.

@Test
void testAttachLifecycle() {
    ContextAttached inEvent = mock(ContextAttached.class);
    ContextAttached outEvent = mock(ContextAttached.class);
    Function<SpanContext, ContextAttached> newEvent = mock(Function.class);
    when(delegate.attach(newContext)).thenReturn(delegatedScope);
    when(inEvent.shouldCommit()).thenReturn(true);
    when(outEvent.shouldCommit()).thenReturn(true);
    when(newEvent.apply(spanContext)).thenReturn(inEvent);
    when(newEvent.apply(SpanContext.getInvalid())).thenReturn(outEvent);
    JfrContextStorage contextStorage = new JfrContextStorage(delegate, newEvent);
    Scope resultScope = contextStorage.attach(newContext);
    verify(inEvent).begin();
    verify(inEvent).commit();
    verify(outEvent, never()).begin();
    verify(outEvent, never()).commit();
    verify(delegatedScope, never()).close();
    // returns back to the initial/default span
    resultScope.close();
    verify(outEvent).begin();
    verify(outEvent).commit();
    verify(delegatedScope).close();
}
Also used : ContextAttached(com.splunk.opentelemetry.profiler.events.ContextAttached) SpanContext(io.opentelemetry.api.trace.SpanContext) Scope(io.opentelemetry.context.Scope) Test(org.junit.jupiter.api.Test)

Example 54 with SpanContext

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

the class JfrContextStorageTest method testNotSampled.

@Test
void testNotSampled() {
    Scope scope = mock(Scope.class);
    ContextStorage delegate = mock(ContextStorage.class);
    spanContext = SpanContext.create(traceId, spanId, TraceFlags.getDefault(), TraceState.getDefault());
    span = Span.wrap(spanContext);
    newContext = Context.root().with(span);
    when(delegate.attach(newContext)).thenReturn(scope);
    AtomicBoolean newEventWasCalled = new AtomicBoolean(false);
    Function<SpanContext, ContextAttached> newEvent = x -> {
        newEventWasCalled.set(true);
        return null;
    };
    JfrContextStorage contextStorage = new JfrContextStorage(delegate, newEvent);
    Scope result = contextStorage.attach(newContext);
    assertEquals(scope, result);
    assertFalse(newEventWasCalled.get());
}
Also used : ContextAttached(com.splunk.opentelemetry.profiler.events.ContextAttached) Assertions.fail(org.junit.jupiter.api.Assertions.fail) TraceFlags(io.opentelemetry.api.trace.TraceFlags) Context(io.opentelemetry.context.Context) ContextStorage(io.opentelemetry.context.ContextStorage) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SpanId(io.opentelemetry.api.trace.SpanId) Scope(io.opentelemetry.context.Scope) SpanContext(io.opentelemetry.api.trace.SpanContext) Mock(org.mockito.Mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) TraceState(io.opentelemetry.api.trace.TraceState) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TraceId(io.opentelemetry.api.trace.TraceId) ContextAttached(com.splunk.opentelemetry.profiler.events.ContextAttached) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SpanContext(io.opentelemetry.api.trace.SpanContext) Scope(io.opentelemetry.context.Scope) ContextStorage(io.opentelemetry.context.ContextStorage) Test(org.junit.jupiter.api.Test)

Example 55 with SpanContext

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

the class TLABProcessor method accept.

@Override
public void accept(RecordedEvent event) {
    // them because JFR sends all enabled events to all recordings, if that is the case ignore them.
    if (!enabled) {
        return;
    }
    RecordedStackTrace stackTrace = event.getStackTrace();
    if (stackTrace == null) {
        return;
    }
    // Discard events not chosen by the sampling strategy
    if (sampler != null && !sampler.shouldSample(event)) {
        return;
    }
    Instant time = event.getStartTime();
    String body = buildBody(event, stackTrace);
    AttributesBuilder builder = commonAttributes.builder(event.getEventType().getName()).put(ALLOCATION_SIZE_KEY, event.getLong("allocationSize"));
    if (sampler != null) {
        sampler.addAttributes(builder);
    }
    Attributes attributes = builder.build();
    LogDataBuilder logDataBuilder = LogDataBuilder.create(resource, INSTRUMENTATION_LIBRARY_INFO).setEpoch(time).setBody(body).setAttributes(attributes);
    RecordedThread thread = event.getThread();
    if (thread != null) {
        SpanContext spanContext = spanContextualizer.link(thread.getJavaThreadId()).getSpanContext();
        if (spanContext.isValid()) {
            logDataBuilder.setSpanContext(spanContext);
        }
    }
    logProcessor.emit(logDataBuilder.build());
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) Instant(java.time.Instant) Attributes(io.opentelemetry.api.common.Attributes) LogDataBuilder(io.opentelemetry.sdk.logs.data.LogDataBuilder) RecordedStackTrace(jdk.jfr.consumer.RecordedStackTrace) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) RecordedThread(jdk.jfr.consumer.RecordedThread)

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