From 2ad586baae2ca880042496c12cbac0a095f27b53 Mon Sep 17 00:00:00 2001 From: Matt Jacobs Date: Tue, 16 May 2017 12:45:19 -0700 Subject: [PATCH] Get CodaHale metrics working by starting up the stream consumers --- ...ystrixCodaHaleMetricsPublisherCommand.java | 13 +++--- ...ixCodaHaleMetricsPublisherCommandTest.java | 42 ++++++++++--------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/hystrix-contrib/hystrix-codahale-metrics-publisher/src/main/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommand.java b/hystrix-contrib/hystrix-codahale-metrics-publisher/src/main/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommand.java index 88bbef9c3..e743853d3 100644 --- a/hystrix-contrib/hystrix-codahale-metrics-publisher/src/main/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommand.java +++ b/hystrix-contrib/hystrix-codahale-metrics-publisher/src/main/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommand.java @@ -17,11 +17,8 @@ import com.codahale.metrics.Gauge; import com.codahale.metrics.MetricRegistry; -import com.netflix.hystrix.HystrixCircuitBreaker; -import com.netflix.hystrix.HystrixCommandGroupKey; -import com.netflix.hystrix.HystrixCommandKey; -import com.netflix.hystrix.HystrixCommandMetrics; -import com.netflix.hystrix.HystrixCommandProperties; +import com.netflix.hystrix.*; +import com.netflix.hystrix.metric.consumer.*; import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisherCommand; import com.netflix.hystrix.util.HystrixRollingNumberEvent; import org.slf4j.Logger; @@ -488,6 +485,12 @@ public Number getValue() { return properties.fallbackIsolationSemaphoreMaxConcurrentRequests().get(); } }); + + RollingCommandEventCounterStream.getInstance(key, properties).startCachingStreamValuesIfUnstarted(); + CumulativeCommandEventCounterStream.getInstance(key, properties).startCachingStreamValuesIfUnstarted(); + RollingCommandLatencyDistributionStream.getInstance(key, properties).startCachingStreamValuesIfUnstarted(); + RollingCommandUserLatencyDistributionStream.getInstance(key, properties).startCachingStreamValuesIfUnstarted(); + RollingCommandMaxConcurrencyStream.getInstance(key, properties).startCachingStreamValuesIfUnstarted(); } protected String createMetricName(String name) { diff --git a/hystrix-contrib/hystrix-codahale-metrics-publisher/src/test/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommandTest.java b/hystrix-contrib/hystrix-codahale-metrics-publisher/src/test/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommandTest.java index 9cc95e16f..003c21477 100644 --- a/hystrix-contrib/hystrix-codahale-metrics-publisher/src/test/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommandTest.java +++ b/hystrix-contrib/hystrix-codahale-metrics-publisher/src/test/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommandTest.java @@ -12,6 +12,9 @@ import java.util.Map; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + public class HystrixCodaHaleMetricsPublisherCommandTest { private final MetricRegistry metricRegistry = new MetricRegistry(); @@ -20,27 +23,28 @@ public void setup() { HystrixPlugins.getInstance().registerMetricsPublisher(new HystrixCodaHaleMetricsPublisher(metricRegistry)); } - @After - public void teardown() { - HystrixPlugins.reset(); + @Test + public void testCommandSuccess() throws InterruptedException { + Command command = new Command(); + command.execute(); + + Thread.sleep(1000); + + assertThat((Long) metricRegistry.getGauges().get("test.test.countSuccess").getValue(), is(1L)); + } - @Test - public void commandMaxActiveGauge() { - final HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey("test"); - final HystrixCommandGroupKey hystrixCommandGroupKey = HystrixCommandGroupKey.Factory.asKey("test"); - - new HystrixCommand(HystrixCommand.Setter - .withGroupKey(hystrixCommandGroupKey) - .andCommandKey(hystrixCommandKey)) { - @Override - protected Void run() throws Exception { - return null; - } - }.execute(); - - for (Map.Entry entry : metricRegistry.getGauges().entrySet()) { - entry.getValue().getValue(); + private static class Command extends HystrixCommand { + final static HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey("test"); + final static HystrixCommandGroupKey hystrixCommandGroupKey = HystrixCommandGroupKey.Factory.asKey("test"); + + Command() { + super(Setter.withGroupKey(hystrixCommandGroupKey).andCommandKey(hystrixCommandKey)); + } + + @Override + protected Void run() throws Exception { + return null; } } }