Skip to content

Commit

Permalink
Wildcard support on domains
Browse files Browse the repository at this point in the history
  • Loading branch information
olivielpeau committed May 26, 2015
1 parent f83b809 commit 60d80ae
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,24 @@ Object getJmxValue() throws AttributeNotFoundException, InstanceNotFoundExceptio

boolean matchDomain(Configuration conf) {
String includeDomain = conf.getInclude().getDomain();
return includeDomain == null || includeDomain.equals(domain);
return includeDomain == null
|| includeDomain.equals(domain) || this.matchDomainPrefix(includeDomain);
}

boolean excludeMatchDomain(Configuration conf) {
String excludeDomain = conf.getExclude().getDomain();
return excludeDomain != null && excludeDomain.equals(domain);
return excludeDomain != null
&& (excludeDomain.equals(domain) || this.matchDomainPrefix(excludeDomain));
}

private boolean matchDomainPrefix(String confDomain) {
if (confDomain.isEmpty() || confDomain.charAt(confDomain.length() - 1) != '*') {
return false;
}

String confDomainPrefix = confDomain.substring(0, confDomain.length() - 1);

return domain.equals(confDomainPrefix) || domain.startsWith(confDomainPrefix + ".");
}

boolean excludeMatchBean(Configuration conf) {
Expand Down
33 changes: 31 additions & 2 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,37 @@ public void testDomainExclude() throws Exception {
// First filter 14 = 13 metrics from java.lang + 2 metrics explicitly define- 1 implicitly defined in the exclude section
assertEquals(14, metrics.size());

mbs.unregisterMBean(includeMe);
mbs.unregisterMBean(excludeMe);
mbs.unregisterMBean(includeMe);
mbs.unregisterMBean(excludeMe);
}

@Test
public void testDomainWildcard() throws Exception {
// We expose a few metrics through JMX
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

ObjectName includeObjectName1 = new ObjectName("org.datadog.jmxfetch.includeme:type=AType");
ObjectName includeObjectName2 = new ObjectName("org.datadog.jmxfetch.includeme.too:type=AType");
ObjectName excludeObjectName = new ObjectName("org.datadog.jmxfetch.includeme.not.me:type=AType");
SimpleTestJavaApp testApp = new SimpleTestJavaApp();
mbs.registerMBean(testApp, includeObjectName1);
mbs.registerMBean(testApp, includeObjectName2);
mbs.registerMBean(testApp, excludeObjectName);

// Initializing application
AppConfig appConfig = new AppConfig();
App app = initApp("jmx_domain_wildcard.yaml", appConfig);

// Collecting metrics
app.doIteration();
LinkedList<HashMap<String, Object>> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics();

// First filter 15 = 13 metrics from java.lang + 3 metrics explicitly defined - 1 implicitly defined in exclude section
assertEquals(15, metrics.size());

mbs.unregisterMBean(includeObjectName1);
mbs.unregisterMBean(includeObjectName2);
mbs.unregisterMBean(excludeObjectName);
}

@Test
Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/jmx_domain_wildcard.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
init_config:

instances:
- process_name_regex: .*surefire.*
name: jmx_test_instance
conf:
- include:
domain: org.datadog.jmxfetch.includeme*
attribute:
ShouldBe100:
metric_type: gauge
alias: this.is.100
exclude:
domain: org.datadog.jmxfetch.includeme.not*

0 comments on commit 60d80ae

Please sign in to comment.