@Test public void createAndRegisterRequestTimer_should_use_requestTimerGenerator() { // given Timer timerMock = mock(Timer.class); Supplier<Timer> timerSupplier = () -> timerMock; EndpointMetricsHandlerDefaultImpl newImpl = new EndpointMetricsHandlerDefaultImpl(timerSupplier); // when Timer result = newImpl.createAndRegisterRequestTimer("foo", metricRegistryMock); // then assertThat(result).isSameAs(timerMock); }
endpointResponsesMeters = new HashMap<>(); for (Endpoint<?> endpoint : config.appEndpoints()) { String timerAndMeterMapKeyForEndpoint = getTimerAndMeterMapKeyForEndpoint(endpoint); String name = name(prefix, "endpoints." + endpoint.getClass().getName().replace(".", "-") + "-" + timerAndMeterMapKeyForEndpoint); endpointRequestsTimers.put(timerAndMeterMapKeyForEndpoint, createAndRegisterRequestTimer(name, metricRegistry)); .put(ENDPOINT_NOT_FOUND_MAP_KEY, createAndRegisterRequestTimer(notFoundNameId, metricRegistry)); endpointResponsesMeters.put(ENDPOINT_NOT_FOUND_MAP_KEY, new Meter[]{ metricRegistry.meter(name(notFoundNameId, "4xx-responses"))}); .put(METHOD_NOT_ALLOWED_MAP_KEY, createAndRegisterRequestTimer(methodNotAllowedNameId, metricRegistry)); endpointResponsesMeters.put(METHOD_NOT_ALLOWED_MAP_KEY, new Meter[]{ metricRegistry.meter(name(methodNotAllowedNameId, "4xx-responses"))}); .put(ROUTING_ERROR_MAP_KEY, createAndRegisterRequestTimer(routingErrorNameId, metricRegistry)); endpointResponsesMeters.put(ROUTING_ERROR_MAP_KEY, new Meter[]{ metricRegistry.meter(name(routingErrorNameId, "5xx-responses"))}); .put(NO_ENDPOINT_SHORT_CIRCUIT_KEY, createAndRegisterRequestTimer(shortCircuitNameId, metricRegistry)); endpointResponsesMeters.put(NO_ENDPOINT_SHORT_CIRCUIT_KEY, new Meter[]{ metricRegistry.meter(name(shortCircuitNameId, "1xx-responses")), // 1xx
protected String getTimerAndMeterMapKeyForEndpoint(Endpoint<?> endpoint) { String methodsString = getMatchingHttpMethodsAsCombinedString(endpoint); //TODO: this might be odd for multi-path endpoints return methodsString + "-" + endpoint.requestMatcher().matchingPathTemplates(); }
@Test public void setupEndpointsMetrics_throws_IllegalArgumentException_if_metricRegistry_is_null() { // given EndpointMetricsHandlerDefaultImpl newImpl = new EndpointMetricsHandlerDefaultImpl(); // when Throwable ex = catchThrowable(() -> newImpl.setupEndpointsMetrics(serverConfig, null)); // then assertThat(ex) .isInstanceOf(IllegalArgumentException.class) .hasMessage("MetricRegistry cannot be null"); }
requestTimer(requestInfo.getMethod()).update(requestElapsedTimeMillis, TimeUnit.MILLISECONDS); String endpointMapKey; if (endpoint != null) endpointMapKey = getTimerAndMeterMapKeyForEndpoint(endpoint); else if (is500RoutingError) endpointMapKey = ROUTING_ERROR_MAP_KEY;
EndpointMetricsHandlerDefaultImpl newImpl = spy(new EndpointMetricsHandlerDefaultImpl()); String expectedTimerPrefix = customizeTimerCreation ? UUID.randomUUID().toString() + newImpl.prefix MetricRegistry registry = invocation.getArgumentAt(1, MetricRegistry.class); return registry.register(name, mock(Timer.class)); }).when(newImpl).createAndRegisterRequestTimer(anyString(), any(MetricRegistry.class)); newImpl.setupEndpointsMetrics(serverConfig, metricRegistryMock); assertThat(newImpl.getRequests()).isSameAs(newImpl.requests); assertThat(newImpl.requests).isEqualTo(registeredTimers.get(name(expectedTimerPrefix, "requests"))); assertThat(mockingDetails(newImpl.requests).isMock()).isEqualTo(customizeTimerCreation); assertThat(newImpl.getResponses()).isSameAs(newImpl.responses); assertThat(newImpl.responses.length).isEqualTo(5); assertThat(newImpl.responses).isEqualTo(new Meter[]{ assertThat(newImpl.getGetRequests()).isSameAs(newImpl.getRequests); assertThat(newImpl.getRequests).isEqualTo(registeredTimers.get(name(expectedTimerPrefix, "get-requests"))); assertThat(mockingDetails(newImpl.getRequests).isMock()).isEqualTo(customizeTimerCreation); assertThat(newImpl.getPostRequests()).isSameAs(newImpl.postRequests); assertThat(newImpl.postRequests).isEqualTo(registeredTimers.get(name(expectedTimerPrefix, "post-requests"))); assertThat(mockingDetails(newImpl.postRequests).isMock()).isEqualTo(customizeTimerCreation); assertThat(newImpl.getPutRequests()).isSameAs(newImpl.putRequests); assertThat(newImpl.putRequests).isEqualTo(registeredTimers.get(name(expectedTimerPrefix, "put-requests"))); assertThat(mockingDetails(newImpl.putRequests).isMock()).isEqualTo(customizeTimerCreation);
long prevFailedCount = metricsListener.getFailedRequests().getCount(); long prevProcessedCount = metricsListener.getProcessedRequests().getCount(); long prevRequestsCount = endpointMetricsHandler.getRequests().getCount(); long prevGetRequestsCount = endpointMetricsHandler.getGetRequests().getCount(); long prevResponsesCount = endpointMetricsHandler.getResponses()[5 - 1].getCount(); long prevRequestCount = ((Timer) endpointMetricsHandler.getEndpointRequestsTimers() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY)) .getCount(); long prevResponseCount = ((Meter[]) endpointMetricsHandler.getEndpointResponsesMeters() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY))[5 - 1] long requestsCount = endpointMetricsHandler.getRequests().getCount(); long requestSize = metricsListener.getRequestSizes().getSnapshot().getMax(); long responseSize = metricsListener.getResponseSizes().getSnapshot().getMax(); long getRequestsCount = endpointMetricsHandler.getGetRequests().getCount(); long responsesCount = endpointMetricsHandler.getResponses()[5 - 1].getCount(); long requestCount = ((Timer) endpointMetricsHandler.getEndpointRequestsTimers() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY)) .getCount(); long responseCount = ((Meter[]) endpointMetricsHandler.getEndpointResponsesMeters() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY))[5 - 1] .getCount(); long postRequests = endpointMetricsHandler.getPostRequests().getCount(); long putRequests = endpointMetricsHandler.getPutRequests().getCount(); long deleteRequests = endpointMetricsHandler.getDeleteRequests().getCount(); long otherRequests = endpointMetricsHandler.getOtherRequests().getCount();
endpointMetricsHandler = new EndpointMetricsHandlerDefaultImpl();
state.setEndpointForExecution(endpoint, matchingPathTemplate); String endpointTimerAndMeterKey = instance.getTimerAndMeterMapKeyForEndpoint(endpoint); instance.handleRequest( requestInfoMock, responseInfoMock, state, responseStatusCode, responseHttpStatusCodeXXValue, elapsedTimeMillis
protected void setupEndpointAggregateMetrics(MetricRegistry metricRegistry) { // codahale this.requests = createAndRegisterRequestTimer(name(prefix, "requests"), metricRegistry); this.responses = new Meter[]{ metricRegistry.meter(name(prefix, "1xx-responses")), // 1xx metricRegistry.meter(name(prefix, "2xx-responses")), // 2xx metricRegistry.meter(name(prefix, "3xx-responses")), // 3xx metricRegistry.meter(name(prefix, "4xx-responses")), // 4xx metricRegistry.meter(name(prefix, "5xx-responses")) // 5xx }; this.getRequests = createAndRegisterRequestTimer(name(prefix, "get-requests"), metricRegistry); this.postRequests = createAndRegisterRequestTimer(name(prefix, "post-requests"), metricRegistry); this.putRequests = createAndRegisterRequestTimer(name(prefix, "put-requests"), metricRegistry); this.deleteRequests = createAndRegisterRequestTimer(name(prefix, "delete-requests"), metricRegistry); this.otherRequests = createAndRegisterRequestTimer(name(prefix, "other-requests"), metricRegistry); }
long prevFailedCount = metricsListener.getFailedRequests().getCount(); long prevProcessedCount = metricsListener.getProcessedRequests().getCount(); long prevRequestsCount = endpointMetricsHandler.getRequests().getCount(); long prevGetRequestsCount = endpointMetricsHandler.getGetRequests().getCount(); long prevResponsesCount = endpointMetricsHandler.getResponses()[4 - 1].getCount(); long prevRequestCount = ((Timer) endpointMetricsHandler.getEndpointRequestsTimers() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY)) .getCount(); long prevResponseCount = ((Meter[]) endpointMetricsHandler.getEndpointResponsesMeters() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY))[4 - 1] long requestsCount = endpointMetricsHandler.getRequests().getCount(); long requestSize = metricsListener.getRequestSizes().getSnapshot().getMax(); long responseSize = metricsListener.getResponseSizes().getSnapshot().getMax(); long getRequestsCount = endpointMetricsHandler.getGetRequests().getCount(); long responsesCount = endpointMetricsHandler.getResponses()[4 - 1].getCount(); long requestCount = ((Timer) endpointMetricsHandler.getEndpointRequestsTimers() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY)) .getCount(); long responseCount = ((Meter[]) endpointMetricsHandler.getEndpointResponsesMeters() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY))[4 - 1] .getCount(); long postRequests = endpointMetricsHandler.getPostRequests().getCount(); long putRequests = endpointMetricsHandler.getPutRequests().getCount(); long deleteRequests = endpointMetricsHandler.getDeleteRequests().getCount(); long otherRequests = endpointMetricsHandler.getOtherRequests().getCount();
@Test public void setupEndpointsMetrics_throws_IllegalArgumentException_if_serverConfig_is_null() { // given EndpointMetricsHandlerDefaultImpl newImpl = new EndpointMetricsHandlerDefaultImpl(); // when Throwable ex = catchThrowable(() -> newImpl.setupEndpointsMetrics(null, metricRegistryMock)); // then assertThat(ex) .isInstanceOf(IllegalArgumentException.class) .hasMessage("ServerConfig cannot be null"); }
@Test public void one_arg_constructor_sets_up_instance_as_expected() { // given Supplier<Timer> timerSupplier = mock(Supplier.class); // when EndpointMetricsHandlerDefaultImpl newImpl = new EndpointMetricsHandlerDefaultImpl(timerSupplier); // then assertThat(newImpl.requestTimerGenerator).isSameAs(timerSupplier); }
Timer originalTimer = (Timer) invocation.callRealMethod(); return spy(originalTimer); }).when(instance).createAndRegisterRequestTimer(anyString(), any(MetricRegistry.class));
long prevFailedCount = metricsListener.getFailedRequests().getCount(); long prevProcessedCount = metricsListener.getProcessedRequests().getCount(); long prevRequestsCount = endpointMetricsHandler.getRequests().getCount(); long prevGetRequestsCount = endpointMetricsHandler.getGetRequests().getCount(); long prevResponsesCount = endpointMetricsHandler.getResponses()[4 - 1].getCount(); long prevRequestCount = ((Timer) endpointMetricsHandler.getEndpointRequestsTimers() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY)) .getCount(); long prevResponseCount = ((Meter[]) endpointMetricsHandler.getEndpointResponsesMeters() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY))[4 - 1] long requestsCount = endpointMetricsHandler.getRequests().getCount(); long requestSize = metricsListener.getRequestSizes().getSnapshot().getMax(); long responseSize = metricsListener.getResponseSizes().getSnapshot().getMax(); long getRequestsCount = endpointMetricsHandler.getGetRequests().getCount(); long responsesCount = endpointMetricsHandler.getResponses()[4 - 1].getCount(); long requestCount = ((Timer) endpointMetricsHandler.getEndpointRequestsTimers() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY)) .getCount(); long responseCount = ((Meter[]) endpointMetricsHandler.getEndpointResponsesMeters() .get(NON_BLOCKING_ENDPOINT_METRICS_LISTENER_MAP_KEY))[4 - 1] .getCount(); long postRequests = endpointMetricsHandler.getPostRequests().getCount(); long putRequests = endpointMetricsHandler.getPutRequests().getCount(); long deleteRequests = endpointMetricsHandler.getDeleteRequests().getCount(); long otherRequests = endpointMetricsHandler.getOtherRequests().getCount();
@Before public void beforeMethod() { instance = spy(new EndpointMetricsHandlerDefaultImpl()); state.setRequestStartTime(Instant.now()); instance.setupEndpointsMetrics(serverConfig, metricRegistryMock);
@Test public void default_constructor_sets_up_instance_as_expected() { // when EndpointMetricsHandlerDefaultImpl newImpl = new EndpointMetricsHandlerDefaultImpl(); // then assertThat(newImpl.requestTimerGenerator) .isSameAs(EndpointMetricsHandlerDefaultImpl.DEFAULT_REQUEST_TIMER_GENERATOR); }