Search in sources :

Example 6 with Traced

use of org.eclipse.microprofile.opentracing.Traced 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);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SpanBuilder(io.opentracing.Tracer.SpanBuilder) SpanContext(io.opentracing.SpanContext) Traced(org.eclipse.microprofile.opentracing.Traced) Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer)

Example 7 with Traced

use of org.eclipse.microprofile.opentracing.Traced in project Payara by payara.

the class JaxwsContainerRequestTracingFilter method determineOperationName.

/**
 * Helper method that determines what the operation name of the span.
 *
 * @param tracedAnnotation The Traced annotation obtained from the target method
 * @return The name to use as the Span's operation name
 */
private String determineOperationName(Packet pipeRequest, MonitorContext monitorContext, Traced tracedAnnotation) {
    HttpServletRequest httpRequest = (HttpServletRequest) pipeRequest.get(SERVLET_REQUEST);
    if (tracedAnnotation != null) {
        String operationName = OpenTracingJaxwsCdiUtils.getConfigOverrideValue(Traced.class, "operationName", monitorContext, String.class).orElse(tracedAnnotation.operationName());
        // followed by the method signature
        if (operationName.equals("")) {
            operationName = createFallbackName(httpRequest, monitorContext);
        }
        return operationName;
    }
    // If there is no @Traced annotation
    Config config = getConfig();
    // Determine if an operation name provider has been given
    Optional<String> operationNameProviderOptional = config.getOptionalValue("mp.opentracing.server.operation-name-provider", String.class);
    if (operationNameProviderOptional.isPresent()) {
        String operationNameProvider = operationNameProviderOptional.get();
        // TODO: Take webservices.xml into account
        WebService classLevelAnnotation = monitorContext.getImplementationClass().getAnnotation(WebService.class);
        WebMethod methodLevelAnnotation = monitorContext.getCallInfo().getMethod().getAnnotation(WebMethod.class);
        // If the provider is set to "http-path" and the class-level @WebService annotation is actually present
        if (operationNameProvider.equals("http-path") && classLevelAnnotation != null) {
            String operationName = httpRequest.getMethod() + ":";
            operationName += "/" + classLevelAnnotation.name();
            // If the method-level WebMethod annotation is present, use its value
            if (methodLevelAnnotation != null) {
                operationName += "/" + methodLevelAnnotation.operationName();
            }
            return operationName;
        }
    }
    // If we haven't returned by now, just go with the default ("class-method")
    return createFallbackName(httpRequest, monitorContext);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebMethod(javax.jws.WebMethod) Traced(org.eclipse.microprofile.opentracing.Traced) WebService(javax.jws.WebService) Config(org.eclipse.microprofile.config.Config)

Example 8 with Traced

use of org.eclipse.microprofile.opentracing.Traced 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();
    }
}
Also used : Traced(org.eclipse.microprofile.opentracing.Traced) Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer) InvocationManager(org.glassfish.api.invocation.InvocationManager) OpenTracingService(fish.payara.opentracing.OpenTracingService) Span(io.opentracing.Span) HashMap(java.util.HashMap) Map(java.util.Map) PayaraTracingServices(fish.payara.requesttracing.jaxrs.client.PayaraTracingServices) AroundInvoke(javax.interceptor.AroundInvoke)

Example 9 with Traced

use of org.eclipse.microprofile.opentracing.Traced in project Payara by payara.

the class OpenTracingRequestEventListener method onEvent.

@Override
public void onEvent(final RequestEvent event) {
    LOG.fine(() -> "onEvent(event.type=" + event.getType() + ", path=" + getPath(event) + ")");
    // early phases are simply ignored.
    if (Arrays.asList(Type.START, Type.MATCHING_START).contains(event.getType())) {
        return;
    }
    try {
        if (event.getType() == Type.REQUEST_MATCHED) {
            final Traced tracedAnnotation = getTracedAnnotation();
            final ContainerRequest requestContext = event.getContainerRequest();
            if (!canTrace(requestContext, tracedAnnotation)) {
                LOG.finest(() -> "canTrace(...) returned false, nothing to do.");
                return;
            }
            final String operationName = determineOperationName(requestContext, tracedAnnotation);
            onIncomingRequest(event, operationName);
            return;
        }
        final Span activeSpan = openTracing.getTracer(this.applicationName).scopeManager().activeSpan();
        if (activeSpan == null) {
            LOG.finest(() -> "Could not find any active span, nothing to do.");
            return;
        }
        if (event.getType() == Type.ON_EXCEPTION) {
            onException(event);
        } else if (event.getType() == Type.RESP_FILTERS_FINISHED) {
            onOutgoingResponse(event);
        } else if (event.getType() == Type.FINISHED) {
            finish(event);
        }
    } catch (final RuntimeException e) {
        LOG.log(Level.CONFIG, "Exception thrown by the listener!", e);
        throw e;
    }
}
Also used : Traced(org.eclipse.microprofile.opentracing.Traced) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) Span(io.opentracing.Span)

Aggregations

Traced (org.eclipse.microprofile.opentracing.Traced)9 Span (io.opentracing.Span)6 Scope (io.opentracing.Scope)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Tracer (io.opentracing.Tracer)2 Config (org.eclipse.microprofile.config.Config)2 Message (com.sun.xml.ws.api.message.Message)1 OpenTracingService (fish.payara.opentracing.OpenTracingService)1 PayaraTracingServices (fish.payara.requesttracing.jaxrs.client.PayaraTracingServices)1 SpanContext (io.opentracing.SpanContext)1 SpanBuilder (io.opentracing.Tracer.SpanBuilder)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AroundInvoke (javax.interceptor.AroundInvoke)1 WebMethod (javax.jws.WebMethod)1 WebService (javax.jws.WebService)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Path (javax.ws.rs.Path)1 Response (javax.ws.rs.core.Response)1 InvocationManager (org.glassfish.api.invocation.InvocationManager)1