From 2fffb2606e064470f1f379076b55322bd9cfe0bf Mon Sep 17 00:00:00 2001 From: ivansjg Date: Mon, 17 Dec 2018 16:08:19 +0100 Subject: [PATCH] =?UTF-8?q?Inner=20map=20metrics=20with=20same=20name=20ar?= =?UTF-8?q?e=20being=20overwritten=20on=20actuator=20me=E2=80=A6=20(#3317)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Inner map metrics with same name are being overwritten on actuator metrics endpoint * Add Hystrix Inner map metrics tests --- .../HystrixCircuitBreakerConfiguration.java | 2 +- .../netflix/hystrix/HystrixOnlyTests.java | 37 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) 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 8be0d8c6f..94dec7ff1 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 @@ -158,7 +158,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 799c8ac03..3be0a6d2f 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 @@ -38,6 +38,8 @@ 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.junit4.SpringJUnit4ClassRunner; import org.springframework.web.bind.annotation.RequestMapping; @@ -45,6 +47,7 @@ import org.springframework.web.bind.annotation.RestController; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; + /** * @author Spencer Gibb */ @@ -55,7 +58,7 @@ public class HystrixOnlyTests { @Value("${local.server.port}") private int port; - + @Value("${security.user.username}") private String username; @@ -92,17 +95,45 @@ public class HystrixOnlyTests { map.containsKey("discovery")); } + @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 = getMetrics(); + + assertTrue("There is no latencyTotal group key specified", + map.containsKey("gauge.servo.hystrix.hystrixcommand.service.hello.latencytotal.75")); + assertTrue("There is no latencyExecute group key specified", + map.containsKey("gauge.servo.hystrix.hystrixcommand.service.hello.latencyexecute.75")); + } + + + private Map getMetrics() { + return getAuthenticatedEndpoint("/admin/metrics"); + } private Map getHealth() { + return getAuthenticatedEndpoint("/admin/health"); + } + + private Map getAuthenticatedEndpoint(String endpoint) { return new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/admin/health", HttpMethod.GET, + "http://localhost:" + this.port + endpoint, HttpMethod.GET, new HttpEntity(createBasicAuthHeader(username, 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;