Search in sources :

Example 6 with Endpoint

use of com.nike.riposte.server.http.Endpoint in project riposte by Nike-Inc.

the class SignalFxEndpointMetricsHandlerTest method handleRequest_gracefully_handles_some_null_args.

@DataProvider(value = { "true   |   false   |   false", "false  |   true    |   false", "false  |   false   |   true", "true   |   true    |   true" }, splitBy = "\\|")
@Test
public void handleRequest_gracefully_handles_some_null_args(boolean endpointIsNull, boolean methodIsNull, boolean matchingPathTemplateIsNull) {
    // given
    int statusCode = 242;
    int statusCodeXXValue = 2;
    long elapsedTimeMillis = 42;
    Endpoint endpoint = (endpointIsNull) ? null : endpointMock;
    doReturn(endpoint).when(httpStateMock).getEndpointForExecution();
    String expectedEndpointClass = (endpointIsNull) ? "NONE" : endpoint.getClass().getName();
    HttpMethod method = (methodIsNull) ? null : httpMethod;
    doReturn(method).when(requestInfoMock).getMethod();
    String expectedMethodName = (methodIsNull) ? "NONE" : method.name();
    String pathTemplate = (matchingPathTemplateIsNull) ? null : matchingPathTemplate;
    doReturn(pathTemplate).when(httpStateMock).getMatchingPathTemplate();
    String expectedPathTemplate = (matchingPathTemplateIsNull) ? "NONE" : pathTemplate;
    // when
    handler.handleRequest(requestInfoMock, responseInfoMock, httpStateMock, statusCode, statusCodeXXValue, elapsedTimeMillis);
    // then
    verify(metricMetadataMock).forBuilder(requestTimerBuilderMock);
    verify(dimensionConfiguratorMock).setupMetricWithDimensions(timerBuilderTaggerMock, requestInfoMock, responseInfoMock, httpStateMock, statusCode, statusCodeXXValue, elapsedTimeMillis, endpoint, expectedEndpointClass, expectedMethodName, expectedPathTemplate);
    verify(timerBuilderTaggerMock).createOrGet(metricRegistryMock);
    verify(timerMock).update(elapsedTimeMillis, TimeUnit.MILLISECONDS);
}
Also used : Endpoint(com.nike.riposte.server.http.Endpoint) Matchers.anyString(org.mockito.Matchers.anyString) Endpoint(com.nike.riposte.server.http.Endpoint) HttpMethod(io.netty.handler.codec.http.HttpMethod) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 7 with Endpoint

use of com.nike.riposte.server.http.Endpoint in project riposte by Nike-Inc.

the class CodahaleMetricsListenerTest method initServerConfigMetrics_adds_expected_metrics.

@DataProvider(value = { "true", "false" })
@Test
public void initServerConfigMetrics_adds_expected_metrics(boolean includeServerConfigMetrics) {
    // given
    setupMetricRegistryAndCodahaleMetricsCollector();
    CodahaleMetricsListener instance = CodahaleMetricsListener.newBuilder(cmcMock).withEndpointMetricsHandler(endpointMetricsHandlerMock).withIncludeServerConfigMetrics(includeServerConfigMetrics).build();
    verifyServerStatisticMetrics(instance);
    String expectedBossThreadsGaugeName = name(ServerConfig.class.getSimpleName(), "boss_threads");
    String expectedWorkerThreadsGaugeName = name(ServerConfig.class.getSimpleName(), "worker_threads");
    String expectedMaxRequestSizeInBytesGaugeName = name(ServerConfig.class.getSimpleName(), "max_request_size_in_bytes");
    String expectedEndpointsListGaugeName = name(ServerConfig.class.getSimpleName(), "endpoints");
    List<String> expectedEndpointsListValue = serverConfig.appEndpoints().stream().map(endpoint -> endpoint.getClass().getName() + "-" + instance.getMatchingHttpMethodsAsCombinedString(endpoint) + "-" + endpoint.requestMatcher().matchingPathTemplates()).collect(Collectors.toList());
    // when
    instance.initEndpointAndServerConfigMetrics(serverConfig);
    // then
    if (includeServerConfigMetrics) {
        // Metrics for server config values
        assertThat(registeredGauges).containsKey(expectedBossThreadsGaugeName);
        assertThat(registeredGauges.get(expectedBossThreadsGaugeName).getValue()).isEqualTo(serverConfig.numBossThreads());
        assertThat(registeredGauges).containsKey(expectedWorkerThreadsGaugeName);
        assertThat(registeredGauges.get(expectedWorkerThreadsGaugeName).getValue()).isEqualTo(serverConfig.numWorkerThreads());
        assertThat(registeredGauges).containsKey(expectedMaxRequestSizeInBytesGaugeName);
        assertThat(registeredGauges.get(expectedMaxRequestSizeInBytesGaugeName).getValue()).isEqualTo(serverConfig.maxRequestSizeInBytes());
        assertThat(registeredGauges).containsKey(expectedEndpointsListGaugeName);
        assertThat(registeredGauges.get(expectedEndpointsListGaugeName).getValue()).isEqualTo(expectedEndpointsListValue);
    } else {
        // No server config values should have been registered.
        verifyNoMoreInteractions(metricRegistryMock);
    }
    // In either case, the EndpointMetricsHandler should have been called to delegate setting up endpoint-specific metrics.
    verify(endpointMetricsHandlerMock).setupEndpointsMetrics(serverConfig, metricRegistryMock);
}
Also used : Arrays(java.util.Arrays) MetricNamingStrategy(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.MetricNamingStrategy) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResponseInfo(com.nike.riposte.server.http.ResponseInfo) EndpointMetricsHandlerDefaultImpl(com.nike.riposte.metrics.codahale.impl.EndpointMetricsHandlerDefaultImpl) ServerConfig(com.nike.riposte.server.config.ServerConfig) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Mockito.doThrow(org.mockito.Mockito.doThrow) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Counter(com.codahale.metrics.Counter) Matchers.anyInt(org.mockito.Matchers.anyInt) DEFAULT_REQUEST_AND_RESPONSE_SIZE_HISTOGRAM_SUPPLIER(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DEFAULT_REQUEST_AND_RESPONSE_SIZE_HISTOGRAM_SUPPLIER) Mockito.doReturn(org.mockito.Mockito.doReturn) Builder(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.Builder) DefaultMetricNamingStrategy(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DefaultMetricNamingStrategy) Collection(java.util.Collection) Metric(com.codahale.metrics.Metric) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Matchers.any(org.mockito.Matchers.any) Endpoint(com.nike.riposte.server.http.Endpoint) List(java.util.List) Whitebox(org.mockito.internal.util.reflection.Whitebox) Timer(com.codahale.metrics.Timer) ResponseSender(com.nike.riposte.server.http.ResponseSender) Gauge(com.codahale.metrics.Gauge) Mockito.mock(org.mockito.Mockito.mock) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) Histogram(com.codahale.metrics.Histogram) RequestInfo(com.nike.riposte.server.http.RequestInfo) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ServerStatisticsMetricNames(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.ServerStatisticsMetricNames) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Supplier(java.util.function.Supplier) Matchers.anyString(org.mockito.Matchers.anyString) ServerConfigMetricNames(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.ServerConfigMetricNames) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Meter(com.codahale.metrics.Meter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ArgumentCaptor(org.mockito.ArgumentCaptor) Matchers.anyLong(org.mockito.Matchers.anyLong) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) DEFAULT_PREFIX(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DefaultMetricNamingStrategy.DEFAULT_PREFIX) DEFAULT_WORD_DELIMITER(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DefaultMetricNamingStrategy.DEFAULT_WORD_DELIMITER) Before(org.junit.Before) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) Mockito.verify(org.mockito.Mockito.verify) Matcher(com.nike.riposte.util.Matcher) Mockito.never(org.mockito.Mockito.never) ChronoUnit(java.time.temporal.ChronoUnit) MetricRegistry.name(com.codahale.metrics.MetricRegistry.name) ServerMetricsEvent(com.nike.riposte.server.metrics.ServerMetricsEvent) ServerConfig(com.nike.riposte.server.config.ServerConfig) Matchers.anyString(org.mockito.Matchers.anyString) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 8 with Endpoint

use of com.nike.riposte.server.http.Endpoint in project riposte by Nike-Inc.

the class CodahaleMetricsListenerTest method beforeMethod.

@Before
public void beforeMethod() {
    setupMetricRegistryAndCodahaleMetricsCollector();
    endpointMetricsHandlerMock = mock(EndpointMetricsHandler.class);
    mockHistogramSupplier = () -> mock(Histogram.class);
    listener = new CodahaleMetricsListener(cmcMock, endpointMetricsHandlerMock, true, null, null, mockHistogramSupplier);
    serverConfig = new ServerConfig() {

        private final List<Endpoint<?>> endpoints = Arrays.asList(new DummyEndpoint(Matcher.match("/foo")), new DummyEndpoint(Matcher.match("/bar", HttpMethod.POST, HttpMethod.PUT)), new DummyEndpoint(Matcher.multiMatch(Arrays.asList("/multiFoo", "/multiBar"))), new DummyEndpoint(Matcher.multiMatch(Arrays.asList("/multiBaz", "/multiBat"), HttpMethod.PATCH, HttpMethod.OPTIONS)));

        @Override
        public Collection<Endpoint<?>> appEndpoints() {
            return endpoints;
        }

        @Override
        public int numBossThreads() {
            return 3;
        }

        @Override
        public int numWorkerThreads() {
            return 42;
        }

        @Override
        public int maxRequestSizeInBytes() {
            return 42434445;
        }
    };
    listener.initEndpointAndServerConfigMetrics(serverConfig);
    requestInfoMock = mock(RequestInfo.class);
    responseInfoMock = mock(ResponseInfo.class);
    state = new HttpProcessingState();
    state.setRequestInfo(requestInfoMock);
    state.setResponseInfo(responseInfoMock);
    requestStartTime = Instant.now().minus(42, ChronoUnit.MILLIS);
    state.setRequestStartTime(requestStartTime);
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) ServerConfig(com.nike.riposte.server.config.ServerConfig) Histogram(com.codahale.metrics.Histogram) Endpoint(com.nike.riposte.server.http.Endpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) Collection(java.util.Collection) RequestInfo(com.nike.riposte.server.http.RequestInfo) Before(org.junit.Before)

Example 9 with Endpoint

use of com.nike.riposte.server.http.Endpoint in project riposte by Nike-Inc.

the class EndpointMetricsHandlerDefaultImplTest method handleRequest_works_as_expected_for_request_that_do_not_hit_an_endpoint.

@DataProvider(value = { "200", "404", "405", "500" })
@Test
public void handleRequest_works_as_expected_for_request_that_do_not_hit_an_endpoint(int responseStatusCode) {
    // given
    state.setEndpointForExecution(null, null);
    String expectedTimerKey;
    switch(responseStatusCode) {
        case 404:
            expectedTimerKey = ENDPOINT_NOT_FOUND_MAP_KEY;
            break;
        case 405:
            expectedTimerKey = METHOD_NOT_ALLOWED_MAP_KEY;
            break;
        case 500:
            expectedTimerKey = ROUTING_ERROR_MAP_KEY;
            break;
        default:
            expectedTimerKey = NO_ENDPOINT_SHORT_CIRCUIT_KEY;
    }
    Timer expectedEndpointTimerToUpdate = instance.endpointRequestsTimers.get(expectedTimerKey);
    assertThat(expectedEndpointTimerToUpdate).isNotNull();
    int responseHttpStatusCodeXXValue = responseStatusCode / 100;
    long elapsedTimeMillis = 42;
    // when
    instance.handleRequest(requestInfoMock, responseInfoMock, state, responseStatusCode, responseHttpStatusCodeXXValue, elapsedTimeMillis);
    // then
    // The special timer for this use case should be updated with the elapsed time of the request
    verify(expectedEndpointTimerToUpdate).update(elapsedTimeMillis, TimeUnit.MILLISECONDS);
    final int httpStatusCodeXXValue = responseStatusCode / 100;
    // The correct 1xx, 2xx, 3xx, 4xx, or 5xx meter for all requests should be marked.
    verify(instance.responses[httpStatusCodeXXValue - 1]).mark();
    // The correct 1xx, 2xx, 3xx, 4xx, or 5xx meter for this request's non-endpoint should be marked.
    Meter[] nonEndpointResponseMeterArray = instance.endpointResponsesMeters.get(expectedTimerKey);
    assertThat(nonEndpointResponseMeterArray).isNotNull();
    int meterIndexToUse = (NO_ENDPOINT_SHORT_CIRCUIT_KEY.equals(expectedTimerKey)) ? httpStatusCodeXXValue - 1 : 0;
    verify(nonEndpointResponseMeterArray[meterIndexToUse]).mark();
}
Also used : Timer(com.codahale.metrics.Timer) Meter(com.codahale.metrics.Meter) Matchers.anyString(org.mockito.Matchers.anyString) 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 10 with Endpoint

use of com.nike.riposte.server.http.Endpoint in project riposte by Nike-Inc.

the class EndpointMetricsHandlerDefaultImplTest method beforeMethod.

@Before
public void beforeMethod() {
    instance = spy(new EndpointMetricsHandlerDefaultImpl());
    serverConfig = new ServerConfig() {

        private final List<Endpoint<?>> endpoints = Arrays.asList(new DummyEndpoint(Matcher.match("/foo")), new DummyEndpoint(Matcher.match("/bar", HttpMethod.POST, HttpMethod.PUT)), new DummyEndpoint(Matcher.multiMatch(Arrays.asList("/multiFoo", "/multiBar"))), new DummyEndpoint(Matcher.multiMatch(Arrays.asList("/multiBaz", "/multiBat"), HttpMethod.PATCH, HttpMethod.OPTIONS)));

        @Override
        public Collection<Endpoint<?>> appEndpoints() {
            return endpoints;
        }

        @Override
        public int numBossThreads() {
            return 3;
        }

        @Override
        public int numWorkerThreads() {
            return 42;
        }

        @Override
        public int maxRequestSizeInBytes() {
            return 42434445;
        }
    };
    setupMetricRegistryMock();
    requestInfoMock = mock(RequestInfo.class);
    responseInfoMock = mock(ResponseInfo.class);
    state = new HttpProcessingState();
    state.setRequestInfo(requestInfoMock);
    state.setResponseInfo(responseInfoMock);
    state.setRequestStartTime(Instant.now());
    instance.setupEndpointsMetrics(serverConfig, metricRegistryMock);
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) ServerConfig(com.nike.riposte.server.config.ServerConfig) Endpoint(com.nike.riposte.server.http.Endpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) Collection(java.util.Collection) RequestInfo(com.nike.riposte.server.http.RequestInfo) Before(org.junit.Before)

Aggregations

Endpoint (com.nike.riposte.server.http.Endpoint)15 Test (org.junit.Test)10 Matchers.anyString (org.mockito.Matchers.anyString)8 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)7 RequestInfo (com.nike.riposte.server.http.RequestInfo)7 StandardEndpoint (com.nike.riposte.server.http.StandardEndpoint)7 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)7 ResponseInfo (com.nike.riposte.server.http.ResponseInfo)6 Timer (com.codahale.metrics.Timer)5 HttpMethod (io.netty.handler.codec.http.HttpMethod)5 Meter (com.codahale.metrics.Meter)4 ServerConfig (com.nike.riposte.server.config.ServerConfig)4 Executor (java.util.concurrent.Executor)4 Histogram (com.codahale.metrics.Histogram)3 MetricRegistry (com.codahale.metrics.MetricRegistry)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3