Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get CodaHale metrics working by starting up the stream consumers #1586

Merged
merged 1 commit into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surely you can use await here instead of sleep from https://github.com/awaitility/awaitility
such as

Gauge<Long> successGauge = metricRegistry.getGauges().get("test.test.countSuccess");
await().atMost(1, SECONDS).until(successGauge::getValue, isEqualTo(1L));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could, but then I introduce an extra dependency for this specific test. You could argue that a utility like this should be applied across all of the unit tests. My personal opinions is that the current state isn't enhanced by that much by a wholesale change to this style, but I'm happy to be convinced otherwise


assertThat((Long) metricRegistry.getGauges().get("test.test.countSuccess").getValue(), is(1L));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ... should this also test the other gauges ?
how about a counter from each stream ?

RollingCommandEventCounterStream
RollingCommandLatencyDistributionStream
RollingCommandUserLatencyDistributionStream
RollingCommandMaxConcurrencyStream

Which stream to timeouts happen on ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, more test coverage here would be great. Would you like to submit a PR?


}

@Test
public void commandMaxActiveGauge() {
final HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey("test");
final HystrixCommandGroupKey hystrixCommandGroupKey = HystrixCommandGroupKey.Factory.asKey("test");

new HystrixCommand<Void>(HystrixCommand.Setter
.withGroupKey(hystrixCommandGroupKey)
.andCommandKey(hystrixCommandKey)) {
@Override
protected Void run() throws Exception {
return null;
}
}.execute();

for (Map.Entry<String, Gauge> entry : metricRegistry.getGauges().entrySet()) {
entry.getValue().getValue();
private static class Command extends HystrixCommand<Void> {
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;
}
}
}