Skip to content

Commit 2ad586b

Browse files
committed
Get CodaHale metrics working by starting up the stream consumers
1 parent 338aadc commit 2ad586b

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

hystrix-contrib/hystrix-codahale-metrics-publisher/src/main/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommand.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717

1818
import com.codahale.metrics.Gauge;
1919
import com.codahale.metrics.MetricRegistry;
20-
import com.netflix.hystrix.HystrixCircuitBreaker;
21-
import com.netflix.hystrix.HystrixCommandGroupKey;
22-
import com.netflix.hystrix.HystrixCommandKey;
23-
import com.netflix.hystrix.HystrixCommandMetrics;
24-
import com.netflix.hystrix.HystrixCommandProperties;
20+
import com.netflix.hystrix.*;
21+
import com.netflix.hystrix.metric.consumer.*;
2522
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisherCommand;
2623
import com.netflix.hystrix.util.HystrixRollingNumberEvent;
2724
import org.slf4j.Logger;
@@ -488,6 +485,12 @@ public Number getValue() {
488485
return properties.fallbackIsolationSemaphoreMaxConcurrentRequests().get();
489486
}
490487
});
488+
489+
RollingCommandEventCounterStream.getInstance(key, properties).startCachingStreamValuesIfUnstarted();
490+
CumulativeCommandEventCounterStream.getInstance(key, properties).startCachingStreamValuesIfUnstarted();
491+
RollingCommandLatencyDistributionStream.getInstance(key, properties).startCachingStreamValuesIfUnstarted();
492+
RollingCommandUserLatencyDistributionStream.getInstance(key, properties).startCachingStreamValuesIfUnstarted();
493+
RollingCommandMaxConcurrencyStream.getInstance(key, properties).startCachingStreamValuesIfUnstarted();
491494
}
492495

493496
protected String createMetricName(String name) {

hystrix-contrib/hystrix-codahale-metrics-publisher/src/test/java/com/netflix/hystrix/contrib/codahalemetricspublisher/HystrixCodaHaleMetricsPublisherCommandTest.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
import java.util.Map;
1414

15+
import static org.hamcrest.core.Is.is;
16+
import static org.junit.Assert.assertThat;
17+
1518
public class HystrixCodaHaleMetricsPublisherCommandTest {
1619
private final MetricRegistry metricRegistry = new MetricRegistry();
1720

@@ -20,27 +23,28 @@ public void setup() {
2023
HystrixPlugins.getInstance().registerMetricsPublisher(new HystrixCodaHaleMetricsPublisher(metricRegistry));
2124
}
2225

23-
@After
24-
public void teardown() {
25-
HystrixPlugins.reset();
26+
@Test
27+
public void testCommandSuccess() throws InterruptedException {
28+
Command command = new Command();
29+
command.execute();
30+
31+
Thread.sleep(1000);
32+
33+
assertThat((Long) metricRegistry.getGauges().get("test.test.countSuccess").getValue(), is(1L));
34+
2635
}
2736

28-
@Test
29-
public void commandMaxActiveGauge() {
30-
final HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey("test");
31-
final HystrixCommandGroupKey hystrixCommandGroupKey = HystrixCommandGroupKey.Factory.asKey("test");
32-
33-
new HystrixCommand<Void>(HystrixCommand.Setter
34-
.withGroupKey(hystrixCommandGroupKey)
35-
.andCommandKey(hystrixCommandKey)) {
36-
@Override
37-
protected Void run() throws Exception {
38-
return null;
39-
}
40-
}.execute();
41-
42-
for (Map.Entry<String, Gauge> entry : metricRegistry.getGauges().entrySet()) {
43-
entry.getValue().getValue();
37+
private static class Command extends HystrixCommand<Void> {
38+
final static HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey("test");
39+
final static HystrixCommandGroupKey hystrixCommandGroupKey = HystrixCommandGroupKey.Factory.asKey("test");
40+
41+
Command() {
42+
super(Setter.withGroupKey(hystrixCommandGroupKey).andCommandKey(hystrixCommandKey));
43+
}
44+
45+
@Override
46+
protected Void run() throws Exception {
47+
return null;
4448
}
4549
}
4650
}

0 commit comments

Comments
 (0)