Search in sources :

Example 1 with BeforeOperationObjectEvent

use of kieker.common.record.flow.trace.operation.object.BeforeOperationObjectEvent in project iobserve-analysis by research-iobserve.

the class TraceObjectInterceptor method interceptMethodCall.

/**
 * Around advice configuration.
 *
 * @param context
 *            the invocation context of a bean call.
 * @return the return value of the next method in the chain
 * @throws Throwable
 */
@AroundInvoke
public Object interceptMethodCall(final InvocationContext context) throws Throwable {
    // (IllegalThrowsCheck)
    if (this.monitoringCtrl.isMonitoringEnabled()) {
        final String signature = context.getMethod().toString();
        if (this.monitoringCtrl.isProbeActivated(signature)) {
            // common fields
            TraceMetadata trace = TraceObjectInterceptor.TRACEREGISTRY.getTrace();
            final boolean newTrace = trace == null;
            if (newTrace) {
                trace = TraceObjectInterceptor.TRACEREGISTRY.registerTrace();
                this.monitoringCtrl.newMonitoringRecord(trace);
            }
            final long traceId = trace.getTraceId();
            final String clazz = context.getTarget().getClass().getCanonicalName();
            final int objectId = System.identityHashCode(context);
            // measure before execution
            this.monitoringCtrl.newMonitoringRecord(new BeforeOperationObjectEvent(this.timeSource.getTime(), traceId, trace.getNextOrderId(), signature, clazz, objectId));
            // execution of the called method
            try {
                final Object retval = context.proceed();
                // measure after successful execution
                this.monitoringCtrl.newMonitoringRecord(new AfterOperationObjectEvent(this.timeSource.getTime(), traceId, trace.getNextOrderId(), signature, clazz, objectId));
                return retval;
            } catch (final Throwable th) {
                // NOPMD NOCS (catch throw might
                // ok here)
                // measure after failed execution
                this.monitoringCtrl.newMonitoringRecord(new AfterOperationFailedObjectEvent(this.timeSource.getTime(), traceId, trace.getNextOrderId(), signature, clazz, th.toString(), objectId));
                throw th;
            } finally {
                if (newTrace) {
                    // close the trace
                    TraceObjectInterceptor.TRACEREGISTRY.unregisterTrace();
                }
            }
        } else {
            return context.proceed();
        }
    } else {
        return context.proceed();
    }
}
Also used : AfterOperationObjectEvent(kieker.common.record.flow.trace.operation.object.AfterOperationObjectEvent) AfterOperationFailedObjectEvent(kieker.common.record.flow.trace.operation.object.AfterOperationFailedObjectEvent) BeforeOperationObjectEvent(kieker.common.record.flow.trace.operation.object.BeforeOperationObjectEvent) TraceMetadata(kieker.common.record.flow.trace.TraceMetadata) AroundInvoke(javax.interceptor.AroundInvoke)

Example 2 with BeforeOperationObjectEvent

use of kieker.common.record.flow.trace.operation.object.BeforeOperationObjectEvent in project iobserve-analysis by research-iobserve.

the class SessionAndTraceRegistrationObjectFilter method doFilter.

/**
 * Register thread-local session and trace information, executes the given {@link FilterChain}
 * and unregisters the session/trace information. If configured, the execution of this filter is
 * also logged to the {@link IMonitoringController}. This method returns immediately if
 * monitoring is not enabled.
 *
 * @param request
 *            The request.
 * @param response
 *            The response.
 * @param chain
 *            The filter chain to be used.
 *
 * @throws IOException
 *             on io errors
 * @throws ServletException
 *             on servlet errors
 */
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    if (SessionAndTraceRegistrationObjectFilter.CTRLINST.isMonitoringEnabled()) {
        // if (CTRLINST.isProbeActivated(this.filterOperationSignatureString)) {
        final String operationSignature;
        final String componentSignature;
        final String method;
        final String path;
        final String sessionId;
        String query;
        if (request instanceof HttpServletRequest) {
            final HttpServletRequest httpRequest = (HttpServletRequest) request;
            method = httpRequest.getMethod();
            path = httpRequest.getRequestURI().replace('/', '.').substring(1);
            sessionId = httpRequest.getSession().getId();
            query = httpRequest.getQueryString();
            if (query == null) {
                query = "";
            }
        } else {
            method = "POST";
            path = request.getServletContext().getContextPath().replace('/', '.').substring(1);
            sessionId = "<no session>";
            query = "";
        }
        componentSignature = path.replaceAll("\\.[A-Za-z0-9]*$", "");
        TraceMetadata trace = SessionAndTraceRegistrationObjectFilter.TRACEREGISTRY.getTrace();
        final boolean newTrace = trace == null;
        if (newTrace) {
            SessionRegistry.INSTANCE.storeThreadLocalSessionId(sessionId);
            trace = SessionAndTraceRegistrationObjectFilter.TRACEREGISTRY.registerTrace();
            SessionAndTraceRegistrationObjectFilter.CTRLINST.newMonitoringRecord(trace);
            SessionAndTraceRegistrationObjectFilter.CTRLINST.newMonitoringRecord(new ServletTraceHelper(trace.getTraceId(), request.getRemoteHost(), request.getRemotePort(), path));
        }
        if ("GET".equals(method)) {
            operationSignature = path + "(" + query.replace(';', ':') + ")";
        } else if ("POST".equals(method)) {
            operationSignature = path + "()";
        } else {
            chain.doFilter(request, response);
            return;
        }
        final long traceId = trace.getTraceId();
        Integer objectId = SessionAndTraceRegistrationObjectFilter.PATH_MAP.get(path);
        if (objectId == null) {
            SessionAndTraceRegistrationObjectFilter.idCounter++;
            objectId = SessionAndTraceRegistrationObjectFilter.idCounter;
            SessionAndTraceRegistrationObjectFilter.PATH_MAP.put(path, objectId);
        }
        try {
            SessionAndTraceRegistrationObjectFilter.CTRLINST.newMonitoringRecord(new BeforeOperationObjectEvent(SessionAndTraceRegistrationObjectFilter.TIMESOURCE.getTime(), traceId, trace.getNextOrderId(), operationSignature, componentSignature, objectId));
            chain.doFilter(request, response);
            SessionAndTraceRegistrationObjectFilter.CTRLINST.newMonitoringRecord(new AfterOperationObjectEvent(SessionAndTraceRegistrationObjectFilter.TIMESOURCE.getTime(), traceId, trace.getNextOrderId(), operationSignature, componentSignature, objectId));
        } catch (final Throwable th) {
            // NOPMD NOCS (catch throw is ok here)
            SessionAndTraceRegistrationObjectFilter.CTRLINST.newMonitoringRecord(new AfterOperationFailedObjectEvent(SessionAndTraceRegistrationObjectFilter.TIMESOURCE.getTime(), traceId, trace.getNextOrderId(), operationSignature, componentSignature, th.toString(), objectId));
            throw new ServletException(th);
        } finally {
            // is this correct?
            SessionAndTraceRegistrationObjectFilter.SESSION_REGISTRY.unsetThreadLocalSessionId();
            // Reset the thread-local trace information
            if (newTrace) {
                // close the trace
                SessionAndTraceRegistrationObjectFilter.TRACEREGISTRY.unregisterTrace();
            }
        }
    // } else {
    // chain.doFilter(request, response);
    // return;
    // }
    } else {
        chain.doFilter(request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AfterOperationObjectEvent(kieker.common.record.flow.trace.operation.object.AfterOperationObjectEvent) AfterOperationFailedObjectEvent(kieker.common.record.flow.trace.operation.object.AfterOperationFailedObjectEvent) BeforeOperationObjectEvent(kieker.common.record.flow.trace.operation.object.BeforeOperationObjectEvent) TraceMetadata(kieker.common.record.flow.trace.TraceMetadata) ServletTraceHelper(org.iobserve.common.record.ServletTraceHelper)

Aggregations

TraceMetadata (kieker.common.record.flow.trace.TraceMetadata)2 AfterOperationFailedObjectEvent (kieker.common.record.flow.trace.operation.object.AfterOperationFailedObjectEvent)2 AfterOperationObjectEvent (kieker.common.record.flow.trace.operation.object.AfterOperationObjectEvent)2 BeforeOperationObjectEvent (kieker.common.record.flow.trace.operation.object.BeforeOperationObjectEvent)2 AroundInvoke (javax.interceptor.AroundInvoke)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ServletTraceHelper (org.iobserve.common.record.ServletTraceHelper)1