Search in sources :

Example 1 with ServerConfig

use of com.nike.riposte.server.config.ServerConfig 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 2 with ServerConfig

use of com.nike.riposte.server.config.ServerConfig 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 3 with ServerConfig

use of com.nike.riposte.server.config.ServerConfig 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)

Example 4 with ServerConfig

use of com.nike.riposte.server.config.ServerConfig in project riposte by Nike-Inc.

the class SignalFxEndpointMetricsHandlerTest method setupEndpointsMetrics_does_nothing.

@Test
public void setupEndpointsMetrics_does_nothing() {
    // given
    ServerConfig serverConfigMock = mock(ServerConfig.class);
    // when
    handler.setupEndpointsMetrics(serverConfigMock, metricRegistryMock);
    // then
    verifyZeroInteractions(serverConfigMock, metricMetadataMock, metricRegistryMock, requestTimerBuilderMock, dimensionConfiguratorMock);
}
Also used : ServerConfig(com.nike.riposte.server.config.ServerConfig) Test(org.junit.Test)

Aggregations

ServerConfig (com.nike.riposte.server.config.ServerConfig)4 Endpoint (com.nike.riposte.server.http.Endpoint)3 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)3 RequestInfo (com.nike.riposte.server.http.RequestInfo)3 ResponseInfo (com.nike.riposte.server.http.ResponseInfo)3 StandardEndpoint (com.nike.riposte.server.http.StandardEndpoint)3 Collection (java.util.Collection)3 Before (org.junit.Before)3 Histogram (com.codahale.metrics.Histogram)2 Counter (com.codahale.metrics.Counter)1 Gauge (com.codahale.metrics.Gauge)1 Meter (com.codahale.metrics.Meter)1 Metric (com.codahale.metrics.Metric)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 MetricRegistry.name (com.codahale.metrics.MetricRegistry.name)1 Timer (com.codahale.metrics.Timer)1 Builder (com.nike.riposte.metrics.codahale.CodahaleMetricsListener.Builder)1 DEFAULT_REQUEST_AND_RESPONSE_SIZE_HISTOGRAM_SUPPLIER (com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DEFAULT_REQUEST_AND_RESPONSE_SIZE_HISTOGRAM_SUPPLIER)1 DefaultMetricNamingStrategy (com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DefaultMetricNamingStrategy)1 DEFAULT_PREFIX (com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DefaultMetricNamingStrategy.DEFAULT_PREFIX)1