forked from Netflix/Hystrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement configurable Codahale MetricsFilter based on Archaius Prope…
…rty Manager
- Loading branch information
Simon Irving
committed
Jul 2, 2014
1 parent
908a9a0
commit c83a7a1
Showing
3 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...om/netflix/hystrix/contrib/codahalemetricspublisher/ConfigurableCodaHaleMetricFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.netflix.hystrix.contrib.codahalemetricspublisher; | ||
|
||
import com.codahale.metrics.Metric; | ||
import com.codahale.metrics.MetricFilter; | ||
import com.netflix.config.DynamicPropertyFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* An implementation of @MetricFilter based upon an Archaius DynamicPropertyFactory | ||
* | ||
* To enable this filter, the property 'filter.graphite,metrics' must be set to TRUE | ||
* | ||
* If this is the case, metrics will be filtered unless METRIC_NAME = true is set in | ||
* the properties | ||
* | ||
* | ||
* eg HystrixCommand.IndiciaService.GetIndicia.countFailure = true | ||
* | ||
* | ||
* For detail on how the metric names are constructed, refer to the source of the | ||
* | ||
* {@link HystrixCodaHaleMetricsPublisherCommand} | ||
* | ||
* and | ||
* | ||
* {@link HystrixCodaHaleMetricsPublisherThreadPool} | ||
* | ||
* classes. | ||
* | ||
* @author Simon Irving | ||
*/ | ||
public class ConfigurableCodaHaleMetricFilter implements MetricFilter{ | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurableCodaHaleMetricFilter.class); | ||
|
||
private DynamicPropertyFactory archaiusPropertyFactory; | ||
|
||
|
||
public ConfigurableCodaHaleMetricFilter(DynamicPropertyFactory archaiusPropertyFactory) | ||
{ | ||
this.archaiusPropertyFactory = archaiusPropertyFactory; | ||
} | ||
|
||
@Override | ||
public boolean matches(String s, Metric metric) { | ||
|
||
if (!isFilterEnabled()) | ||
{ | ||
return true; | ||
} | ||
|
||
boolean matchesFilter = archaiusPropertyFactory.getBooleanProperty(s, false).get(); | ||
|
||
LOGGER.debug("Does metric [{}] match filter? [{}]",s,matchesFilter); | ||
|
||
return matchesFilter; | ||
} | ||
|
||
protected boolean isFilterEnabled() { | ||
|
||
boolean filterEnabled = archaiusPropertyFactory.getBooleanProperty("filter.graphite,metrics", false).get(); | ||
|
||
LOGGER.debug("Is filter enabled? [{}]", filterEnabled); | ||
|
||
return filterEnabled; | ||
} | ||
|
||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...etflix/hystrix/contrib/codahalemetricspublisher/ConfigurableCodaHaleMetricFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.netflix.hystrix.contrib.codahalemetricspublisher; | ||
|
||
import com.codahale.metrics.Metric; | ||
import com.netflix.config.DynamicBooleanProperty; | ||
import com.netflix.config.DynamicPropertyFactory; | ||
import org.junit.After; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* Test the ConfigurableCodaHaleMetricFilter | ||
* | ||
* @author Simon Irving | ||
*/ | ||
public class ConfigurableCodaHaleMetricFilterTest { | ||
|
||
private Metric metric = mock(Metric.class); | ||
|
||
private final static DynamicPropertyFactory archiausPropertyFactory = mock(DynamicPropertyFactory.class); | ||
|
||
private static final DynamicBooleanProperty DYNAMIC_BOOLEAN_TRUE = mock(DynamicBooleanProperty.class); | ||
private static final DynamicBooleanProperty DYNAMIC_BOOLEAN_FALSE = mock(DynamicBooleanProperty.class); | ||
|
||
@BeforeClass | ||
public static void initialiseMocks() | ||
{ | ||
when(archiausPropertyFactory.getBooleanProperty(any(String.class), any(Boolean.class))).thenReturn(DYNAMIC_BOOLEAN_FALSE); | ||
when(archiausPropertyFactory.getBooleanProperty(eq("this.metric.is.allowed"), any(Boolean.class))).thenReturn(DYNAMIC_BOOLEAN_TRUE); | ||
when(DYNAMIC_BOOLEAN_TRUE.get()).thenReturn(true); | ||
when(DYNAMIC_BOOLEAN_FALSE.get()).thenReturn(false); | ||
} | ||
|
||
@After | ||
public void assertMetricsNotTouched() | ||
{ | ||
verifyZeroInteractions(metric); | ||
} | ||
|
||
@Test | ||
public void testMetricConfiguredInFilterWithFilterEnabled() | ||
{ | ||
when(archiausPropertyFactory.getBooleanProperty(eq("filter.graphite,metrics"), any(Boolean.class))).thenReturn(DYNAMIC_BOOLEAN_TRUE); | ||
ConfigurableCodaHaleMetricFilter filter = new ConfigurableCodaHaleMetricFilter(archiausPropertyFactory); | ||
assertTrue(filter.matches("this.metric.is.allowed", metric)); | ||
} | ||
|
||
@Test | ||
public void testMetricConfiguredInFilterWithFilterDisabled() | ||
{ | ||
when(archiausPropertyFactory.getBooleanProperty(eq("filter.graphite,metrics"), any(Boolean.class))).thenReturn(DYNAMIC_BOOLEAN_FALSE); | ||
ConfigurableCodaHaleMetricFilter filter = new ConfigurableCodaHaleMetricFilter(archiausPropertyFactory); | ||
assertTrue(filter.matches("this.metric.is.allowed", metric)); | ||
} | ||
|
||
@Test | ||
public void testMetricNotConfiguredInFilterWithFilterEnabled() | ||
{ | ||
when(archiausPropertyFactory.getBooleanProperty(eq("filter.graphite,metrics"), any(Boolean.class))).thenReturn(DYNAMIC_BOOLEAN_TRUE); | ||
ConfigurableCodaHaleMetricFilter filter = new ConfigurableCodaHaleMetricFilter(archiausPropertyFactory); | ||
assertFalse(filter.matches("this.metric.is.not.allowed", metric)); | ||
} | ||
|
||
@Test | ||
public void testMetricNotConfiguredInFilterWithFilterDisabled() | ||
{ | ||
when(archiausPropertyFactory.getBooleanProperty(eq("filter.graphite,metrics"), any(Boolean.class))).thenReturn(DYNAMIC_BOOLEAN_FALSE); | ||
ConfigurableCodaHaleMetricFilter filter = new ConfigurableCodaHaleMetricFilter(archiausPropertyFactory); | ||
assertTrue(filter.matches("this.metric.is.not.allowed", metric)); | ||
} | ||
} |