Skip to content

Commit

Permalink
Ensure jolokia2 matches metrics against full read-response object
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanmei committed Jan 28, 2017
1 parent 6b5361e commit 5a51530
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 29 deletions.
4 changes: 2 additions & 2 deletions plugins/inputs/jolokia2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ through the Jolokia REST endpoint and its [JSON-over-HTTP protocol](https://jolo
paths = ["HeapMemoryUsage", "NonHeapMemoryUsage", "ObjectPendingFinalizationCount"]

# By default, all mbean keys are added as tags
# Use 'taginclude' to specify the exact tags to add.
# Use 'tag_keys' to specify the exact tags to add.
[[inputs.jolokia2.metric]]
name = "jvm_g1_garbage_collector"
mbean = "java.lang:name=G1*,type=GarbageCollector"
Expand All @@ -42,7 +42,7 @@ through the Jolokia REST endpoint and its [JSON-over-HTTP protocol](https://jolo
]
tag_keys = ["name"]

# Use 'tagexclude' to specify just the tags to remove.
# Use 'untag_keys' to specify just the tags to remove.
[[inputs.jolokia2.metric]]
name = "jvm_memory_pool"
mbean = "java.lang:name=*,type=MemoryPool"
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/jolokia2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ func (pb *pointBuilder) formatTagName(tagName string) string {
}

func (pb *pointBuilder) extractFields(value interface{}) map[string]interface{} {
fieldMap := make(map[string]interface{})
valueMap, ok := value.(map[string]interface{})

attributes := pb.request.Attributes
path := pb.request.Path

fieldMap := make(map[string]interface{})
valueMap, ok := value.(map[string]interface{})

if ok {
// complex value
if len(attributes) == 0 {
Expand Down
32 changes: 29 additions & 3 deletions plugins/inputs/jolokia2/gatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ func (g *Gatherer) gatherMetric(metric Metric, responses []ReadResponse, tags ma
for _, response := range responses {
request := response.Request

// FIXME: it takes more to correlate metrics and
// responses than an object name/pattern.
if metric.Mbean != request.Mbean {
if !metricMatchesRequest(metric, request) {
continue
}

Expand Down Expand Up @@ -85,3 +83,31 @@ func gatherTags(metricTags, outerTags map[string]string) map[string]string {

return tags
}

func metricMatchesRequest(metric Metric, request ReadRequest) bool {
if metric.Mbean != request.Mbean {
return false
}

if len(metric.Paths) == 0 {
return len(request.Attributes) == 0
}

for _, fullPath := range metric.Paths {
segments := strings.SplitN(fullPath, "/", 2)
attribute := segments[0]

var path string
if len(segments) == 2 {
path = segments[1]
}

for _, rattr := range request.Attributes {
if attribute == rattr {
return path == request.Path
}
}
}

return false
}
2 changes: 1 addition & 1 deletion plugins/inputs/jolokia2/jolokia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestJolokia2_ObjectValuesFixture(t *testing.T) {
}

func TestJolokia2_TagAndFieldCustomizations(t *testing.T) {
runFixture(t, "./testdata/tag_and_field_customizations.toml")
runFixture(t, "./testdata/tags_and_fields.toml")
}

func TestJolokia2_JvmFixture(t *testing.T) {
Expand Down
134 changes: 114 additions & 20 deletions plugins/inputs/jolokia2/testdata/object_values.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,48 @@ config = '''
default_tag_prefix = ""
[[inputs.jolokia2.metric]]
name = "object_value_without_attribute"
mbean = "object_value_without_attribute:foo=bar"
name = "object_without_attribute"
mbean = "object_without_attribute:foo=bar"
[[inputs.jolokia2.metric]]
name = "object_value_with_attribute"
mbean = "object_value_with_attribute:foo=bar"
name = "object_with_attribute"
mbean = "object_with_attribute:foo=bar"
paths = ["biz"]
[[inputs.jolokia2.metric]]
name = "object_value_with_attribute_and_path"
mbean = "object_value_with_attribute_and_path:foo=bar"
name = "object_with_attribute_and_path"
mbean = "object_with_attribute_and_path:foo=bar"
paths = ["biz/baz"]
[[inputs.jolokia2.metric]]
name = "object_value_with_pattern"
mbean = "object_value_with_pattern:test=*"
name = "object_with_pattern"
mbean = "object_with_pattern:test=*"
[[inputs.jolokia2.metric]]
name = "object_with_attribute_sharing_an_mbean_name1"
mbean = "object_with_attribute_sharing_an_mbean_name"
paths = ["foo"]
[[inputs.jolokia2.metric]]
name = "object_with_attribute_sharing_an_mbean_name2"
mbean = "object_with_attribute_sharing_an_mbean_name"
paths = ["bar"]
[[inputs.jolokia2.metric]]
name = "object_with_attribute_and_path_sharing_an_mbean_name1"
mbean = "object_with_attribute_and_path_sharing_an_mbean_name"
paths = ["foo/fiz"]
[[inputs.jolokia2.metric]]
name = "object_with_attribute_and_path_sharing_an_mbean_name2"
mbean = "object_with_attribute_and_path_sharing_an_mbean_name"
paths = ["bar/biz"]
'''

response = '''
[{
"request": {
"mbean": "object_value_without_attribute:foo=bar",
"mbean": "object_without_attribute:foo=bar",
"type": "read"
},
"value": {
Expand All @@ -35,7 +55,7 @@ response = '''
"status": 200
}, {
"request": {
"mbean": "object_value_with_attribute:foo=bar",
"mbean": "object_with_attribute:foo=bar",
"attribute": "biz",
"type": "read"
},
Expand All @@ -46,7 +66,7 @@ response = '''
"status": 200
}, {
"request": {
"mbean": "object_value_with_attribute_and_path:foo=bar",
"mbean": "object_with_attribute_and_path:foo=bar",
"attribute": "biz",
"path": "baz",
"type": "read"
Expand All @@ -58,23 +78,65 @@ response = '''
"status": 200
}, {
"request": {
"mbean": "object_value_with_pattern:test=*",
"mbean": "object_with_pattern:test=*",
"type": "read"
},
"value": {
"object_value_with_pattern:test=foo": {
"object_with_pattern:test=foo": {
"fiz": 123
},
"object_value_with_pattern:test=bar": {
"object_with_pattern:test=bar": {
"biz": 456
}
},
"status": 200
}]
}, {
"request": {
"mbean": "object_with_attribute_sharing_an_mbean_name",
"attribute": "foo",
"type": "read"
},
"value": {
"bing": 123
},
"status": 200
}, {
"request": {
"mbean": "object_with_attribute_sharing_an_mbean_name",
"attribute": "bar",
"type": "read"
},
"value": {
"fang": 456
},
"status": 200
}, {
"request": {
"mbean": "object_with_attribute_and_path_sharing_an_mbean_name",
"attribute": "foo",
"path": "fiz",
"type": "read"
},
"value": {
"bing": 123
},
"status": 200
}, {
"request": {
"mbean": "object_with_attribute_and_path_sharing_an_mbean_name",
"attribute": "bar",
"path": "biz",
"type": "read"
},
"value": {
"fang": 456
},
"status": 200
}]
'''

[[expects]]
measurement = "object_value_without_attribute"
measurement = "object_without_attribute"

[expects.tags]
foo = "bar"
Expand All @@ -84,7 +146,7 @@ response = '''
baz = 456.0

[[expects]]
measurement = "object_value_with_attribute"
measurement = "object_with_attribute"

[expects.tags]
foo = "bar"
Expand All @@ -94,7 +156,7 @@ response = '''
biz_faz = 456.0

[[expects]]
measurement = "object_value_with_attribute_and_path"
measurement = "object_with_attribute_and_path"

[expects.tags]
foo = "bar"
Expand All @@ -104,7 +166,7 @@ response = '''
biz_baz_bang = 456.0

[[expects]]
measurement = "object_value_with_pattern"
measurement = "object_with_pattern"

[expects.tags]
test = "foo"
Expand All @@ -113,10 +175,42 @@ response = '''
fiz = 123.0

[[expects]]
measurement = "object_value_with_pattern"
measurement = "object_with_pattern"

[expects.tags]
test = "bar"

[expects.fields]
biz = 456.0

[[expects]]
measurement = "object_with_attribute_sharing_an_mbean_name1"

[expects.tags]

[expects.fields]
foo_bing = 123.0

[[expects]]
measurement = "object_with_attribute_sharing_an_mbean_name2"

[expects.tags]

[expects.fields]
bar_fang = 456.0

[[expects]]
measurement = "object_with_attribute_and_path_sharing_an_mbean_name1"

[expects.tags]

[expects.fields]
foo_fiz_bing = 123.0

[[expects]]
measurement = "object_with_attribute_and_path_sharing_an_mbean_name2"

[expects.tags]

[expects.fields]
bar_biz_fang = 456.0

0 comments on commit 5a51530

Please sign in to comment.