use of io.opentracing.Scope in project cxf by apache.
the class AbstractOpenTracingProvider method startTraceSpan.
protected TraceScopeHolder<TraceScope> startTraceSpan(final Map<String, List<String>> requestHeaders, URI uri, String method) {
SpanContext parent = tracer.extract(Builtin.HTTP_HEADERS, new TextMapAdapter(requestHeaders.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, this::getFirstValueOrEmpty))));
final Span activeSpan;
final Scope scope;
if (parent == null) {
activeSpan = tracer.buildSpan(buildSpanDescription(uri.getPath(), method)).start();
scope = tracer.scopeManager().activate(activeSpan);
} else {
activeSpan = tracer.buildSpan(buildSpanDescription(uri.getPath(), method)).asChildOf(parent).start();
scope = tracer.scopeManager().activate(activeSpan);
}
// Set additional tags
activeSpan.setTag(Tags.HTTP_METHOD.getKey(), method);
activeSpan.setTag(Tags.HTTP_URL.getKey(), uri.toString());
// If the service resource is using asynchronous processing mode, the trace
// scope will be closed in another thread and as such should be detached.
Span span = null;
if (isAsyncResponse()) {
// Do not modify the current context span
span = activeSpan;
propagateContinuationSpan(span);
scope.close();
}
return new TraceScopeHolder<TraceScope>(new TraceScope(activeSpan, scope), span != null);
}
use of io.opentracing.Scope in project cxf by apache.
the class OpenTracingTracingTest method testThatProvidedSpanIsNotClosedWhenActive.
@Test
public void testThatProvidedSpanIsNotClosedWhenActive() throws Exception {
final BookStoreService service = createJaxWsService(new OpenTracingClientFeature(tracer));
final Span span = tracer.buildSpan("test span").start();
try (Scope scope = tracer.activateSpan(span)) {
assertThat(service.getBooks().size(), equalTo(2));
assertThat(tracer.activeSpan(), not(nullValue()));
assertThat(REPORTER.getSpans().size(), equalTo(3));
assertThat(REPORTER.getSpans().get(0).getOperationName(), equalTo("Get Books"));
assertThat(REPORTER.getSpans().get(0).getReferences(), not(empty()));
assertThat(REPORTER.getSpans().get(1).getOperationName(), equalTo("POST /BookStore"));
assertThat(REPORTER.getSpans().get(1).getReferences(), not(empty()));
assertThat(REPORTER.getSpans().get(2).getOperationName(), equalTo("POST http://localhost:" + PORT + "/BookStore"));
assertThat(REPORTER.getSpans().get(2).getReferences(), not(empty()));
} finally {
span.finish();
}
// Await till flush happens, usually every second
await().atMost(Duration.ofSeconds(1L)).until(() -> REPORTER.getSpans().size() == 4);
assertThat(REPORTER.getSpans().size(), equalTo(4));
assertThat(REPORTER.getSpans().get(3).getOperationName(), equalTo("test span"));
assertThat(REPORTER.getSpans().get(3).getReferences(), empty());
}
use of io.opentracing.Scope in project cxf by apache.
the class OpenTracingTracingTest method testThatProvidedSpanIsNotClosedWhenActive.
@Test
public void testThatProvidedSpanIsNotClosedWhenActive() throws MalformedURLException {
final WebClient client = createWebClient("/bookstore/books", new OpenTracingClientProvider(tracer));
final Span span = tracer.buildSpan("test span").start();
try (Scope scope = tracer.scopeManager().activate(span)) {
final Response r = client.get();
assertEquals(Status.OK.getStatusCode(), r.getStatus());
assertEquals(REPORTER.getSpans().toString(), 3, REPORTER.getSpans().size());
assertThat(REPORTER.getSpans().get(0).getOperationName(), equalTo("Get Books"));
assertThat(REPORTER.getSpans().get(0).getReferences(), not(empty()));
assertThat(REPORTER.getSpans().get(1).getOperationName(), equalTo("GET /bookstore/books"));
assertThat(REPORTER.getSpans().get(1).getReferences(), not(empty()));
assertThat(REPORTER.getSpans().get(2).getOperationName(), equalTo("GET " + client.getCurrentURI()));
assertThat(REPORTER.getSpans().get(2).getReferences(), not(empty()));
} finally {
span.finish();
}
// Await till flush happens, usually every second
await().atMost(Duration.ofSeconds(1L)).until(() -> REPORTER.getSpans().size() == 4);
assertThat(REPORTER.getSpans().size(), equalTo(4));
assertThat(REPORTER.getSpans().get(3).getOperationName(), equalTo("test span"));
assertThat(REPORTER.getSpans().get(3).getReferences(), empty());
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class AutoFinishScopeManagerTest method activateSpan.
@Test
public void activateSpan() throws Exception {
Span span = mock(Span.class);
// We can't use 1.7 features like try-with-resources in this repo without meddling with pom details for tests.
Scope active = source.activate(span, true);
try {
assertNotNull(active);
Scope otherScope = source.active();
assertEquals(otherScope, active);
} finally {
active.close();
}
// Make sure the Span got finish()ed.
verify(span).finish();
// And now it's gone:
Scope missingSpan = source.active();
assertNull(missingSpan);
}
use of io.opentracing.Scope in project opentracing-java by opentracing.
the class AutoFinishScopeTest method continuation.
@Test
public void continuation() throws Exception {
Span span = mock(Span.class);
// Quasi try-with-resources (this is 1.6).
AutoFinishScope active = (AutoFinishScope) manager.activate(span, true);
AutoFinishScope.Continuation continued = null;
try {
assertNotNull(active);
continued = active.capture();
} finally {
active.close();
}
// Make sure the Span was not finished since there was a capture().
verify(span, never()).finish();
// Activate the continuation.
try {
active = continued.activate();
} finally {
active.close();
}
// Now the Span should be finished.
verify(span, times(1)).finish();
// And now it's no longer active.
Scope missingSpan = manager.active();
assertNull(missingSpan);
}
Aggregations