Search in sources :

Example 1 with ServerMetricsEvent

use of com.nike.riposte.server.metrics.ServerMetricsEvent in project riposte by Nike-Inc.

the class CodahaleMetricsListenerTest method onEvent_does_nothing_if_event_type_is_unknown.

@Test
public void onEvent_does_nothing_if_event_type_is_unknown() {
    // given
    ServerMetricsEvent event = null;
    Logger loggerMock = mock(Logger.class);
    doReturn(false).when(loggerMock).isDebugEnabled();
    Whitebox.setInternalState(listener, "logger", loggerMock);
    // when
    listener.onEvent(event, null);
    // then
    verifyZeroInteractions(listener.inflightRequests, listener.responseWriteFailed, listener.processedRequests);
    verify(loggerMock).error("Metrics Error: unknown metrics event " + event);
}
Also used : ServerMetricsEvent(com.nike.riposte.server.metrics.ServerMetricsEvent) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 2 with ServerMetricsEvent

use of com.nike.riposte.server.metrics.ServerMetricsEvent in project riposte by Nike-Inc.

the class CodahaleMetricsListenerTest method onEvent_works_as_expected_for_RESPONSE_WRITE_FAILED.

@Test
public void onEvent_works_as_expected_for_RESPONSE_WRITE_FAILED() {
    // given
    ServerMetricsEvent event = ServerMetricsEvent.RESPONSE_WRITE_FAILED;
    // when
    listener.onEvent(event, null);
    // then
    verify(listener.responseWriteFailed).inc();
}
Also used : ServerMetricsEvent(com.nike.riposte.server.metrics.ServerMetricsEvent) Test(org.junit.Test)

Example 3 with ServerMetricsEvent

use of com.nike.riposte.server.metrics.ServerMetricsEvent in project riposte by Nike-Inc.

the class CodahaleMetricsListenerTest method onEvent_works_as_expected_for_REQUEST_RECEIVED.

@Test
public void onEvent_works_as_expected_for_REQUEST_RECEIVED() {
    // given
    ServerMetricsEvent event = ServerMetricsEvent.REQUEST_RECEIVED;
    // when
    listener.onEvent(event, null);
    // then
    verify(listener.inflightRequests).inc();
}
Also used : ServerMetricsEvent(com.nike.riposte.server.metrics.ServerMetricsEvent) Test(org.junit.Test)

Example 4 with ServerMetricsEvent

use of com.nike.riposte.server.metrics.ServerMetricsEvent in project riposte by Nike-Inc.

the class CodahaleMetricsListenerTest method onEvent_works_as_expected_for_RESPONSE_SENT_without_endpoint.

@DataProvider(value = { "200", "404", "405", "500" })
@Test
public void onEvent_works_as_expected_for_RESPONSE_SENT_without_endpoint(int responseStatusCode) {
    // Should work the same as the onEvent_works_as_expected_for_RESPONSE_SENT_with_endpoint test above
    //      as far as CodahaleMetricsListener is concerned - it's the EndpointMetricsHandler's responsibility
    //      to deal with any intricacies around this use case.
    // given
    ServerMetricsEvent event = ServerMetricsEvent.RESPONSE_SENT;
    state.setEndpointForExecution(null, null);
    doReturn(responseStatusCode).when(responseInfoMock).getHttpStatusCodeWithDefault(ResponseSender.DEFAULT_HTTP_STATUS_CODE);
    int requestRawContentLengthBytes = (int) (Math.random() * 10000);
    doReturn(requestRawContentLengthBytes).when(requestInfoMock).getRawContentLengthInBytes();
    long finalResponseContentLength = (long) (Math.random() * 10000);
    doReturn(finalResponseContentLength).when(responseInfoMock).getFinalContentLength();
    // when
    long beforeCallTime = System.currentTimeMillis();
    listener.onEvent(event, state);
    long afterCallTime = System.currentTimeMillis();
    // then
    // Inflight requests counter decremented
    verify(listener.inflightRequests).dec();
    // Processed requests counter incremented
    verify(listener.processedRequests).inc();
    // If response code is greater than or equal to 400, then the failed requests counter should be incremented.
    if (responseStatusCode >= 400)
        verify(listener.failedRequests).inc();
    // Request and response size histograms should be updated with the relevant values from the request and response.
    verify(listener.requestSizes).update(requestRawContentLengthBytes);
    verify(listener.responseSizes).update(finalResponseContentLength);
    // The EndpointMetricsHandler should have been notified
    int responseHttpStatusCodeXXValue = responseStatusCode / 100;
    long expectedElapsedTimeMillisLowerBound = beforeCallTime - requestStartTime.toEpochMilli();
    long expectedElapsedTimeMillisUpperBound = afterCallTime - requestStartTime.toEpochMilli();
    ArgumentCaptor<Long> elapsedTimeMillisArgCaptor = ArgumentCaptor.forClass(Long.class);
    verify(endpointMetricsHandlerMock).handleRequest(eq(requestInfoMock), eq(responseInfoMock), eq(state), eq(responseStatusCode), eq(responseHttpStatusCodeXXValue), elapsedTimeMillisArgCaptor.capture());
    assertThat(elapsedTimeMillisArgCaptor.getValue()).isBetween(expectedElapsedTimeMillisLowerBound, expectedElapsedTimeMillisUpperBound);
}
Also used : Matchers.anyLong(org.mockito.Matchers.anyLong) ServerMetricsEvent(com.nike.riposte.server.metrics.ServerMetricsEvent) Endpoint(com.nike.riposte.server.http.Endpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 5 with ServerMetricsEvent

use of com.nike.riposte.server.metrics.ServerMetricsEvent in project riposte by Nike-Inc.

the class CodahaleMetricsListenerTest method onEvent_works_as_expected_for_RESPONSE_SENT_with_endpoint.

@DataProvider(value = { "GET    |   99", "GET    |   142", "GET    |   242", "GET    |   342", "GET    |   404", "GET    |   405", "GET    |   442", "GET    |   500", "GET    |   542", "GET    |   600", "POST   |   99", "POST   |   142", "POST   |   242", "POST   |   342", "POST   |   404", "POST   |   405", "POST   |   442", "POST   |   500", "POST   |   542", "POST   |   600", "PUT    |   99", "PUT    |   142", "PUT    |   242", "PUT    |   342", "PUT    |   404", "PUT    |   405", "PUT    |   442", "PUT    |   500", "PUT    |   542", "PUT    |   600", "DELETE |   99", "DELETE |   142", "DELETE |   242", "DELETE |   342", "DELETE |   404", "DELETE |   405", "DELETE |   442", "DELETE |   500", "DELETE |   542", "DELETE |   600", "PATCH  |   99", "PATCH  |   142", "PATCH  |   242", "PATCH  |   342", "PATCH  |   404", "PATCH  |   405", "PATCH  |   442", "PATCH  |   500", "PATCH  |   542", "PATCH  |   600", "null   |   99", "null   |   142", "null   |   242", "null   |   342", "null   |   404", "null   |   405", "null   |   442", "null   |   500", "null   |   542", "null   |   600" }, splitBy = "\\|")
@Test
public void onEvent_works_as_expected_for_RESPONSE_SENT_with_endpoint(String requestMethodStr, int responseStatusCode) throws InterruptedException {
    // given
    ServerMetricsEvent event = ServerMetricsEvent.RESPONSE_SENT;
    HttpMethod requestMethod = (requestMethodStr == null) ? null : HttpMethod.valueOf(requestMethodStr);
    doReturn(requestMethod).when(requestInfoMock).getMethod();
    Endpoint<?> endpoint = serverConfig.appEndpoints().iterator().next();
    String matchingPathTemplate = "/" + UUID.randomUUID().toString();
    state.setEndpointForExecution(endpoint, matchingPathTemplate);
    doReturn(responseStatusCode).when(responseInfoMock).getHttpStatusCodeWithDefault(ResponseSender.DEFAULT_HTTP_STATUS_CODE);
    int requestRawContentLengthBytes = (int) (Math.random() * 10000);
    doReturn(requestRawContentLengthBytes).when(requestInfoMock).getRawContentLengthInBytes();
    long finalResponseContentLength = (long) (Math.random() * 10000);
    doReturn(finalResponseContentLength).when(responseInfoMock).getFinalContentLength();
    Thread.sleep((long) (Math.random() * 25));
    // when
    long beforeCallTime = System.currentTimeMillis();
    listener.onEvent(event, state);
    long afterCallTime = System.currentTimeMillis();
    // then
    // Inflight requests counter decremented
    verify(listener.inflightRequests).dec();
    // Processed requests counter incremented
    verify(listener.processedRequests).inc();
    // If response code is greater than or equal to 400, then the failed requests counter should be incremented.
    if (responseStatusCode >= 400)
        verify(listener.failedRequests).inc();
    // Request and response size histograms should be updated with the relevant values from the request and response.
    verify(listener.requestSizes).update(requestRawContentLengthBytes);
    verify(listener.responseSizes).update(finalResponseContentLength);
    // The EndpointMetricsHandler should have been notified
    int responseHttpStatusCodeXXValue = responseStatusCode / 100;
    long expectedElapsedTimeMillisLowerBound = beforeCallTime - requestStartTime.toEpochMilli();
    long expectedElapsedTimeMillisUpperBound = afterCallTime - requestStartTime.toEpochMilli();
    ArgumentCaptor<Long> elapsedTimeMillisArgCaptor = ArgumentCaptor.forClass(Long.class);
    verify(endpointMetricsHandlerMock).handleRequest(eq(requestInfoMock), eq(responseInfoMock), eq(state), eq(responseStatusCode), eq(responseHttpStatusCodeXXValue), elapsedTimeMillisArgCaptor.capture());
    assertThat(elapsedTimeMillisArgCaptor.getValue()).isBetween(expectedElapsedTimeMillisLowerBound, expectedElapsedTimeMillisUpperBound);
}
Also used : Matchers.anyLong(org.mockito.Matchers.anyLong) ServerMetricsEvent(com.nike.riposte.server.metrics.ServerMetricsEvent) Matchers.anyString(org.mockito.Matchers.anyString) HttpMethod(io.netty.handler.codec.http.HttpMethod) Endpoint(com.nike.riposte.server.http.Endpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Aggregations

ServerMetricsEvent (com.nike.riposte.server.metrics.ServerMetricsEvent)6 Test (org.junit.Test)6 Endpoint (com.nike.riposte.server.http.Endpoint)2 StandardEndpoint (com.nike.riposte.server.http.StandardEndpoint)2 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)2 Matchers.anyLong (org.mockito.Matchers.anyLong)2 Logger (org.slf4j.Logger)2 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 Matchers.anyString (org.mockito.Matchers.anyString)1