@Override
  protected void onComplete(Request request, Response response) {
    activeRequestsCounter.dec();
    Long duration =
        computeDurationMillis(START_TIMES_BY_CORRELATION_ID.remove(request.getCorrelationId()));

    if (duration != null && duration.longValue() > 0) {
      allTimesTimer.update(duration, TimeUnit.MILLISECONDS);

      String name = getRouteName(request);
      if (name == null || name.isEmpty()) return;

      ROUTE_TIMERS.get(name).update(duration, TimeUnit.MILLISECONDS);
    }

    Counter responseCounter = COUNTERS_BY_RESPONSE.get(response.getResponseStatus().getCode());

    if (responseCounter == null) {
      responseCounter = metrics.counter(getResponseCounterName(response.getResponseStatus()));
      COUNTERS_BY_RESPONSE.putIfAbsent(response.getResponseStatus().getCode(), responseCounter);
    }

    responseCounter.inc();
    publish(request, response, duration);
  }
  @Override
  public void handle(
      String path,
      Request request,
      HttpServletRequest httpRequest,
      HttpServletResponse httpResponse)
      throws IOException, ServletException {

    activeDispatches.inc();

    final long start;
    final HttpChannelState state = request.getHttpChannelState();
    if (state.isInitial()) {
      // new request
      activeRequests.inc();
      start = request.getTimeStamp();
    } else {
      // resumed request
      start = System.currentTimeMillis();
      activeSuspended.dec();
      if (state.getState() == State.DISPATCHED) {
        asyncDispatches.mark();
      }
    }

    try {
      super.handle(path, request, httpRequest, httpResponse);
    } finally {
      final long now = System.currentTimeMillis();
      final long dispatched = now - start;

      activeDispatches.dec();
      dispatches.update(dispatched, TimeUnit.MILLISECONDS);

      if (state.isSuspended()) {
        if (state.isInitial()) {
          state.addListener(listener);
        }
        activeSuspended.inc();
      } else if (state.isInitial()) {
        requests.update(dispatched, TimeUnit.MILLISECONDS);
        updateResponses(request);
      }
      // else onCompletion will handle it.
    }
  }
 private void updateResponses(Request request) {
   final int response = request.getResponse().getStatus() / 100;
   if (response >= 1 && response <= 5) {
     responses[response - 1].mark();
   }
   activeRequests.dec();
   final long elapsedTime = System.currentTimeMillis() - request.getTimeStamp();
   requests.update(elapsedTime, TimeUnit.MILLISECONDS);
   requestTimer(request.getMethod()).update(elapsedTime, TimeUnit.MILLISECONDS);
 }
  @Test
  public void testThatPercolationNotificationWorks() throws IOException, InterruptedException {
    SimpleNotifier notifier = new SimpleNotifier();

    MetricFilter percolationFilter =
        new MetricFilter() {
          @Override
          public boolean matches(String name, Metric metric) {
            return name.startsWith(prefix + ".foo");
          }
        };
    elasticsearchReporter =
        createElasticsearchReporterBuilder()
            .percolationFilter(percolationFilter)
            .percolationNotifier(notifier)
            .build();

    final Counter evictions = registry.counter("foo");
    evictions.inc(18);
    reportAndRefresh();

    QueryBuilder queryBuilder =
        QueryBuilders.boolQuery()
            .must(QueryBuilders.matchAllQuery())
            .filter(
                QueryBuilders.boolQuery()
                    .must(QueryBuilders.rangeQuery("count").gte(20))
                    .must(QueryBuilders.termQuery("name", prefix + ".foo")));
    String json = String.format("{ \"query\" : %s }", queryBuilder.buildAsBytes().toUtf8());
    client()
        .prepareIndex(indexWithDate, ".percolator", "myName")
        .setRefresh(true)
        .setSource(json)
        .execute()
        .actionGet();

    evictions.inc(1);
    reportAndRefresh();
    assertThat(notifier.metrics.size(), is(0));

    evictions.inc(2);
    reportAndRefresh();
    org.assertj.core.api.Assertions.assertThat(notifier.metrics.size()).isEqualTo(1);
    org.assertj.core.api.Assertions.assertThat(notifier.metrics).containsKey("myName");
    org.assertj.core.api.Assertions.assertThat(notifier.metrics.get("myName").name())
        .isEqualTo(prefix + ".foo");

    notifier.metrics.clear();
    evictions.dec(2);
    reportAndRefresh();
    org.assertj.core.api.Assertions.assertThat(notifier.metrics.size()).isEqualTo(0);
  }
示例#5
0
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
     throws IOException, ServletException {
   final StatusExposingServletResponse wrappedResponse =
       new StatusExposingServletResponse((HttpServletResponse) response);
   activeRequests.inc();
   final Timer.Context context = requestTimer.time();
   try {
     chain.doFilter(request, wrappedResponse);
   } finally {
     context.stop();
     activeRequests.dec();
     markMeterForStatusCode(wrappedResponse.getStatus());
   }
 }