|
| 1 | +package com.gitblit.servlet; |
| 2 | + |
| 3 | +import io.prometheus.client.Counter; |
| 4 | +import io.prometheus.client.Histogram; |
| 5 | + |
| 6 | +import javax.servlet.*; |
| 7 | +import javax.servlet.http.HttpServletRequest; |
| 8 | +import javax.servlet.http.HttpServletResponse; |
| 9 | +import java.io.IOException; |
| 10 | + |
| 11 | +/** |
| 12 | + */ |
| 13 | +public class MetricsFilter implements Filter { |
| 14 | + public static final String PARAM_PATH_MAX_DEPTH = "max-path-depth"; |
| 15 | + public static final String PARAM_DURATION_HIST_BUCKET_CONFIG = "request-duration-histogram-buckets"; |
| 16 | + |
| 17 | + private Histogram httpRequestDuration = null; |
| 18 | + private Counter requests = null; |
| 19 | + |
| 20 | + // Package-level for testing purposes. |
| 21 | + int pathDepth = 1; |
| 22 | + private double[] buckets = null; |
| 23 | + |
| 24 | + public MetricsFilter() { |
| 25 | + } |
| 26 | + |
| 27 | + public MetricsFilter( |
| 28 | + Integer maxPathDepth, |
| 29 | + double[] buckets |
| 30 | + ) throws ServletException { |
| 31 | + this.buckets = buckets; |
| 32 | + if (maxPathDepth != null) { |
| 33 | + this.pathDepth = maxPathDepth; |
| 34 | + } |
| 35 | + this.init(null); |
| 36 | + } |
| 37 | + |
| 38 | + private boolean isEmpty(String s) { |
| 39 | + return s == null || s.length() == 0; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void init(FilterConfig filterConfig) throws ServletException { |
| 44 | + |
| 45 | + Histogram.Builder httpRequestDurationBuilder = Histogram.build() |
| 46 | + .name("http_request_duration_seconds") |
| 47 | + .labelNames("path", "method") |
| 48 | + .help("The time taken fulfilling servlet requests"); |
| 49 | + |
| 50 | + Counter.Builder requestsBuilder = Counter.build() |
| 51 | + .name("http_requests_total") |
| 52 | + .help("Total requests.") |
| 53 | + .labelNames("path", "method", "status"); |
| 54 | + |
| 55 | + if (filterConfig == null && isEmpty("http_request_duration")) { |
| 56 | + throw new ServletException("No configuration object provided, and no metricName passed via constructor"); |
| 57 | + } |
| 58 | + |
| 59 | + if (filterConfig != null) { |
| 60 | + |
| 61 | + // Allow overriding of the path "depth" to track |
| 62 | + if (!isEmpty(filterConfig.getInitParameter(PARAM_PATH_MAX_DEPTH))) { |
| 63 | + pathDepth = Integer.valueOf(filterConfig.getInitParameter(PARAM_PATH_MAX_DEPTH)); |
| 64 | + } |
| 65 | + |
| 66 | + // Allow users to override the default bucket configuration |
| 67 | + if (!isEmpty(filterConfig.getInitParameter(PARAM_DURATION_HIST_BUCKET_CONFIG))) { |
| 68 | + String[] bucketParams = filterConfig.getInitParameter(PARAM_DURATION_HIST_BUCKET_CONFIG).split(","); |
| 69 | + buckets = new double[bucketParams.length]; |
| 70 | + |
| 71 | + for (int i = 0; i < bucketParams.length; i++) { |
| 72 | + buckets[i] = Double.parseDouble(bucketParams[i]); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + requests = requestsBuilder.register(); |
| 78 | + |
| 79 | + if (buckets != null) { |
| 80 | + httpRequestDurationBuilder = httpRequestDurationBuilder.buckets(buckets); |
| 81 | + } |
| 82 | + |
| 83 | + httpRequestDuration = httpRequestDurationBuilder.register(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { |
| 88 | + if (!(servletRequest instanceof HttpServletRequest)) { |
| 89 | + filterChain.doFilter(servletRequest, servletResponse); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + HttpServletRequest request = (HttpServletRequest) servletRequest; |
| 94 | + HttpServletResponse response = (HttpServletResponse) servletResponse; |
| 95 | + |
| 96 | + String path = request.getRequestURI(); |
| 97 | + String normalizedPath = extractPathFrom(path, pathDepth); |
| 98 | + |
| 99 | + Histogram.Timer timer = httpRequestDuration |
| 100 | + .labels(normalizedPath, request.getMethod()) |
| 101 | + .startTimer(); |
| 102 | + try { |
| 103 | + filterChain.doFilter(servletRequest, servletResponse); |
| 104 | + requests.labels(normalizedPath, request.getMethod().toUpperCase(), String.valueOf(response.getStatus())).inc(); |
| 105 | + } finally { |
| 106 | + timer.observeDuration(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public String extractPathFrom(String requestUri, int maxPathDepth) { |
| 111 | + if (maxPathDepth < 0 || requestUri == null) { |
| 112 | + throw new IllegalArgumentException("Path depth has to >= 0"); |
| 113 | + } |
| 114 | + |
| 115 | + int count = 0; |
| 116 | + int pathPosition = -1; |
| 117 | + do { |
| 118 | + int lastPathPosition = pathPosition; |
| 119 | + pathPosition = requestUri.indexOf("/", pathPosition + 1); |
| 120 | + if (count > maxPathDepth || pathPosition < 0) { |
| 121 | + return requestUri.substring(0, lastPathPosition + 1); |
| 122 | + } |
| 123 | + count++; |
| 124 | + } while (count <= maxPathDepth); |
| 125 | + |
| 126 | + return requestUri.substring(0, pathPosition + 1); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void destroy() { |
| 131 | + } |
| 132 | + |
| 133 | +} |
| 134 | + |
0 commit comments