|
| 1 | +package io.prometheus.client.filter; |
| 2 | + |
| 3 | +import io.prometheus.client.Histogram; |
| 4 | + |
| 5 | +import javax.servlet.Filter; |
| 6 | +import javax.servlet.FilterChain; |
| 7 | +import javax.servlet.FilterConfig; |
| 8 | +import javax.servlet.ServletException; |
| 9 | +import javax.servlet.ServletRequest; |
| 10 | +import javax.servlet.ServletResponse; |
| 11 | +import javax.servlet.http.HttpServletRequest; |
| 12 | +import java.io.IOException; |
| 13 | + |
| 14 | +/** |
| 15 | + * The MetricsFilter class exists to provide a high-level filter that enables tunable collection of metrics for Servlet |
| 16 | + * performance. |
| 17 | + * |
| 18 | + * The Histogram name itself is required, and configured with a {@code metric-name} init parameter. |
| 19 | + * |
| 20 | + * The help parameter, configured with the {@code help} init parameter, is not required but strongly recommended. |
| 21 | + * |
| 22 | + * By default, this filter will provide metrics that distinguish only 1 level deep for the request path |
| 23 | + * (including servlet context path), but can be configured with the {@code path-components} init parameter. Any number |
| 24 | + * provided that is less than 1 will provide the full path granularity (warning, this may affect performance). |
| 25 | + * |
| 26 | + * The Histogram buckets can be configured with a {@code buckets} init parameter whose value is a comma-separated list |
| 27 | + * of valid {@code double} values. |
| 28 | + * |
| 29 | + * {@code |
| 30 | + * <filter> |
| 31 | + * <filter-name>prometheusFilter</filter-name> |
| 32 | + * <filter-class>net.cccnext.ssp.portal.spring.filter.PrometheusMetricsFilter</filter-class> |
| 33 | + * <init-param> |
| 34 | + * <param-name>metric-name</param-name> |
| 35 | + * <param-value>webapp_metrics_filter</param-value> |
| 36 | + * </init-param> |
| 37 | + * <init-param> |
| 38 | + * <param-name>help</param-name> |
| 39 | + * <param-value>The time taken fulfilling servlet requests</param-value> |
| 40 | + * </init-param> |
| 41 | + * <init-param> |
| 42 | + * <param-name>buckets</param-name> |
| 43 | + * <param-value>0.005,0.01,0.025,0.05,0.075,0.1,0.25,0.5,0.75,1,2.5,5,7.5,10</param-value> |
| 44 | + * </init-param> |
| 45 | + * <init-param> |
| 46 | + * <param-name>path-components</param-name> |
| 47 | + * <param-value>0</param-value> |
| 48 | + * </init-param> |
| 49 | + * </filter> |
| 50 | + * } |
| 51 | + * |
| 52 | + * @author Andrew Stuart <andrew.stuart2@gmail.com> |
| 53 | + */ |
| 54 | +public class MetricsFilter implements Filter { |
| 55 | + static final String PATH_COMPONENT_PARAM = "path-components"; |
| 56 | + static final String HELP_PARAM = "help"; |
| 57 | + static final String METRIC_NAME_PARAM = "metric-name"; |
| 58 | + static final String BUCKET_CONFIG_PARAM = "buckets"; |
| 59 | + |
| 60 | + private Histogram histogram = null; |
| 61 | + |
| 62 | + // Package-level for testing purposes. |
| 63 | + int pathComponents = 1; |
| 64 | + private String metricName = null; |
| 65 | + private String help = "The time taken fulfilling servlet requests"; |
| 66 | + private double[] buckets = null; |
| 67 | + |
| 68 | + public MetricsFilter() {} |
| 69 | + |
| 70 | + public MetricsFilter( |
| 71 | + String metricName, |
| 72 | + String help, |
| 73 | + Integer pathComponents, |
| 74 | + double[] buckets |
| 75 | + ) throws ServletException { |
| 76 | + this.metricName = metricName; |
| 77 | + this.buckets = buckets; |
| 78 | + if (help != null) { |
| 79 | + this.help = help; |
| 80 | + } |
| 81 | + if (pathComponents != null) { |
| 82 | + this.pathComponents = pathComponents; |
| 83 | + } |
| 84 | + this.init(null); |
| 85 | + } |
| 86 | + |
| 87 | + private boolean isEmpty(String s) { |
| 88 | + return s == null || s.length() == 0; |
| 89 | + } |
| 90 | + |
| 91 | + private String getComponents(String str) { |
| 92 | + if (str == null || pathComponents < 1) { |
| 93 | + return str; |
| 94 | + } |
| 95 | + int count = 0; |
| 96 | + int i = -1; |
| 97 | + do { |
| 98 | + i = str.indexOf("/", i + 1); |
| 99 | + if (i < 0) { |
| 100 | + // Path is longer than specified pathComponents. |
| 101 | + return str; |
| 102 | + } |
| 103 | + count++; |
| 104 | + } while (count <= pathComponents); |
| 105 | + |
| 106 | + return str.substring(0, i); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void init(FilterConfig filterConfig) throws ServletException { |
| 111 | + Histogram.Builder builder = Histogram.build() |
| 112 | + .labelNames("path", "method"); |
| 113 | + |
| 114 | + if (filterConfig == null && isEmpty(metricName)) { |
| 115 | + throw new ServletException("No configuration object provided, and no metricName passed via constructor"); |
| 116 | + } |
| 117 | + |
| 118 | + if (filterConfig != null) { |
| 119 | + if (isEmpty(metricName)) { |
| 120 | + metricName = filterConfig.getInitParameter(METRIC_NAME_PARAM); |
| 121 | + if (isEmpty(metricName)) { |
| 122 | + throw new ServletException("Init parameter \"" + METRIC_NAME_PARAM + "\" is required; please supply a value"); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (!isEmpty(filterConfig.getInitParameter(HELP_PARAM))) { |
| 127 | + help = filterConfig.getInitParameter(HELP_PARAM); |
| 128 | + } |
| 129 | + |
| 130 | + // Allow overriding of the path "depth" to track |
| 131 | + if (!isEmpty(filterConfig.getInitParameter(PATH_COMPONENT_PARAM))) { |
| 132 | + pathComponents = Integer.valueOf(filterConfig.getInitParameter(PATH_COMPONENT_PARAM)); |
| 133 | + } |
| 134 | + |
| 135 | + // Allow users to override the default bucket configuration |
| 136 | + if (!isEmpty(filterConfig.getInitParameter(BUCKET_CONFIG_PARAM))) { |
| 137 | + String[] bucketParams = filterConfig.getInitParameter(BUCKET_CONFIG_PARAM).split(","); |
| 138 | + buckets = new double[bucketParams.length]; |
| 139 | + |
| 140 | + for (int i = 0; i < bucketParams.length; i++) { |
| 141 | + buckets[i] = Double.parseDouble(bucketParams[i]); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if (buckets != null) { |
| 147 | + builder = builder.buckets(buckets); |
| 148 | + } |
| 149 | + |
| 150 | + histogram = builder |
| 151 | + .help(help) |
| 152 | + .name(metricName) |
| 153 | + .register(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { |
| 158 | + if (!(servletRequest instanceof HttpServletRequest)) { |
| 159 | + filterChain.doFilter(servletRequest, servletResponse); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + HttpServletRequest request = (HttpServletRequest) servletRequest; |
| 164 | + |
| 165 | + String path = request.getRequestURI(); |
| 166 | + |
| 167 | + Histogram.Timer timer = histogram |
| 168 | + .labels(getComponents(path), request.getMethod()) |
| 169 | + .startTimer(); |
| 170 | + |
| 171 | + try { |
| 172 | + filterChain.doFilter(servletRequest, servletResponse); |
| 173 | + } finally { |
| 174 | + timer.observeDuration(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void destroy() { |
| 180 | + } |
| 181 | +} |
0 commit comments