Skip to content

Commit

Permalink
Merge pull request #269 from ofek/service-tag
Browse files Browse the repository at this point in the history
Support sending a `service` tag
  • Loading branch information
remeh authored Feb 17, 2020
2 parents db4acec + b500d36 commit 6c67ab1
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public Yaml initialValue() {
private Map<String, Object> instanceMap;
private Map<String, Object> initConfig;
private String instanceName;
private String service;
private Map<String, String> tags;
private String checkName;
private int maxReturnedMetrics;
Expand Down Expand Up @@ -140,6 +141,14 @@ public Instance(
this.refreshBeansPeriod = appConfig.getRefreshBeansPeriod();
}

this.service = (String) instanceMap.get("service");
if ((this.service == null || this.service.isEmpty()) && initConfig != null) {
this.service = (String) initConfig.get("service");
}
if (this.service != null && !this.service.isEmpty()) {
this.tags.put("service", this.service);
}

this.minCollectionPeriod = (Integer) instanceMap.get("min_collection_interval");
if (this.minCollectionPeriod == null && initConfig != null) {
this.minCollectionPeriod = (Integer) initConfig.get("min_collection_interval");
Expand Down Expand Up @@ -667,6 +676,10 @@ public String[] getServiceCheckTags() {
}
tags.add("instance:" + this.instanceName);

if (this.service != null && !this.service.isEmpty()) {
tags.add("service:" + this.service);
}

if (this.emptyDefaultHostname) {
tags.add("host:");
}
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/org/datadog/jmxfetch/TestInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,53 @@ public void testEmptyDefaultHostname() throws Exception {
}
}

// assertServiceTag is used by testServiceTagGlobal and testServiceTagInstanceOverride
private void assertServiceTag(List<String> tagList, String service) throws Exception {
assertTrue(tagList.contains(new String("service:" + service)));
}

@Test
public void testServiceTagGlobal() throws Exception {
registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:foo=Bar,qux=Baz");
initApplication("jmx_service_tag_global.yaml");
run();

LinkedList<HashMap<String, Object>> metrics = getMetrics();
assertEquals(28, metrics.size());
for (HashMap<String, Object> metric : metrics) {
String[] tags = (String[]) metric.get("tags");
this.assertServiceTag(Arrays.asList(tags), "global");
}

LinkedList<HashMap<String, Object>> serviceChecks = getServiceChecks();
assertEquals(2, serviceChecks.size());
for (HashMap<String, Object> sc : serviceChecks) {
String[] tags = (String[]) sc.get("tags");
this.assertServiceTag(Arrays.asList(tags), "global");
}
}

@Test
public void testServiceTagInstanceOverride() throws Exception {
registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:foo=Bar,qux=Baz");
initApplication("jmx_service_tag_instance_override.yaml");
run();

LinkedList<HashMap<String, Object>> metrics = getMetrics();
assertEquals(28, metrics.size());
for (HashMap<String, Object> metric : metrics) {
String[] tags = (String[]) metric.get("tags");
this.assertServiceTag(Arrays.asList(tags), "override");
}

LinkedList<HashMap<String, Object>> serviceChecks = getServiceChecks();
assertEquals(2, serviceChecks.size());
for (HashMap<String, Object> sc : serviceChecks) {
String[] tags = (String[]) sc.get("tags");
this.assertServiceTag(Arrays.asList(tags), "override");
}
}

@Test
public void testLoadMetricConfigFiles() throws Exception {
URL defaultConfig = Instance.class.getResource("default-jmx-metrics.yaml");
Expand Down
27 changes: 27 additions & 0 deletions src/test/resources/jmx_service_tag_global.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
init_config:
service: global

instances:
- process_name_regex: .*surefire.*
name: jmx_test_1
tags:
- jmx:fetch
conf:
- include:
domain: org.datadog.jmxfetch.test
attribute:
ShouldBe100:
metric_type: gauge
alias: this.is.100.$foo.$qux

- process_name_regex: .*surefire.*
name: jmx_test_2
tags:
- jmx:fetch
conf:
- include:
domain: org.datadog.jmxfetch.test
attribute:
ShouldBe100:
metric_type: gauge
alias: this.is.100.$foo.$qux
29 changes: 29 additions & 0 deletions src/test/resources/jmx_service_tag_instance_override.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
init_config:
service: global

instances:
- process_name_regex: .*surefire.*
name: jmx_test_1
service: override
tags:
- jmx:fetch
conf:
- include:
domain: org.datadog.jmxfetch.test
attribute:
ShouldBe100:
metric_type: gauge
alias: this.is.100.$foo.$qux

- process_name_regex: .*surefire.*
name: jmx_test_2
service: override
tags:
- jmx:fetch
conf:
- include:
domain: org.datadog.jmxfetch.test
attribute:
ShouldBe100:
metric_type: gauge
alias: this.is.100.$foo.$qux

0 comments on commit 6c67ab1

Please sign in to comment.