use of io.opentracing.Span in project java-spring-web by opentracing-contrib.
the class TracingAsyncRestTemplateInterceptor method intercept.
@Override
public ListenableFuture<ClientHttpResponse> intercept(final HttpRequest httpRequest, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException {
final Scope scope = tracer.buildSpan(httpRequest.getMethod().toString()).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).startActive(false);
tracer.inject(scope.span().context(), Format.Builtin.HTTP_HEADERS, new HttpHeadersCarrier(httpRequest.getHeaders()));
for (RestTemplateSpanDecorator spanDecorator : spanDecorators) {
try {
spanDecorator.onRequest(httpRequest, scope.span());
} catch (RuntimeException exDecorator) {
log.error("Exception during decorating span", exDecorator);
}
}
ListenableFuture<ClientHttpResponse> future = execution.executeAsync(httpRequest, body);
final Span span = scope.span();
future.addCallback(new ListenableFutureCallback<ClientHttpResponse>() {
@Override
public void onSuccess(ClientHttpResponse httpResponse) {
try (Scope asyncScope = tracer.scopeManager().activate(span, true)) {
for (RestTemplateSpanDecorator spanDecorator : spanDecorators) {
try {
spanDecorator.onResponse(httpRequest, httpResponse, scope.span());
} catch (RuntimeException exDecorator) {
log.error("Exception during decorating span", exDecorator);
}
}
}
}
@Override
public void onFailure(Throwable ex) {
try (Scope asyncScope = tracer.scopeManager().activate(span, true)) {
for (RestTemplateSpanDecorator spanDecorator : spanDecorators) {
try {
spanDecorator.onError(httpRequest, ex, scope.span());
} catch (RuntimeException exDecorator) {
log.error("Exception during decorating span", exDecorator);
}
}
}
}
});
scope.close();
return future;
}
use of io.opentracing.Span in project java-spring-web by opentracing-contrib.
the class TracingHandlerInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
if (!isTraced(httpServletRequest)) {
return true;
}
/**
* 1. check if there is an active span, it has been activated in servlet filter or in this interceptor (forward)
* 2. if there is no active span then it can be handling of an async request or spring boot default error handling
*/
Scope serverSpan = tracer.scopeManager().active();
if (serverSpan == null) {
if (httpServletRequest.getAttribute(CONTINUATION_FROM_ASYNC_STARTED) != null) {
Span contd = (Span) httpServletRequest.getAttribute(CONTINUATION_FROM_ASYNC_STARTED);
serverSpan = tracer.scopeManager().activate(contd, false);
httpServletRequest.removeAttribute(CONTINUATION_FROM_ASYNC_STARTED);
} else {
// spring boot default error handling, executes interceptor after processing in the filter (ugly huh?)
serverSpan = tracer.buildSpan(httpServletRequest.getMethod()).addReference(References.FOLLOWS_FROM, TracingFilter.serverSpanContext(httpServletRequest)).startActive(true);
}
}
for (HandlerInterceptorSpanDecorator decorator : decorators) {
decorator.onPreHandle(httpServletRequest, handler, serverSpan.span());
}
Deque<Scope> activeSpanStack = getScopeStack(httpServletRequest);
activeSpanStack.push(serverSpan);
return true;
}
use of io.opentracing.Span in project java-spring-web by opentracing-contrib.
the class TestController method localSpan.
@RequestMapping("/localSpan")
public String localSpan(HttpServletRequest request) {
verifyActiveSpan();
io.opentracing.Tracer.SpanBuilder spanBuilder = tracer.buildSpan("localSpan");
Span localSpan = spanBuilder.startManual();
localSpan.finish();
return "sync";
}
use of io.opentracing.Span in project Payara by payara.
the class RemoteEjbClientIT method executeRemoteEjbMethodIT.
@Test
public void executeRemoteEjbMethodIT() throws NamingException {
Properties contextProperties = new Properties();
contextProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
contextProperties.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
contextProperties.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
Context context = new InitialContext(contextProperties);
EjbRemote ejb = (EjbRemote) context.lookup(String.format("java:global%sEjb", uri.getPath()));
Tracer tracer = GlobalTracer.get();
Span span = null;
try {
span = tracer.buildSpan("ExecuteEjb").start();
tracer.activateSpan(span);
span.setBaggageItem("Wibbles", "Wobbles");
String baggageItems = ejb.annotatedMethod();
Assert.assertTrue("Baggage items didn't match, received: " + baggageItems, baggageItems.contains("\nWibbles : Wobbles\n"));
span.setBaggageItem("Nibbles", "Nobbles");
baggageItems = ejb.nonAnnotatedMethod();
Assert.assertTrue("Baggage items didn't match, received: " + baggageItems, baggageItems.contains("Wibbles : Wobbles") && baggageItems.contains("Nibbles : Nobbles"));
span.setBaggageItem("Bibbles", "Bobbles");
baggageItems = ejb.shouldNotBeTraced();
Assert.assertTrue("Baggage items didn't match, received: " + baggageItems, baggageItems.contains("Wibbles : Wobbles") && baggageItems.contains("Nibbles : Nobbles") && baggageItems.contains("Bibbles : Bobbles"));
baggageItems = ejb.editBaggageItems();
Assert.assertTrue("Baggage items didn't match, received: " + baggageItems, baggageItems.contains("Wibbles : Wabbles") && baggageItems.contains("Nibbles : Nabbles") && baggageItems.contains("Bibbles : Babbles"));
} finally {
if (span != null) {
span.finish();
}
}
}
use of io.opentracing.Span in project Payara by payara.
the class Ejb method editBaggageItems.
@Override
public String editBaggageItems() {
randomSleep();
Span activeSpan = tracer.activeSpan();
if (activeSpan != null) {
activeSpan.setBaggageItem("Wibbles", "Wabbles");
activeSpan.setBaggageItem("Nibbles", "Nabbles");
activeSpan.setBaggageItem("Bibbles", "Babbles");
return getBaggageItems(activeSpan);
} else {
return "Nothing found!";
}
}
Aggregations