Skip to content

Commit

Permalink
Allow to store hystrix commands metrics in the given root node
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Stoklosa committed May 26, 2017
1 parent dbc7429 commit 16c8606
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@
* Coda Hale Metrics (https://github.com/codahale/metrics) implementation of {@link HystrixMetricsPublisher}.
*/
public class HystrixCodaHaleMetricsPublisher extends HystrixMetricsPublisher {
private final String metricsRootNode;
private final MetricRegistry metricRegistry;

public HystrixCodaHaleMetricsPublisher(MetricRegistry metricRegistry) {
this(null, metricRegistry);
}

public HystrixCodaHaleMetricsPublisher(String metricsRootNode, MetricRegistry metricRegistry) {
this.metricsRootNode = metricsRootNode;
this.metricRegistry = metricRegistry;
}

@Override
public HystrixMetricsPublisherCommand getMetricsPublisherForCommand(HystrixCommandKey commandKey, HystrixCommandGroupKey commandGroupKey, HystrixCommandMetrics metrics, HystrixCircuitBreaker circuitBreaker, HystrixCommandProperties properties) {
return new HystrixCodaHaleMetricsPublisherCommand(commandKey, commandGroupKey, metrics, circuitBreaker, properties, metricRegistry);
return new HystrixCodaHaleMetricsPublisherCommand(metricsRootNode, commandKey, commandGroupKey, metrics, circuitBreaker, properties, metricRegistry);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Implementation of {@link HystrixMetricsPublisherCommand} using Coda Hale Metrics (https://github.com/codahale/metrics)
*/
public class HystrixCodaHaleMetricsPublisherCommand implements HystrixMetricsPublisherCommand {
private final String metricsRootNode;
private final HystrixCommandKey key;
private final HystrixCommandGroupKey commandGroupKey;
private final HystrixCommandMetrics metrics;
Expand All @@ -41,6 +42,11 @@ public class HystrixCodaHaleMetricsPublisherCommand implements HystrixMetricsPub
static final Logger logger = LoggerFactory.getLogger(HystrixCodaHaleMetricsPublisherCommand.class);

public HystrixCodaHaleMetricsPublisherCommand(HystrixCommandKey commandKey, HystrixCommandGroupKey commandGroupKey, HystrixCommandMetrics metrics, HystrixCircuitBreaker circuitBreaker, HystrixCommandProperties properties, MetricRegistry metricRegistry) {
this(null, commandKey, commandGroupKey, metrics, circuitBreaker, properties, metricRegistry);
}

public HystrixCodaHaleMetricsPublisherCommand(String metricsRootNode, HystrixCommandKey commandKey, HystrixCommandGroupKey commandGroupKey, HystrixCommandMetrics metrics, HystrixCircuitBreaker circuitBreaker, HystrixCommandProperties properties, MetricRegistry metricRegistry) {
this.metricsRootNode = metricsRootNode;
this.key = commandKey;
this.commandGroupKey = commandGroupKey;
this.metrics = metrics;
Expand Down Expand Up @@ -494,7 +500,7 @@ public Number getValue() {
}

protected String createMetricName(String name) {
return MetricRegistry.name(metricGroup, metricType, name);
return MetricRegistry.name(metricsRootNode, metricGroup, metricType, name);
}

protected void createCumulativeCountForEvent(final String name, final HystrixRollingNumberEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.netflix.hystrix.contrib.codahalemetricspublisher;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.strategy.HystrixPlugins;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Map;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

Expand All @@ -20,7 +16,7 @@ public class HystrixCodaHaleMetricsPublisherCommandTest {

@Before
public void setup() {
HystrixPlugins.getInstance().registerMetricsPublisher(new HystrixCodaHaleMetricsPublisher(metricRegistry));
HystrixPlugins.getInstance().registerMetricsPublisher(new HystrixCodaHaleMetricsPublisher("hystrix", metricRegistry));
}

@Test
Expand All @@ -30,13 +26,13 @@ public void testCommandSuccess() throws InterruptedException {

Thread.sleep(1000);

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

}

private static class Command extends HystrixCommand<Void> {
final static HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey("test");
final static HystrixCommandGroupKey hystrixCommandGroupKey = HystrixCommandGroupKey.Factory.asKey("test");
final static HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey("testCommand");
final static HystrixCommandGroupKey hystrixCommandGroupKey = HystrixCommandGroupKey.Factory.asKey("testGroup");

Command() {
super(Setter.withGroupKey(hystrixCommandGroupKey).andCommandKey(hystrixCommandKey));
Expand Down

0 comments on commit 16c8606

Please sign in to comment.