use of io.opentracing.Scope in project Payara by payara.
the class OpenTracingRequestEventListener method onIncomingRequest.
private void onIncomingRequest(final RequestEvent event, final String operationName) {
LOG.fine(() -> "onIncomingRequest(event=" + event.getType() + ", operationName=" + operationName + ")");
final ContainerRequest requestContext = event.getContainerRequest();
final Tracer tracer = openTracing.getTracer(this.applicationName);
// Create a Span and instrument it with details about the request
final SpanBuilder spanBuilder = //
tracer.buildSpan(operationName).withTag(Tags.SPAN_KIND.getKey(), //
Tags.SPAN_KIND_SERVER).withTag(Tags.HTTP_METHOD.getKey(), //
requestContext.getMethod()).withTag(Tags.HTTP_URL.getKey(), //
toString(requestContext.getUriInfo())).withTag(Tags.COMPONENT.getKey(), "jaxrs");
// If there was a context injected into the tracer, add it as a parent of the new span
final SpanContext spanContext = findSpanContext(requestContext, tracer);
if (spanContext != null) {
spanBuilder.asChildOf(spanContext);
}
// Start the span and continue on to the targeted method
Scope scope = tracer.activateSpan(spanBuilder.start());
// tracer.activeSpan();
requestContext.setProperty(Scope.class.getName(), scope);
LOG.fine(() -> "Request tracing enabled for request=" + requestContext.getRequest() + " to application=" + this.applicationName + " on uri=" + toString(requestContext.getUriInfo()));
}
use of io.opentracing.Scope in project java-spring-web by opentracing-contrib.
the class TracingHandlerInterceptor method afterCompletion.
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, Exception ex) throws Exception {
if (!isTraced(httpServletRequest)) {
return;
}
Deque<Scope> scopeStack = getScopeStack(httpServletRequest);
Scope scope = scopeStack.pop();
for (HandlerInterceptorSpanDecorator decorator : decorators) {
decorator.onAfterCompletion(httpServletRequest, httpServletResponse, handler, ex, scope.span());
}
scope.close();
}
use of io.opentracing.Scope in project java-spring-web by opentracing-contrib.
the class TestController method async.
@RequestMapping("/async")
public Callable<String> async() {
verifyActiveSpan();
final Span cont = tracer.scopeManager().active().span();
return new Callable<String>() {
public String call() throws Exception {
try (Scope scope = tracer.scopeManager().activate(cont, false)) {
if (tracer.scopeManager().active() == null) {
throw new RuntimeException("No active span");
}
Thread.sleep(1000);
return "async";
}
}
};
}
use of io.opentracing.Scope in project java-spring-web by opentracing-contrib.
the class TracingAsyncRestTemplateTest method testMultipleRequests.
@Test
public void testMultipleRequests() throws InterruptedException, ExecutionException {
final String url = "http://localhost:8080/foo/";
int numberOfCalls = 1000;
mockServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(new Contains("/foo"))).andRespond(MockRestResponseCreators.withSuccess());
ExecutorService executorService = Executors.newFixedThreadPool(100);
List<Future<?>> futures = new ArrayList<>(numberOfCalls);
for (int i = 0; i < numberOfCalls; i++) {
final String requestUrl = url + i;
final Scope parentSpan = mockTracer.buildSpan("foo").startActive(false);
parentSpan.span().setTag("request-url", requestUrl);
final Span cont = parentSpan.span();
futures.add(executorService.submit(new Runnable() {
@Override
public void run() {
try (Scope span = mockTracer.scopeManager().activate(cont, true)) {
client.getForEntity(requestUrl, String.class);
}
}
}));
parentSpan.close();
}
// wait to finish all calls
for (Future<?> future : futures) {
future.get();
}
executorService.awaitTermination(1, TimeUnit.SECONDS);
executorService.shutdown();
List<MockSpan> mockSpans = mockTracer.finishedSpans();
Assert.assertEquals(numberOfCalls * 2, mockSpans.size());
final List<MockSpan> parentSpans = new ArrayList<>();
final Map<Long, MockSpan> childSpans = new HashMap<>();
for (MockSpan mockSpan : mockSpans) {
if (mockSpan.tags().containsKey("request-url")) {
parentSpans.add(mockSpan);
} else {
childSpans.put(mockSpan.parentId(), mockSpan);
}
}
Assert.assertEquals(numberOfCalls, parentSpans.size());
Assert.assertEquals(numberOfCalls, childSpans.size());
for (MockSpan parentSpan : parentSpans) {
MockSpan childSpan = childSpans.get(parentSpan.context().spanId());
Assert.assertEquals(parentSpan.tags().get("request-url"), childSpan.tags().get(Tags.HTTP_URL.getKey()));
Assert.assertEquals(parentSpan.context().traceId(), childSpan.context().traceId());
Assert.assertEquals(parentSpan.context().spanId(), childSpan.parentId());
Assert.assertEquals(0, childSpan.generatedErrors().size());
Assert.assertEquals(0, parentSpan.generatedErrors().size());
}
}
use of io.opentracing.Scope in project keywhiz by square.
the class Tracing method trace.
public static <T> T trace(String spanName, Supplier<T> delegate) {
Tracer tracer = GlobalTracer.get();
Span span = tracer.buildSpan(spanName).start();
try {
try (Scope ignored = tracer.scopeManager().activate(span)) {
return delegate.get();
} catch (Exception e) {
tagErrors(span, e);
throw e;
}
} finally {
span.finish();
}
}
Aggregations