use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java by open-telemetry.
the class TestClientServerTest method verify.
private void verify() {
await().atMost(Duration.ofSeconds(15)).until(finishedSpansSize(otelTesting), equalTo(2));
List<SpanData> finished = otelTesting.getSpans();
assertThat(finished).hasSize(2);
assertThat(finished.get(1).getTraceId()).isEqualTo(finished.get(0).getTraceId());
SpanKind firstSpanKind = finished.get(0).getKind();
switch(firstSpanKind) {
case CLIENT:
assertThat(finished.get(1).getKind()).isEqualTo(SpanKind.SERVER);
break;
case SERVER:
assertThat(finished.get(1).getKind()).isEqualTo(SpanKind.CLIENT);
break;
default:
fail("Unexpected first span kind: " + firstSpanKind);
}
assertThat(tracer.scopeManager().activeSpan()).isNull();
}
use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java-instrumentation by open-telemetry.
the class Instrumenter method start.
/**
* Starts a new operation to be instrumented. The {@code parentContext} is the parent of the
* resulting instrumented operation and should usually be {@code Context.current()}. The {@code
* request} is the request object of this operation. The returned {@link Context} should be
* propagated along with the operation and passed to {@link #end(Context, Object, Object,
* Throwable)} when it is finished.
*/
public Context start(Context parentContext, REQUEST request) {
SpanKind spanKind = spanKindExtractor.extract(request);
SpanBuilder spanBuilder = tracer.spanBuilder(spanNameExtractor.extract(request)).setSpanKind(spanKind).setParent(parentContext);
Instant startTime = null;
if (timeExtractor != null) {
startTime = timeExtractor.extractStartTime(request);
spanBuilder.setStartTimestamp(startTime);
}
SpanLinksBuilder spanLinksBuilder = new SpanLinksBuilderImpl(spanBuilder);
for (SpanLinksExtractor<? super REQUEST> spanLinksExtractor : spanLinksExtractors) {
spanLinksExtractor.extract(spanLinksBuilder, parentContext, request);
}
UnsafeAttributes attributesBuilder = new UnsafeAttributes();
for (AttributesExtractor<? super REQUEST, ? super RESPONSE> extractor : attributesExtractors) {
extractor.onStart(attributesBuilder, parentContext, request);
}
Attributes attributes = attributesBuilder;
Context context = parentContext;
for (ContextCustomizer<? super REQUEST> contextCustomizer : contextCustomizers) {
context = contextCustomizer.start(context, request, attributes);
}
if (!requestListeners.isEmpty()) {
long startNanos = getNanos(startTime);
for (RequestListener requestListener : requestListeners) {
context = requestListener.start(context, attributes, startNanos);
}
}
spanBuilder.setAllAttributes(attributes);
Span span = spanBuilder.startSpan();
context = context.with(span);
// for them to generate exemplars
if (!requestMetricListeners.isEmpty()) {
long startNanos = getNanos(startTime);
for (RequestListener requestListener : requestMetricListeners) {
context = requestListener.start(context, attributes, startNanos);
}
}
return spanSuppressionStrategy.storeInContext(context, spanKind, span);
}
use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java-instrumentation by open-telemetry.
the class CamelRoutePolicy method spanOnExchangeBegin.
private static Context spanOnExchangeBegin(Route route, Exchange exchange, SpanDecorator sd, Context parentContext) {
Span activeSpan = Span.fromContext(parentContext);
if (!activeSpan.getSpanContext().isValid()) {
parentContext = CamelPropagationUtil.extractParent(exchange.getIn().getHeaders(), route.getEndpoint());
}
SpanKind spanKind = spanKind(activeSpan, sd);
CamelRequest request = CamelRequest.create(sd, exchange, route.getEndpoint(), CamelDirection.INBOUND, spanKind);
sd.updateServerSpanName(parentContext, exchange, route.getEndpoint(), CamelDirection.INBOUND);
if (!instrumenter().shouldStart(parentContext, request)) {
return null;
}
Context context = instrumenter().start(parentContext, request);
ActiveContextManager.activate(context, request);
return context;
}
Aggregations