Search in sources :

Example 1 with OpenTracingService

use of fish.payara.opentracing.OpenTracingService in project Payara by payara.

the class JavaEETransactionManagerSimplified method commit.

@Override
public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
    boolean acquiredlock = false;
    try {
        JavaEETransaction tx = transactions.get();
        if (tx != null && tx.isLocalTx()) {
            if (monitoringEnabled) {
                // XXX acquireReadLock();
                getDelegate().getReadLock().lock();
                acquiredlock = true;
            }
            // commit local tx
            tx.commit();
        } else {
            try {
                // an XA transaction
                getDelegate().commitDistributedTransaction();
            } finally {
                if (tx != null) {
                    ((JavaEETransactionImpl) tx).onTxCompletion(true);
                }
            }
        }
        if (openTracingServiceProvider != null) {
            OpenTracingService openTracingService = getOpenTracing();
            if (openTracingService != null && openTracingService.isEnabled()) {
                addJtaEventTraceLog(constructJTAEndSpanLog(tx), tx, openTracingService);
            }
        }
    } finally {
        // clear current thread's tx
        setCurrentTransaction(null);
        delegates.set(null);
        if (acquiredlock) {
            // XXX releaseReadLock();
            getDelegate().getReadLock().unlock();
        }
    }
// END IASRI 4662745
}
Also used : JavaEETransaction(com.sun.enterprise.transaction.api.JavaEETransaction) OpenTracingService(fish.payara.opentracing.OpenTracingService)

Example 2 with OpenTracingService

use of fish.payara.opentracing.OpenTracingService 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 OpenTracingService

use of fish.payara.opentracing.OpenTracingService 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 4 with OpenTracingService

use of fish.payara.opentracing.OpenTracingService in project Payara by payara.

the class JavaEETransactionManagerSimplified method initJavaEETransaction.

private JavaEETransactionImpl initJavaEETransaction(int timeout) {
    JavaEETransactionImpl tx;
    // Do not need to use injection.
    if (timeout > 0) {
        tx = new JavaEETransactionImpl(timeout, this);
        ScheduledFuture<?> scheduledFuture = scheduledTransactionManagerExecutor.schedule(tx, timeout, TimeUnit.SECONDS);
        tx.setScheduledTimeoutFuture(scheduledFuture);
        scheduledTransactionTimeouts.incrementAndGet();
    } else {
        tx = new JavaEETransactionImpl(this);
    }
    setCurrentTransaction(tx);
    if (openTracingServiceProvider != null) {
        OpenTracingService openTracingService = getOpenTracing();
        if (openTracingService != null && openTracingService.isEnabled()) {
            addJtaEventTraceLog(constructJTABeginSpanLog(tx), tx, openTracingService);
        }
    }
    return tx;
}
Also used : OpenTracingService(fish.payara.opentracing.OpenTracingService)

Example 5 with OpenTracingService

use of fish.payara.opentracing.OpenTracingService in project Payara by payara.

the class JavaEETransactionManagerSimplified method rollback.

@Override
public void rollback() throws IllegalStateException, SecurityException, SystemException {
    boolean acquiredlock = false;
    try {
        JavaEETransaction tx = transactions.get();
        if (tx != null && tx.isLocalTx()) {
            if (monitoringEnabled) {
                // XXX acquireReadLock();
                getDelegate().getReadLock().lock();
                acquiredlock = true;
            }
            // rollback local tx
            tx.rollback();
        } else {
            try {
                // an XA transaction
                getDelegate().rollbackDistributedTransaction();
            } finally {
                if (tx != null) {
                    ((JavaEETransactionImpl) tx).onTxCompletion(false);
                }
            }
        }
        if (openTracingServiceProvider != null) {
            OpenTracingService openTracingService = getOpenTracing();
            if (openTracingService != null && openTracingService.isEnabled()) {
                addJtaEventTraceLog(constructJTAEndSpanLog(tx), tx, openTracingService);
            }
        }
    } finally {
        // clear current thread's tx
        setCurrentTransaction(null);
        delegates.set(null);
        if (acquiredlock) {
            // XXX releaseReadLock();
            getDelegate().getReadLock().unlock();
        }
    }
}
Also used : JavaEETransaction(com.sun.enterprise.transaction.api.JavaEETransaction) OpenTracingService(fish.payara.opentracing.OpenTracingService)

Aggregations

OpenTracingService (fish.payara.opentracing.OpenTracingService)5 JavaEETransaction (com.sun.enterprise.transaction.api.JavaEETransaction)2 Span (io.opentracing.Span)2 Tracer (io.opentracing.Tracer)2 HashMap (java.util.HashMap)2 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)1 RequestTraceSpanContext (fish.payara.notification.requesttracing.RequestTraceSpanContext)1 RequestTracingService (fish.payara.nucleus.requesttracing.RequestTracingService)1 MapToTextMap (fish.payara.opentracing.propagation.MapToTextMap)1 PayaraTracingServices (fish.payara.requesttracing.jaxrs.client.PayaraTracingServices)1 Scope (io.opentracing.Scope)1 SpanContext (io.opentracing.SpanContext)1 Map (java.util.Map)1 AroundInvoke (javax.interceptor.AroundInvoke)1 Traced (org.eclipse.microprofile.opentracing.Traced)1 InvocationManager (org.glassfish.api.invocation.InvocationManager)1 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)1