/** * Creates bean of type WebRequestTrackingFilter for request tracking * @param applicationName Name of the application to bind filter to * @return {@link Bean} of type {@link WebRequestTrackingFilter} */ @Bean @ConditionalOnMissingBean @DependsOn("telemetryConfiguration") public WebRequestTrackingFilter webRequestTrackingFilter(@Value("${spring.application.name:application}") String applicationName) { return new WebRequestTrackingFilter(applicationName); }
/** * Processing the given request and response. * * @param req The servlet request. * @param res The servlet response. * @param chain The filters chain * @throws IOException Exception that can be thrown from invoking the filters chain. * @throws ServletException Exception that can be thrown from invoking the filters chain. */ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { ApplicationInsightsHttpResponseWrapper response = new ApplicationInsightsHttpResponseWrapper((HttpServletResponse) res); setKeyOnTLS(key); boolean isRequestProcessedSuccessfully = invokeSafeOnBeginRequest(req, response); try { chain.doFilter(req, response); invokeSafeOnEndRequest(req, response, isRequestProcessedSuccessfully); } catch (ServletException se) { onException(se, req, response, isRequestProcessedSuccessfully); throw se; } catch (IOException ioe) { onException(ioe, req, response, isRequestProcessedSuccessfully); throw ioe; } catch (RuntimeException re) { onException(re, req, response, isRequestProcessedSuccessfully); throw re; } finally { cleanup(); } }
/** * Initializes the filter from the given config. * * @param config The filter configuration. */ public void init(FilterConfig config) { try { String appName = extractAppName(config.getServletContext()); initializeAgentIfAvailable(config); TelemetryConfiguration configuration = TelemetryConfiguration.getActive(); if (configuration == null) { InternalLogger.INSTANCE.error( "Java SDK configuration cannot be null. Web request tracking filter will be disabled."); return; } configureWebAppNameContextInitializer(appName, configuration); telemetryClient = new TelemetryClient(configuration); webModulesContainer = new WebModulesContainer(configuration); if (StringUtils.isNotEmpty(config.getFilterName())) { this.filterName = config.getFilterName(); } isInitialized = true; } catch (Exception e) { String filterName = this.getClass().getSimpleName(); InternalLogger.INSTANCE.info( "Application Insights filter %s has been failed to initialized.\n" + "Web request tracking filter will be disabled. Exception: %s", filterName, ExceptionUtils.getStackTrace(e)); } }
private void onException(Exception e, ServletRequest req, ServletResponse res, boolean isRequestProcessedSuccessfully) { try { InternalLogger.INSTANCE.trace("Unhandled application exception: %s", ExceptionUtils.getStackTrace(e)); if (telemetryClient != null) { telemetryClient.trackException(e); } } catch (Exception ignoreMe) { } invokeSafeOnEndRequest(req, res, isRequestProcessedSuccessfully); }
private void cleanup() { try { ThreadContext.remove(); setKeyOnTLS(null); for (ThreadLocalCleaner cleaner : cleaners) { cleaner.clean(); } } catch (ThreadDeath td) { throw td; } catch (Throwable t) { } }
/** * Destroy the filter by releases resources. */ public void destroy() { cleanup(); }
/** * Initializes the filter from the given config. * * @param config The filter configuration. */ public void init(FilterConfig config) { try { String appName = extractAppName(config.getServletContext()); initializeAgentIfAvailable(config); TelemetryConfiguration configuration = TelemetryConfiguration.getActive(); if (configuration == null) { InternalLogger.INSTANCE.error( "Java SDK configuration cannot be null. Web request tracking filter will be disabled."); return; } configureWebAppNameContextInitializer(appName, configuration); telemetryClient = new TelemetryClient(configuration); webModulesContainer = new WebModulesContainer(configuration); if (StringUtils.isNotEmpty(config.getFilterName())) { this.filterName = config.getFilterName(); } isInitialized = true; } catch (Exception e) { String filterName = this.getClass().getSimpleName(); InternalLogger.INSTANCE.info( "Application Insights filter %s has been failed to initialized.\n" + "Web request tracking filter will be disabled. Exception: %s", filterName, ExceptionUtils.getStackTrace(e)); } }
private void onException(Exception e, ServletRequest req, ServletResponse res, boolean isRequestProcessedSuccessfully) { try { InternalLogger.INSTANCE.trace("Unhandled application exception: %s", ExceptionUtils.getStackTrace(e)); if (telemetryClient != null) { telemetryClient.trackException(e); } } catch (Exception ignoreMe) { } invokeSafeOnEndRequest(req, res, isRequestProcessedSuccessfully); }
private void cleanup() { try { ThreadContext.remove(); setKeyOnTLS(null); for (ThreadLocalCleaner cleaner : cleaners) { cleaner.clean(); } } catch (ThreadDeath td) { throw td; } catch (Throwable t) { } }
/** * Destroy the filter by releases resources. */ public void destroy() { cleanup(); }
/** * Processing the given request and response. * * @param req The servlet request. * @param res The servlet response. * @param chain The filters chain * @throws IOException Exception that can be thrown from invoking the filters chain. * @throws ServletException Exception that can be thrown from invoking the filters chain. */ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { ApplicationInsightsHttpResponseWrapper response = new ApplicationInsightsHttpResponseWrapper((HttpServletResponse) res); setKeyOnTLS(key); boolean isRequestProcessedSuccessfully = invokeSafeOnBeginRequest(req, response); try { chain.doFilter(req, response); invokeSafeOnEndRequest(req, response, isRequestProcessedSuccessfully); } catch (ServletException se) { onException(se, req, response, isRequestProcessedSuccessfully); throw se; } catch (IOException ioe) { onException(ioe, req, response, isRequestProcessedSuccessfully); throw ioe; } catch (RuntimeException re) { onException(re, req, response, isRequestProcessedSuccessfully); throw re; } finally { cleanup(); } }
private Filter createInitializedFilter() throws ServletException { Filter filter = new WebRequestTrackingFilter(); FilterConfig config = mock(FilterConfig.class); when(config.getFilterName()).thenReturn(WebRequestTrackingFilter.FILTER_NAME); ServletContext context = mock(ServletContext.class); when(context.getContextPath()).thenReturn(this.mockContextPath); when(config.getServletContext()).thenReturn(context); filter.init(config); return filter; }