Search in sources :

Example 1 with ServletTraceHelper

use of org.iobserve.common.record.ServletTraceHelper 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

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