HttpResponseStatisticsCollector extends HandlerWrapper implements Graceful { public List<StatisticsEntry> takeStatistics() { var ret = new ArrayList<StatisticsEntry>(); for (HttpScheme scheme : HttpScheme.values()) { int schemeIndex = scheme.ordinal(); for (HttpMethod method : HttpMethod.values()) { int methodIndex = method.ordinal(); for (int group = 0; group < HTTP_RESPONSE_GROUPS.length; group++) { for (HttpRequest.RequestType type : HttpRequest.RequestType.values()) { long value = statistics[schemeIndex][methodIndex][group][type.ordinal()].sumThenReset(); if (value > 0) { ret.add(new StatisticsEntry(scheme.name().toLowerCase(), method.name(), HTTP_RESPONSE_GROUPS[group], type.name().toLowerCase(), value)); } } } } } return ret; } HttpResponseStatisticsCollector(List<String> monitoringHandlerPaths, List<String> searchHandlerPaths); @Override void handle(String path, Request baseRequest, HttpServletRequest request, HttpServletResponse response); List<StatisticsEntry> takeStatistics(); @Override Future<Void> shutdown(); @Override boolean isShutdown();  }
@Test public void statistics_are_aggregated_by_category() { testRequest("http", 300, "GET"); testRequest("http", 301, "GET"); testRequest("http", 200, "GET"); var stats = collector.takeStatistics(); assertStatisticsEntryPresent(stats, "http", "GET", Metrics.RESPONSES_2XX, 1L); assertStatisticsEntryPresent(stats, "http", "GET", Metrics.RESPONSES_3XX, 2L); }
@Test public void statistics_are_grouped_by_http_method_and_scheme() { testRequest("http", 200, "GET"); testRequest("http", 200, "PUT"); testRequest("http", 200, "POST"); testRequest("http", 200, "POST"); testRequest("http", 404, "GET"); testRequest("https", 404, "GET"); testRequest("https", 200, "POST"); testRequest("https", 200, "POST"); testRequest("https", 200, "POST"); testRequest("https", 200, "POST"); var stats = collector.takeStatistics(); assertStatisticsEntryPresent(stats, "http", "GET", Metrics.RESPONSES_2XX, 1L); assertStatisticsEntryPresent(stats, "http", "GET", Metrics.RESPONSES_4XX, 1L); assertStatisticsEntryPresent(stats, "http", "PUT", Metrics.RESPONSES_2XX, 1L); assertStatisticsEntryPresent(stats, "http", "POST", Metrics.RESPONSES_2XX, 2L); assertStatisticsEntryPresent(stats, "https", "GET", Metrics.RESPONSES_4XX, 1L); assertStatisticsEntryPresent(stats, "https", "POST", Metrics.RESPONSES_2XX, 4L); }
@Test public void statistics_include_grouped_and_single_statuscodes() { testRequest("http", 401, "GET"); testRequest("http", 404, "GET"); testRequest("http", 403, "GET"); var stats = collector.takeStatistics(); assertStatisticsEntryPresent(stats, "http", "GET", Metrics.RESPONSES_4XX, 3L); assertStatisticsEntryPresent(stats, "http", "GET", Metrics.RESPONSES_401, 1L); assertStatisticsEntryPresent(stats, "http", "GET", Metrics.RESPONSES_403, 1L); }
@Test public void retrieving_statistics_resets_the_counters() { testRequest("http", 200, "GET"); testRequest("http", 200, "GET"); var stats = collector.takeStatistics(); assertStatisticsEntryPresent(stats, "http", "GET", Metrics.RESPONSES_2XX, 2L); testRequest("http", 200, "GET"); stats = collector.takeStatistics(); assertStatisticsEntryPresent(stats, "http", "GET", Metrics.RESPONSES_2XX, 1L); }
@Test public void statistics_include_request_type_dimension() { testRequest("http", 200, "GET", "/search"); testRequest("http", 200, "POST", "/search"); testRequest("http", 200, "POST", "/feed"); testRequest("http", 200, "GET", "/status.html?foo=bar"); var stats = collector.takeStatistics(); assertStatisticsEntryWithRequestTypePresent(stats, "http", "GET", Metrics.RESPONSES_2XX, "monitoring", 1L); assertStatisticsEntryWithRequestTypePresent(stats, "http", "GET", Metrics.RESPONSES_2XX, "read", 1L); assertStatisticsEntryWithRequestTypePresent(stats, "http", "POST", Metrics.RESPONSES_2XX, "read", 1L); assertStatisticsEntryWithRequestTypePresent(stats, "http", "POST", Metrics.RESPONSES_2XX, "write", 1L); testRequest("http", 200, "GET"); stats = collector.takeStatistics(); assertStatisticsEntryPresent(stats, "http", "GET", Metrics.RESPONSES_2XX, 1L); }
@Test public void request_type_can_be_set_explicitly() { testRequest("http", 200, "GET", "/search", com.yahoo.jdisc.Request.RequestType.WRITE); var stats = collector.takeStatistics(); assertStatisticsEntryWithRequestTypePresent(stats, "http", "GET", Metrics.RESPONSES_2XX, "write", 1L); }