Skip to content

Commit

Permalink
Add debug to Jolokia JMX metricset (#4341)
Browse files Browse the repository at this point in the history
Add debug logging of the JSON request and responses.

Log the JSON body in errors if unmarshalling fails.
  • Loading branch information
andrewkroh authored and tsg committed May 17, 2017
1 parent 8bec232 commit 03690a8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ https://github.com/elastic/beats/compare/v6.0.0-alpha1...master[Check the HEAD d
- Add macOS implementation of the system diskio metricset. {issue}4144[4144]
- Add process_summary metricset that records high level metrics about processes. {pull}4231[4231]
- Add `kube-state-metrics` based metrics to `kubernetes` module {pull}4253[4253]
- Add debug logging to Jolokia JMX metricset. {pull}4341[4341]

*Packetbeat*

Expand Down
4 changes: 1 addition & 3 deletions metricbeat/module/jolokia/jmx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ type RequestBlock struct {
}

func buildRequestBodyAndMapping(mappings []JMXMapping) ([]byte, map[string]string, error) {

responseMapping := map[string]string{}
blocks := []RequestBlock{}
var blocks []RequestBlock

for _, mapping := range mappings {

rb := RequestBlock{
Type: "read",
MBean: mapping.MBean,
Expand Down
23 changes: 12 additions & 11 deletions metricbeat/module/jolokia/jmx/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package jmx

import (
"encoding/json"
"fmt"

"github.com/elastic/beats/libbeat/common"
"github.com/joeshaw/multierror"
"github.com/pkg/errors"
)

type Entry struct {
Expand Down Expand Up @@ -47,11 +47,9 @@ type Entry struct {
// }
// ]
func eventMapping(content []byte, mapping map[string]string) (common.MapStr, error) {

var entries []Entry
err := json.Unmarshal(content, &entries)
if err != nil {
return nil, fmt.Errorf("Cannot unmarshal json response: %s", err)
if err := json.Unmarshal(content, &entries); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal jolokia JSON response '%v'", string(content))
}

event := common.MapStr{}
Expand All @@ -68,18 +66,21 @@ func eventMapping(content []byte, mapping map[string]string) (common.MapStr, err
}

return event, errs.Err()

}

func parseResponseEntry(mbeanName string, attributeName string, attibuteValue interface{},
event common.MapStr, mapping map[string]string) error {

//create metric name by merging mbean and attribute fields
func parseResponseEntry(
mbeanName string,
attributeName string,
attibuteValue interface{},
event common.MapStr,
mapping map[string]string,
) error {
// Create metric name by merging mbean and attribute fields.
var metricName = mbeanName + "_" + attributeName

key, exists := mapping[metricName]
if !exists {
return fmt.Errorf("No key found for metric: '%s', skipping...", metricName)
return errors.Errorf("metric key '%v' not found in response", key)
}

_, err := event.Put(key, attibuteValue)
Expand Down
16 changes: 14 additions & 2 deletions metricbeat/module/jolokia/jmx/jmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
)

var (
debugf = logp.MakeDebug("jolokia-jmx")
metricsetName = "jolokia.jmx"
logPrefix = "[" + metricsetName + "]"
debugf = logp.MakeDebug(metricsetName)
)

// init registers the MetricSet with the central registry.
Expand Down Expand Up @@ -67,6 +69,11 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
http.SetMethod("POST")
http.SetBody(body)

if logp.IsDebug(metricsetName) {
debugf("%v The body for POST requests to jolokia host %v is: %v",
logPrefix, base.HostData().Host, string(body))
}

return &MetricSet{
BaseMetricSet: base,
mapping: mapping,
Expand All @@ -83,12 +90,17 @@ func (m *MetricSet) Fetch() (common.MapStr, error) {
return nil, err
}

if logp.IsDebug(metricsetName) {
debugf("%v The response body from jolokia host %v is: %v",
logPrefix, m.HostData().Host, string(body))
}

event, err := eventMapping(body, m.mapping)
if err != nil {
return nil, err
}

// Set dynamic namespace
// Set dynamic namespace.
event[mb.NamespaceKey] = m.namespace

return event, nil
Expand Down

0 comments on commit 03690a8

Please sign in to comment.