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())));
}
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");
}
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();
}
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());
}
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());
}
Aggregations