PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/atlassian-profiling/src/main/java/com/atlassian/util/profiling/filters/ProfilingFilter.java

https://bitbucket.org/atlassian/atlassian-profiling
Java | 161 lines | 81 code | 19 blank | 61 comment | 15 complexity | ccfbd82a87698d2e27eed6f45b357fbe MD5 | raw file
  1. package com.atlassian.util.profiling.filters;
  2. import com.atlassian.util.profiling.ProfilerConfiguration;
  3. import com.atlassian.util.profiling.Ticker;
  4. import com.atlassian.util.profiling.Timers;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import javax.servlet.FilterChain;
  8. import javax.servlet.FilterConfig;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletRequest;
  11. import javax.servlet.ServletResponse;
  12. import javax.servlet.http.HttpServletRequest;
  13. import java.io.IOException;
  14. import java.util.concurrent.TimeUnit;
  15. /**
  16. * Filter that will intercept requests & time how long it takes for them to return. It stores this information
  17. * in the ProfilingTimerBean.
  18. * <p>
  19. * Install the filter in your web.xml file as follows:
  20. * <pre>
  21. * &lt;filter&gt;
  22. * &lt;filter-name&gt;profiling&lt;/filter-name&gt;
  23. * &lt;filter-class&gt;com.atlassian.util.profiling.filters.ProfilingFilter&lt;/filter-class&gt;
  24. * &lt;init-param&gt;
  25. * &lt;param-name&gt;activate.param&lt;/param-name&gt;
  26. * &lt;param-value&gt;profilingfilter&lt;/param-value&gt;
  27. * &lt;/init-param&gt;
  28. * &lt;init-param&gt;
  29. * &lt;param-name&gt;autostart&lt;/param-name&gt;
  30. * &lt;param-value&gt;false&lt;/param-value&gt;
  31. * &lt;/init-param&gt;
  32. * &lt;/filter&gt;
  33. *
  34. * &lt;filter-mapping&gt;
  35. * &lt;filter-name&gt;profiling&lt;/filter-name&gt;
  36. * &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  37. * &lt;/filter-mapping&gt;
  38. * </pre>
  39. *
  40. * With the above settings you can turn the filter on by accessing any URL with the parameter
  41. * <code>profilingfilter=on</code>.eg:
  42. *
  43. * <pre> http://mywebsite.com/a.jsp?<b><i>profilingfilter=on</i></b></pre>
  44. * <p>
  45. * The above settings also sets the filter to not start automatically upon startup. This may be useful for
  46. * production, but you will most likely want to set this true in development.
  47. *
  48. * @author <a href="mailto:mike@atlassian.com">Mike Cannon-Brookes</a>
  49. * @author <a href="mailto:scott@atlassian.com">Scott Farquhar</a>
  50. *
  51. * @deprecated in 3.0 for removal in 5.0. Use the replacement {@link RequestProfilingFilter} that allows enabling
  52. * profiling for individual requests
  53. */
  54. @Deprecated
  55. public class ProfilingFilter implements javax.servlet.Filter {
  56. private static final Logger log = LoggerFactory.getLogger(ProfilingFilter.class);
  57. /**
  58. * This is the parameter you pass to the init parameter &amp; specify in the web.xml file: eg.
  59. * <pre>
  60. * &lt;filter&gt;
  61. * &lt;filter-name&gt;profile&lt;/filter-name&gt;
  62. * &lt;filter-class&gt;com.atlassian.util.profiling.filters.ProfilingFilter&lt;/filter-class&gt;
  63. * &lt;init-param&gt;
  64. * &lt;param-name&gt;autostart&lt;/param-name&gt;
  65. * &lt;param-value&gt;true&lt;/param-value&gt;
  66. * &lt;/init-param&gt;
  67. * &lt;/filter&gt;
  68. * </pre>
  69. */
  70. protected static final String AUTOSTART_PARAM = "autostart";
  71. protected final StatusUpdateStrategy statusUpdateStrategy;
  72. public ProfilingFilter() {
  73. this.statusUpdateStrategy = new ProfilingStatusUpdateViaRequestStrategy();
  74. }
  75. protected ProfilingFilter(StatusUpdateStrategy statusUpdateStrategy) {
  76. if (statusUpdateStrategy == null) {
  77. throw new IllegalArgumentException("statusUpdateStrategy must not be null!");
  78. }
  79. this.statusUpdateStrategy = statusUpdateStrategy;
  80. }
  81. public void destroy() {
  82. }
  83. /**
  84. * If the filter is on record start time, pass to filter chain, and then record total time on the return. Otherwise
  85. * just pass to the filter chain. The filter can be activated via a request through the
  86. * ProfilingStatusUpdateStrategy.
  87. */
  88. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  89. throws IOException, ServletException {
  90. statusUpdateStrategy.setStateViaRequest(request);
  91. //if filter is not on - then don't do any processing.
  92. if (!isFilterOn()) {
  93. chain.doFilter(request, response);
  94. return;
  95. }
  96. final String resource = getResourceName(request);
  97. try (Ticker ignored = Timers.start(resource)) {
  98. chain.doFilter(request, response);
  99. }
  100. }
  101. public void init(FilterConfig filterConfig) {
  102. //some servlet containers set this to be null
  103. if (filterConfig != null) {
  104. // init profiling: autostart
  105. String autostartParam = filterConfig.getInitParameter(AUTOSTART_PARAM);
  106. if (autostartParam != null) {
  107. if ("true".equals(autostartParam)) {
  108. log.debug("[Filter: {}] defaulting to on [{}=true]", filterConfig.getFilterName(), AUTOSTART_PARAM);
  109. turnProfilingOn();
  110. } else if ("false".equals(autostartParam)) {
  111. log.debug("[Filter: {}] defaulting to off [{}=false]", filterConfig.getFilterName(), AUTOSTART_PARAM);
  112. turnProfilingOff();
  113. } else {
  114. log.debug("[Filter: {}] autostart value: {} is unknown no action taken]",
  115. filterConfig.getFilterName(), autostartParam);
  116. }
  117. }
  118. if (statusUpdateStrategy instanceof FilterConfigAware) {
  119. ((FilterConfigAware) statusUpdateStrategy).configure(filterConfig);
  120. }
  121. }
  122. }
  123. private boolean isFilterOn() {
  124. return Timers.getConfiguration().isEnabled();
  125. }
  126. private String getResourceName(ServletRequest request) {
  127. //if an include file then get the proper resource name.
  128. if (request.getAttribute("javax.servlet.include.request_uri") != null) {
  129. return (String) request.getAttribute("javax.servlet.include.request_uri");
  130. } else {
  131. return ((HttpServletRequest) request).getRequestURI();
  132. }
  133. }
  134. protected void turnProfilingOn() {
  135. Timers.getConfiguration().setEnabled(true);
  136. }
  137. protected void turnProfilingOff() {
  138. ProfilerConfiguration config = Timers.getConfiguration();
  139. config.setMinTraceTime(0, TimeUnit.MILLISECONDS);
  140. config.setEnabled(false);
  141. }
  142. }