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

Support sending a service tag #269

Merged
merged 3 commits into from
Feb 17, 2020
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
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