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);
}
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);
}
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);
}
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();
}
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);
}
Aggregations