use of io.opentracing.Scope in project java-spring-web by opentracing-contrib.
the class TracingHandlerInterceptor method afterConcurrentHandlingStarted.
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
if (!isTraced(httpServletRequest)) {
return;
}
Deque<Scope> activeSpanStack = getScopeStack(httpServletRequest);
Scope scope = activeSpanStack.pop();
for (HandlerInterceptorSpanDecorator decorator : decorators) {
decorator.onAfterConcurrentHandlingStarted(httpServletRequest, httpServletResponse, handler, scope.span());
}
httpServletRequest.setAttribute(CONTINUATION_FROM_ASYNC_STARTED, scope.span());
scope.close();
}
use of io.opentracing.Scope in project wildfly by wildfly.
the class SimpleRestClientTestCase method clientRequestSpanJoinsServer.
@Test
public void clientRequestSpanJoinsServer() {
// sanity checks
Assert.assertNotNull(tracer);
Assert.assertTrue(tracer instanceof MockTracer);
// the first span
try (Scope ignored = tracer.activateSpan(tracer.buildSpan("existing-span").start())) {
// the second span is the client request, as a child of `existing-span`
Client restClient = ClientTracingRegistrar.configure(ClientBuilder.newBuilder()).build();
// the third span is the traced endpoint, child of the client request
String targetUrl = url.toString() + "opentracing/traced";
System.out.println("We are trying to open " + targetUrl);
WebTarget target = restClient.target(targetUrl);
try (Response response = target.request().get()) {
// just a sanity check
Assert.assertEquals(200, response.getStatus());
}
tracer.activeSpan().finish();
}
// verify
MockTracer mockTracer = (MockTracer) tracer;
List<MockSpan> spans = mockTracer.finishedSpans();
Assert.assertEquals(3, spans.size());
long traceId = spans.get(0).context().traceId();
for (MockSpan span : spans) {
// they should all belong to the same trace
Assert.assertEquals(traceId, span.context().traceId());
}
}
use of io.opentracing.Scope in project Payara by payara.
the class JaxrsClientRequestTracingFilter method filter.
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
final PayaraTracingServices payaraTracingServices = new PayaraTracingServices();
final RequestTracingService requestTracing = payaraTracingServices.getRequestTracingService();
if (requestTracing != null && requestTracing.isRequestTracingEnabled()) {
// If there is a trace in progress, add the propagation headers with the relevant details
if (requestTracing.isTraceInProgress()) {
// Check that we aren't overwriting a header
if (!requestContext.getHeaders().containsKey(PropagationHeaders.PROPAGATED_TRACE_ID)) {
requestContext.getHeaders().add(PropagationHeaders.PROPAGATED_TRACE_ID, requestTracing.getConversationID());
}
// Check that we aren't overwriting a header
if (!requestContext.getHeaders().containsKey(PropagationHeaders.PROPAGATED_PARENT_ID)) {
requestContext.getHeaders().add(PropagationHeaders.PROPAGATED_PARENT_ID, requestTracing.getStartingTraceID());
}
// Check that we aren't overwriting a relationship type
if (!requestContext.getHeaders().containsKey(PropagationHeaders.PROPAGATED_RELATIONSHIP_TYPE)) {
if (requestContext.getMethod().equals("POST")) {
requestContext.getHeaders().add(PropagationHeaders.PROPAGATED_RELATIONSHIP_TYPE, RequestTraceSpan.SpanContextRelationshipType.FollowsFrom);
} else {
requestContext.getHeaders().add(PropagationHeaders.PROPAGATED_RELATIONSHIP_TYPE, RequestTraceSpan.SpanContextRelationshipType.ChildOf);
}
}
}
// Check if we should trace this client call
if (shouldTrace(requestContext)) {
// Get or create the tracer instance for this application
final Tracer tracer = payaraTracingServices.getActiveTracer();
// Build a span with the required MicroProfile Opentracing tags
SpanBuilder spanBuilder = tracer.buildSpan(requestContext.getMethod()).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).withTag(Tags.HTTP_METHOD.getKey(), requestContext.getMethod()).withTag(Tags.HTTP_URL.getKey(), requestContext.getUri().toURL().toString()).withTag(Tags.COMPONENT.getKey(), "jaxrs");
// Get the propagated span context from the request if present
// This is required to account for asynchronous client requests
SpanContext parentSpanContext = (SpanContext) requestContext.getProperty(PropagationHeaders.OPENTRACING_PROPAGATED_SPANCONTEXT);
if (parentSpanContext != null) {
spanBuilder.asChildOf(parentSpanContext);
} else {
parentSpanContext = SpanPropagator.propagatedContext();
if (parentSpanContext != null) {
spanBuilder.asChildOf(parentSpanContext);
}
}
// If there is a propagated span context, set it as a parent of the new span
parentSpanContext = tracer.extract(Format.Builtin.HTTP_HEADERS, new MultivaluedMapToTextMap(requestContext.getHeaders()));
if (parentSpanContext != null) {
spanBuilder.asChildOf(parentSpanContext);
}
// Start the span and mark it as active
Span span = spanBuilder.start();
Scope scope = tracer.activateSpan(span);
requestContext.setProperty(Scope.class.getName(), scope);
// Inject the active span context for propagation
tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new MultivaluedMapToTextMap(requestContext.getHeaders()));
}
}
}
use of io.opentracing.Scope in project Payara by payara.
the class JaxwsContainerRequestTracingFilter method filterResponse.
// After method invocation
@Override
public void filterResponse(Packet pipeRequest, Packet pipeResponse, MonitorContext monitorContext) {
// Try block so that we can always attach error info if there is any
try {
// If request tracing is enabled, and there is 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)) {
Span activeSpan = getTracer().scopeManager().activeSpan();
if (activeSpan == null) {
return;
}
HttpServletRequest httpRequest = (HttpServletRequest) pipeRequest.get(SERVLET_REQUEST);
try {
// Get and add the response status to the active span
Response.StatusType statusInfo = getResponseStatus(pipeRequest, pipeRequest);
activeSpan.setTag(HTTP_STATUS.getKey(), statusInfo.getStatusCode());
// If the response status is an error, add error information to the span
if (statusInfo.getFamily() == CLIENT_ERROR || statusInfo.getFamily() == SERVER_ERROR) {
activeSpan.setTag(ERROR.getKey(), true);
activeSpan.log(singletonMap("event", "error"));
// If there's an attached exception, add it to the span
Message message = pipeResponse.getMessage();
if (message != null && message.isFault()) {
activeSpan.log(singletonMap("error.object", getErrorObject(message)));
}
}
} finally {
activeSpan.finish();
Object scopeObj = httpRequest.getAttribute(Scope.class.getName());
if (scopeObj != null && scopeObj instanceof Scope) {
try (Scope scope = (Scope) scopeObj) {
httpRequest.removeAttribute(Scope.class.getName());
}
}
}
}
}
} finally {
// If there's an attached error on the response, log the exception
Message message = pipeResponse.getMessage();
if (message != null && message.isFault()) {
Object errorObject = getErrorObject(message);
if (errorObject != null) {
// TODO: have an actual detail formatter for fault
logger.log(SEVERE, getErrorObject(message).toString());
}
}
}
}
use of io.opentracing.Scope in project Payara by payara.
the class OpenTracingRequestEventListener method finish.
private void finish(final RequestEvent event) {
LOG.fine(() -> "finish(event=" + event.getType() + ")");
ScopeManager scopeManager = openTracing.getTracer(this.applicationName).scopeManager();
final Span activeSpan = scopeManager.activeSpan();
if (activeSpan == null) {
LOG.finest("Active span is null, nothing to do.");
return;
}
scopeManager.activeSpan().finish();
Object scopeObj = event.getContainerRequest().getProperty(Scope.class.getName());
if (scopeObj != null && scopeObj instanceof Scope) {
try (Scope scope = (Scope) scopeObj) {
event.getContainerRequest().removeProperty(Scope.class.getName());
}
}
LOG.finest("Finished.");
}
Aggregations