Search in sources :

Example 61 with Tracer

use of io.opentracing.Tracer 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 62 with Tracer

use of io.opentracing.Tracer 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 63 with Tracer

use of io.opentracing.Tracer in project Payara by payara.

the class OpenTracingRequestEventListener method onIncomingRequest.

private void onIncomingRequest(final RequestEvent event, final String operationName) {
    LOG.fine(() -> "onIncomingRequest(event=" + event.getType() + ", operationName=" + operationName + ")");
    final ContainerRequest requestContext = event.getContainerRequest();
    final Tracer tracer = openTracing.getTracer(this.applicationName);
    // Create a Span and instrument it with details about the request
    final SpanBuilder spanBuilder = // 
    tracer.buildSpan(operationName).withTag(Tags.SPAN_KIND.getKey(), // 
    Tags.SPAN_KIND_SERVER).withTag(Tags.HTTP_METHOD.getKey(), // 
    requestContext.getMethod()).withTag(Tags.HTTP_URL.getKey(), // 
    toString(requestContext.getUriInfo())).withTag(Tags.COMPONENT.getKey(), "jaxrs");
    // If there was a context injected into the tracer, add it as a parent of the new span
    final SpanContext spanContext = findSpanContext(requestContext, tracer);
    if (spanContext != null) {
        spanBuilder.asChildOf(spanContext);
    }
    // Start the span and continue on to the targeted method
    Scope scope = tracer.activateSpan(spanBuilder.start());
    // tracer.activeSpan();
    requestContext.setProperty(Scope.class.getName(), scope);
    LOG.fine(() -> "Request tracing enabled for request=" + requestContext.getRequest() + " to application=" + this.applicationName + " on uri=" + toString(requestContext.getUriInfo()));
}
Also used : SpanBuilder(io.opentracing.Tracer.SpanBuilder) SpanContext(io.opentracing.SpanContext) Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer) ContainerRequest(org.glassfish.jersey.server.ContainerRequest)

Example 64 with Tracer

use of io.opentracing.Tracer in project Payara by payara.

the class RemoteEjbClientIT method transactionIdAddedAsBaggageIT.

@Test
public void transactionIdAddedAsBaggageIT() 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);
        String baggageItems = ejb.annotatedMethod();
        Assert.assertTrue("Baggage items didn't contain transaction ID, received: " + baggageItems, baggageItems.contains("TX-ID"));
    } finally {
        if (span != null) {
            span.finish();
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) Properties(java.util.Properties) Span(io.opentracing.Span) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 65 with Tracer

use of io.opentracing.Tracer in project Payara by payara.

the class StandardWrapper method service.

// START IASRI 4665318
/**
 * Wrapper for the service method on the actual servlet
 * @param request The request sent
 * @param response
 * @param servlet The servlet to process
 * @throws IOException
 * @throws ServletException
 * @see Servlet#service(ServletRequest, ServletResponse)
 */
void service(ServletRequest request, ServletResponse response, Servlet servlet) throws IOException, ServletException {
    InstanceSupport supp = getInstanceSupport();
    try {
        supp.fireInstanceEvent(BEFORE_SERVICE_EVENT, servlet, request, response);
        if (!isAsyncSupported()) {
            RequestFacadeHelper reqFacHelper = RequestFacadeHelper.getInstance(request);
            if (reqFacHelper != null) {
                reqFacHelper.disableAsyncSupport();
            }
        }
        if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
            if (SecurityUtil.executeUnderSubjectDoAs()) {
                final ServletRequest req = request;
                final ServletResponse res = response;
                Principal principal = ((HttpServletRequest) req).getUserPrincipal();
                Object[] serviceType = new Object[2];
                serviceType[0] = req;
                serviceType[1] = res;
                SecurityUtil.doAsPrivilege("service", servlet, classTypeUsedInService, serviceType, principal);
            } else {
                RequestTraceSpan span = null;
                if (requestTracing.isRequestTracingEnabled()) {
                    if (servlet instanceof ServletContainer) {
                        span = constructWebServiceRequestSpan((HttpServletRequest) request);
                    } else if (servlet instanceof Servlet) {
                        span = constructServletRequestSpan((HttpServletRequest) request, servlet);
                    }
                }
                try {
                    if (isJspServlet) {
                        isInSuppressFFNFThread.set(true);
                    }
                    servlet.service((HttpServletRequest) request, (HttpServletResponse) response);
                } finally {
                    String applicationName = openTracing.getApplicationName(Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class));
                    Tracer tracer = openTracing.getTracer(applicationName);
                    if (tracer != null && tracer.activeSpan() != null) {
                        // Presumably held open by return being handled by another thread
                        tracer.activeSpan().setTag(Tags.HTTP_STATUS.getKey(), Integer.toString(((HttpServletResponse) response).getStatus()));
                        tracer.activeSpan().finish();
                    }
                    if (requestTracing.isRequestTracingEnabled() && span != null) {
                        span.addSpanTag("ResponseStatus", Integer.toString(((HttpServletResponse) response).getStatus()));
                        requestTracing.traceSpan(span);
                    }
                    isInSuppressFFNFThread.set(false);
                }
            }
        } else {
            servlet.service(request, response);
        }
        supp.fireInstanceEvent(AFTER_SERVICE_EVENT, servlet, request, response);
    } catch (IOException | ServletException | RuntimeException | Error e) {
        // Set response status before firing event, see IT 10022
        if (response instanceof HttpServletResponse) {
            ((HttpServletResponse) response).setStatus(SC_INTERNAL_SERVER_ERROR);
        }
        supp.fireInstanceEvent(AFTER_SERVICE_EVENT, servlet, request, response, e);
        throw e;
    } catch (Throwable e) {
        // Set response status before firing event, see IT 10022
        ((HttpServletResponse) response).setStatus(SC_INTERNAL_SERVER_ERROR);
        supp.fireInstanceEvent(AFTER_SERVICE_EVENT, servlet, request, response, e);
        throw new ServletException(rb.getString(SERVLET_EXECUTION_EXCEPTION), e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Tracer(io.opentracing.Tracer) InvocationManager(org.glassfish.api.invocation.InvocationManager) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) ContainerServlet(org.apache.catalina.ContainerServlet) InstanceSupport(org.apache.catalina.util.InstanceSupport) Principal(java.security.Principal)

Aggregations

Tracer (io.opentracing.Tracer)104 Span (io.opentracing.Span)49 SpanContext (io.opentracing.SpanContext)30 Map (java.util.Map)21 Vertx (io.vertx.core.Vertx)19 HashMap (java.util.HashMap)19 Test (org.junit.Test)19 BeforeEach (org.junit.jupiter.api.BeforeEach)19 Test (org.junit.jupiter.api.Test)19 Future (io.vertx.core.Future)18 Buffer (io.vertx.core.buffer.Buffer)16 HttpURLConnection (java.net.HttpURLConnection)14 EventBus (io.vertx.core.eventbus.EventBus)13 JsonObject (io.vertx.core.json.JsonObject)12 Objects (java.util.Objects)12 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 Tags (io.opentracing.tag.Tags)9 Scope (io.opentracing.Scope)8 GlobalTracer (io.opentracing.util.GlobalTracer)8