diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java index 20e6a6117..49e52a5b5 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java +++ b/spring-cloud-netflix-core/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-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixOnlyTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixOnlyTests.java index 8060de7d7..f5c5bde86 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixOnlyTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixOnlyTests.java @@ -17,6 +17,7 @@ package org.springframework.cloud.netflix.hystrix; import java.util.Base64; +import java.util.List; import java.util.Map; import org.junit.Test; @@ -33,6 +34,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; @@ -45,6 +47,7 @@ import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 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; @@ -54,7 +57,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 { @@ -96,15 +99,45 @@ public class HystrixOnlyTests { map.containsKey("discovery")); } - private Map getHealth() { + @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 + "/health", HttpMethod.GET, + "http://localhost:" + this.port + BASE_PATH + endpoint, HttpMethod.GET, new HttpEntity(createBasicAuthHeader(USER, PASSWORD)), 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;