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

Fixed issue where TabularData would not be returned if the metrics do not have tags #406

Merged
merged 1 commit into from
Feb 3, 2023
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
23 changes: 13 additions & 10 deletions src/main/java/org/datadog/jmxfetch/JmxTabularAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,22 @@ protected String[] getTags(String key, String subAttribute)
Map<String, ?> attributeParams = getAttributesFor(fullMetricKey);
if (attributeParams != null) {
Map<String, String> yamlTags = (Map) attributeParams.get("tags");
for (String tagName : yamlTags.keySet()) {
String tag = tagName;
String value = yamlTags.get(tagName);
Object resolvedValue;

if (value.startsWith("$")) {
resolvedValue = getValue(key, value.substring(1));
if (resolvedValue != null) {
value = (String) resolvedValue;
if (yamlTags != null) {
for (String tagName : yamlTags.keySet()) {
String tag = tagName;
String value = yamlTags.get(tagName);
Object resolvedValue;

if (value.startsWith("$")) {
resolvedValue = getValue(key, value.substring(1));
if (resolvedValue != null) {
value = (String) resolvedValue;
}
}
}

tagsList.add(tag + ":" + value);
tagsList.add(tag + ":" + value);
}
}
}
String[] defaultTags = super.getTags();
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -990,5 +990,60 @@ public void testMetricError() throws Exception {

assertCoverage();
}
@Test
public void testTabularDataTagless() throws Exception {
// We expose a few metrics through JMX
SimpleTestJavaApp testApp = new SimpleTestJavaApp();
registerMBean(testApp, "org.datadog.jmxfetch.test:type=SimpleTestJavaApp");

// We do a first collection
when(appConfig.isTargetDirectInstances()).thenReturn(true);
initApplication("jmx_tabular_data_tagless.yaml");

run();
List<Map<String, Object>> metrics = getMetrics();

// 13 metrics from java.lang + 2 defined - 1 error
assertEquals(14, metrics.size());

List<String> tags = Arrays.asList(
"instance:jmx_test_instance",
"jmx_domain:org.datadog.jmxfetch.test",
"type:SimpleTestJavaApp",
"newTag:test"
);

assertMetric("multiattr.foo_tagless", 1.0, tags, -1);

assertCoverage();
}
@Test
public void testTabularDataTagged() throws Exception {
// We expose a few metrics through JMX
SimpleTestJavaApp testApp = new SimpleTestJavaApp();
registerMBean(testApp, "org.datadog.jmxfetch.test:type=SimpleTestJavaApp");

// We do a first collection
when(appConfig.isTargetDirectInstances()).thenReturn(true);
initApplication("jmx_tabular_data_tagged.yaml");

run();
List<Map<String, Object>> metrics = getMetrics();

// 13 metrics from java.lang + 2 defined - 1 error
assertEquals(14, metrics.size());

List<String> tags = Arrays.asList(
"instance:jmx_test_instance",
"jmx_domain:org.datadog.jmxfetch.test",
"type:SimpleTestJavaApp",
"foo:1",
"toto:tata",
"newTag:test"
);

assertMetric("multiattr.foo_tagged", 1.0, tags, -1);

assertCoverage();
}
}
22 changes: 22 additions & 0 deletions src/test/resources/jmx_tabular_data_tagged.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
init_config:

instances:
- jvm_direct: true
refresh_beans: 4
name: jmx_test_instance
tags:
- "env:stage"
- "newTag:test"
conf:
- include:
domain: org.datadog.jmxfetch.test
attribute:
Tabulardata.foo:
metric_type: gauge
alias: multiattr.foo_tagged
tags:
foo: $foo
toto: $toto
limit: 1
sort: desc
19 changes: 19 additions & 0 deletions src/test/resources/jmx_tabular_data_tagless.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
init_config:

instances:
- jvm_direct: true
refresh_beans: 4
name: jmx_test_instance
tags:
- "env:stage"
- "newTag:test"
conf:
- include:
domain: org.datadog.jmxfetch.test
attribute:
Tabulardata.foo:
metric_type: gauge
alias: multiattr.foo_tagless
limit: 1
sort: desc