diff --git a/spring-cloud-netflix-hystrix/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java b/spring-cloud-netflix-hystrix/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java index 20e6a6117..49e52a5b5 100644 --- a/spring-cloud-netflix-hystrix/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java +++ b/spring-cloud-netflix-hystrix/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java @@ -121,7 +121,7 @@ public class HystrixCircuitBreakerConfiguration { else if (value instanceof Map) { @SuppressWarnings("unchecked") Map sub = (Map) value; - addMetrics(sub, prefix); + addMetrics(sub, prefix + "." + key); } } } diff --git a/spring-cloud-netflix-hystrix/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixOnlyTests.java b/spring-cloud-netflix-hystrix/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixOnlyTests.java index 50b9d8f18..9f1b59915 100644 --- a/spring-cloud-netflix-hystrix/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixOnlyTests.java +++ b/spring-cloud-netflix-hystrix/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixOnlyTests.java @@ -18,6 +18,7 @@ package org.springframework.cloud.netflix.hystrix; import java.util.Base64; +import java.util.List; import java.util.Map; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @@ -50,6 +51,7 @@ import org.springframework.web.bind.annotation.RestController; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.cloud.netflix.test.TestAutoConfiguration.PASSWORD; import static org.springframework.cloud.netflix.test.TestAutoConfiguration.USER; @@ -59,7 +61,7 @@ import static org.springframework.cloud.netflix.test.TestAutoConfiguration.USER; */ @RunWith(SpringRunner.class) @SpringBootTest(classes = HystrixOnlyApplication.class, webEnvironment = RANDOM_PORT, - properties = "management.endpoint.health.show-details=ALWAYS") + properties = {"management.endpoint.health.show-details=ALWAYS"}) @DirtiesContext @ActiveProfiles("proxysecurity") public class HystrixOnlyTests { @@ -101,17 +103,45 @@ public class HystrixOnlyTests { map.containsKey("discovery")); } - private Map getHealth() { - ResponseEntity response = new TestRestTemplate().exchange( - "http://localhost:" + this.port + BASE_PATH + "/health", HttpMethod.GET, + @Test + public void testHystrixInnerMapMetrics() { + // We have to hit any Hystrix command before Hystrix metrics to be populated + String url = "http://localhost:" + this.port; + ResponseEntity response = new TestRestTemplate().getForEntity(url, + String.class); + assertEquals("bad response code", HttpStatus.OK, response.getStatusCode()); + + // Poller takes some time to realize for new metrics + try { + Thread.sleep(2000); + } catch (InterruptedException e) {} + + Map> map = (Map>) getMetrics(); + + assertTrue("There is no latencyTotal group key specified", + map.get("names").contains("hystrix.latency.total")); + assertTrue("There is no latencyExecute group key specified", + map.get("names").contains("hystrix.latency.execution")); + } + + + private Map getMetrics() { + return getAuthenticatedEndpoint("/metrics"); + } + + private Map getHealth() { + return getAuthenticatedEndpoint("/health"); + } + + private Map getAuthenticatedEndpoint(String endpoint) { + return new TestRestTemplate().exchange( + "http://localhost:" + this.port + BASE_PATH + endpoint, HttpMethod.GET, new HttpEntity(createBasicAuthHeader(USER, PASSWORD)), - Map.class); - Assertions.assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); - return response.getBody(); + Map.class).getBody(); } public static HttpHeaders createBasicAuthHeader(final String username, - final String password) { + final String password) { return new HttpHeaders() { private static final long serialVersionUID = 1766341693637204893L;