use of io.opentracing.Scope in project cxf by apache.
the class AbstractOpenTracingProvider method stopTraceSpan.
protected void stopTraceSpan(final Map<String, List<String>> requestHeaders, final Map<String, List<Object>> responseHeaders, final int responseStatus, final TraceScopeHolder<TraceScope> holder) {
if (holder == null) {
return;
}
final TraceScope traceScope = holder.getScope();
if (traceScope != null) {
Span span = traceScope.getSpan();
Scope scope = traceScope.getScope();
// one.
if (holder.isDetached()) {
scope = tracer.scopeManager().activate(span);
}
span.setTag(Tags.HTTP_STATUS.getKey(), responseStatus);
span.setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER);
span.finish();
scope.close();
}
}
use of io.opentracing.Scope in project cxf by apache.
the class OpenTracingContext method wrap.
@Override
public <T> Callable<T> wrap(final String description, final Traceable<T> traceable) {
final Callable<T> callable = new Callable<T>() {
@Override
public T call() throws Exception {
return traceable.call(new OpenTracingContext(tracer));
}
};
// Carry over parent from the current thread
final Span parent = tracer.activeSpan();
return () -> {
try (Scope scope = newOrChildSpan(description, parent)) {
return callable.call();
}
};
}
use of io.opentracing.Scope in project cxf by apache.
the class OpenTracingTracingTest method testThatProvidedSpanIsNotDetachedWhenActiveUsingAsyncClient.
@Test
public void testThatProvidedSpanIsNotDetachedWhenActiveUsingAsyncClient() throws Exception {
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.async().get().get(1L, TimeUnit.MINUTES);
assertEquals(Status.OK.getStatusCode(), r.getStatus());
assertThat(tracer.activeSpan().context(), equalTo(span.context()));
await().atMost(Duration.ofSeconds(1L)).until(() -> REPORTER.getSpans().size() == 3);
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("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 Payara by payara.
the class JaxwsContainerRequestTracingFilter method filterRequest.
// Before method invocation
@Override
public void filterRequest(Packet pipeRequest, MonitorContext monitorContext) {
// If request tracing is enabled, and there's a trace in progress (which there should be!)
if (isTraceInProgress()) {
// Get the Traced annotation from the target method if CDI is initialised
Traced tracedAnnotation = getTraceAnnotation(monitorContext);
// pattern and a traced annotation set to true (via annotation or config override)
if (shouldTrace(pipeRequest) && shouldTrace(monitorContext, tracedAnnotation)) {
// Get the application's tracer instance
Tracer tracer = getTracer();
HttpServletRequest httpRequest = (HttpServletRequest) pipeRequest.get(SERVLET_REQUEST);
// Create a Span and instrument it with details about the request
SpanBuilder spanBuilder = tracer.buildSpan(determineOperationName(pipeRequest, monitorContext, tracedAnnotation)).withTag(SPAN_KIND.getKey(), SPAN_KIND_SERVER).withTag(HTTP_METHOD.getKey(), httpRequest.getMethod()).withTag(HTTP_URL.getKey(), httpRequest.getRequestURL().toString()).withTag(COMPONENT.getKey(), "jaxws");
SpanContext spanContext = null;
try {
// Extract the context from the tracer if there is one
spanContext = tracer.extract(HTTP_HEADERS, new MultivaluedMapToTextMap(getHeaders(httpRequest)));
} catch (IllegalArgumentException e) {
logger.log(WARNING, e.getMessage());
}
// If there was a context injected into the tracer, add it as a parent of the new span
if (spanContext != null) {
spanBuilder.asChildOf(spanContext);
}
// Start the span and continue on to the targeted method
Scope scope = tracer.activateSpan(spanBuilder.start());
httpRequest.setAttribute(Scope.class.getName(), scope);
}
}
}
use of io.opentracing.Scope in project Payara by payara.
the class TracedInterceptor method traceCdiCall.
/**
* If the tracing is enabled and possible for the invoked method, traces it's execution.
* If not, only executes the invocation context.
*
* @param invocationContext the context to be executed
* @return result of the invocation context execution.
* @throws Exception any execution thrown from the invocation context execution.
*/
@AroundInvoke
public Object traceCdiCall(final InvocationContext invocationContext) throws Exception {
LOG.fine(() -> "traceCdiCall(" + invocationContext + ")");
// Get the required HK2 services
final PayaraTracingServices payaraTracingServices = new PayaraTracingServices();
final OpenTracingService openTracing = payaraTracingServices.getOpenTracingService();
final InvocationManager invocationManager = payaraTracingServices.getInvocationManager();
// If Request Tracing is enabled, and this isn't a JaxRs method
if (//
openTracing == null || !openTracing.isEnabled() || isJaxRsMethod(invocationContext) || isWebServiceMethod(invocationContext, invocationManager)) {
// If request tracing was turned off, or this is a JaxRs method, just carry on
LOG.finest("The call is already monitored by some different component, proceeding the invocation.");
return invocationContext.proceed();
}
// Get the Traced annotation present on the method or class
final Traced traced = getAnnotation(beanManager, Traced.class, invocationContext);
// Get the enabled (value) variable from a config override, or from the annotation if there is no override
final boolean tracingEnabled = OpenTracingCdiUtils.getConfigOverrideValue(Traced.class, "value", invocationContext, boolean.class).orElse(traced.value());
// If we've explicitly been told not to trace the method: don't!
if (!tracingEnabled) {
LOG.finest("Tracing is not enabled, nothing to do.");
return invocationContext.proceed();
}
// If we *have* been told to, get the application's Tracer instance and start an active span.
final String applicationName = openTracing.getApplicationName(invocationManager, invocationContext);
final Tracer tracer = openTracing.getTracer(applicationName);
final String operationName = getOperationName(invocationContext, traced);
Span parentSpan = tracer.activeSpan();
final Span span = tracer.buildSpan(operationName).start();
try (Scope scope = tracer.scopeManager().activate(span)) {
try {
return invocationContext.proceed();
} catch (final Exception ex) {
LOG.log(Level.FINEST, "Setting the error to the active span ...", ex);
span.setTag(Tags.ERROR.getKey(), true);
final Map<String, Object> errorInfoMap = new HashMap<>();
errorInfoMap.put(Fields.EVENT, "error");
errorInfoMap.put(Fields.ERROR_OBJECT, ex.getClass().getName());
span.log(errorInfoMap);
throw ex;
}
} finally {
span.finish();
}
}
Aggregations