Search in sources :

Example 1 with RequestTracingService

use of fish.payara.nucleus.requesttracing.RequestTracingService in project Payara by payara.

the class Span method finish.

@Override
public void finish(long finishMicros) {
    // Convert the timestamp to that required by the Request Tracing Service
    long finishMillis = convertTimestampMicrosToTimestampMillis(finishMicros);
    // Pass through to the Request Tracing Service
    RequestTracingService requestTracingService = getRequestTracingService();
    if (requestTracingService != null) {
        requestTracingService.traceSpan(this, finishMillis);
    }
}
Also used : RequestTracingService(fish.payara.nucleus.requesttracing.RequestTracingService)

Example 2 with RequestTracingService

use of fish.payara.nucleus.requesttracing.RequestTracingService in project Payara by payara.

the class InvocationContext method saveTracingContext.

private void saveTracingContext() {
    ServiceLocator serviceLocator = Globals.getDefaultBaseServiceLocator();
    if (serviceLocator != null) {
        RequestTracingService requestTracing = serviceLocator.getService(RequestTracingService.class);
        OpenTracingService openTracing = serviceLocator.getService(OpenTracingService.class);
        // Check that there's actually a trace running
        if (requestTracing != null && requestTracing.isRequestTracingEnabled() && requestTracing.isTraceInProgress() && openTracing != null) {
            Tracer tracer = openTracing.getTracer(openTracing.getApplicationName(serviceLocator.getService(InvocationManager.class)));
            SpanContext spanContext = null;
            // Check if there's an active Span running
            Span activeSpan = tracer.activeSpan();
            if (activeSpan != null) {
                // The traceId is likely incorrect at this point as it initialises as a random UUID
                try {
                    ((RequestTraceSpan) activeSpan).setTraceId(requestTracing.getConversationID());
                } catch (ClassCastException cce) {
                    Logger.getLogger(InvocationContext.class).log(Level.FINE, "ClassCastException caught converting Span", cce);
                }
                spanContext = activeSpan.context();
            } else {
                // Create a new span context using the starting span as a parent - the request tracing service doesn't
                // know about unfinished spans so we can't get the actual parent with the current impl
                spanContext = new RequestTraceSpanContext(requestTracing.getConversationID(), requestTracing.getStartingTraceID());
            }
            // Check to see if we're using the mock tracer to prevent ClassCastExceptions
            try {
                tracer.inject(spanContext, Format.Builtin.TEXT_MAP, new MapToTextMap(spanContextMap = new HashMap()));
            } catch (ClassCastException cce) {
                Logger.getLogger(InvocationContext.class).log(Level.FINE, "ClassCastException caught injecting SpanContext", cce);
            }
        }
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) SpanContext(io.opentracing.SpanContext) HashMap(java.util.HashMap) Tracer(io.opentracing.Tracer) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) OpenTracingService(fish.payara.opentracing.OpenTracingService) MapToTextMap(fish.payara.opentracing.propagation.MapToTextMap) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) Span(io.opentracing.Span) RequestTracingService(fish.payara.nucleus.requesttracing.RequestTracingService)

Example 3 with RequestTracingService

use of fish.payara.nucleus.requesttracing.RequestTracingService 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()));
        }
    }
}
Also used : SpanBuilder(io.opentracing.Tracer.SpanBuilder) SpanContext(io.opentracing.SpanContext) Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) Span(io.opentracing.Span) RequestTracingService(fish.payara.nucleus.requesttracing.RequestTracingService)

Example 4 with RequestTracingService

use of fish.payara.nucleus.requesttracing.RequestTracingService in project Payara by payara.

the class RequestTracingCdiInterceptor method traceCdiCall.

@AroundInvoke
public Object traceCdiCall(InvocationContext ctx) throws Exception {
    RequestTracingService requestTracing = Globals.getDefaultHabitat().getService(RequestTracingService.class);
    Object proceed = null;
    if (requestTracing != null && requestTracing.isRequestTracingEnabled()) {
        RequestTraceSpan span = new RequestTraceSpan("executeCdiMethod");
        span.addSpanTag("TargetClass", ctx.getTarget().getClass().getName());
        span.addSpanTag("MethodName", ctx.getMethod().getName());
        proceed = ctx.proceed();
        requestTracing.traceSpan(span);
    } else {
        proceed = ctx.proceed();
    }
    return proceed;
}
Also used : RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) RequestTracingService(fish.payara.nucleus.requesttracing.RequestTracingService) AroundInvoke(javax.interceptor.AroundInvoke)

Example 5 with RequestTracingService

use of fish.payara.nucleus.requesttracing.RequestTracingService in project Payara by payara.

the class Span method getRequestTracingService.

/**
 * Helper method that gets the Request Tracing Service if this instance does not have it.
 */
private RequestTracingService getRequestTracingService() {
    RequestTracingService requestTracingService = null;
    ServiceLocator serviceLocator = Globals.getDefaultBaseServiceLocator();
    if (serviceLocator != null) {
        ServiceHandle<RequestTracingService> serviceHandle = serviceLocator.getServiceHandle(RequestTracingService.class);
        if (serviceHandle != null && serviceHandle.isActive()) {
            requestTracingService = serviceHandle.getService();
        }
    }
    return requestTracingService;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) RequestTracingService(fish.payara.nucleus.requesttracing.RequestTracingService)

Aggregations

RequestTracingService (fish.payara.nucleus.requesttracing.RequestTracingService)7 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)4 Span (io.opentracing.Span)3 Scope (io.opentracing.Scope)2 SpanContext (io.opentracing.SpanContext)2 Tracer (io.opentracing.Tracer)2 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)2 RequestTraceSpanContext (fish.payara.notification.requesttracing.RequestTraceSpanContext)1 OpenTracingService (fish.payara.opentracing.OpenTracingService)1 MapToTextMap (fish.payara.opentracing.propagation.MapToTextMap)1 SpanBuilder (io.opentracing.Tracer.SpanBuilder)1 HashMap (java.util.HashMap)1 PostConstruct (javax.annotation.PostConstruct)1 AroundInvoke (javax.interceptor.AroundInvoke)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 StatusType (javax.ws.rs.core.Response.StatusType)1 InvocationManager (org.glassfish.api.invocation.InvocationManager)1