From b2427b9712cbaa911e956eed156950b31bb22681 Mon Sep 17 00:00:00 2001 From: Andrew Kroh Date: Wed, 17 May 2017 14:37:18 -0400 Subject: [PATCH] Add debug to Jolokia JMX metricset Add debug logging of the JSON request and responses. Log the JSON body in errors if unmarshalling fails. --- CHANGELOG.asciidoc | 1 + metricbeat/module/jolokia/jmx/config.go | 4 +--- metricbeat/module/jolokia/jmx/data.go | 23 ++++++++++++----------- metricbeat/module/jolokia/jmx/jmx.go | 16 ++++++++++++++-- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 6e375f380f7..72f3ebbbff2 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -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* diff --git a/metricbeat/module/jolokia/jmx/config.go b/metricbeat/module/jolokia/jmx/config.go index 26a263e6e97..a711f3059d6 100644 --- a/metricbeat/module/jolokia/jmx/config.go +++ b/metricbeat/module/jolokia/jmx/config.go @@ -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, diff --git a/metricbeat/module/jolokia/jmx/data.go b/metricbeat/module/jolokia/jmx/data.go index 37ca5b5d879..00d4bf73e33 100644 --- a/metricbeat/module/jolokia/jmx/data.go +++ b/metricbeat/module/jolokia/jmx/data.go @@ -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 { @@ -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{} @@ -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) diff --git a/metricbeat/module/jolokia/jmx/jmx.go b/metricbeat/module/jolokia/jmx/jmx.go index 21155cbacbb..ec2f5cd345d 100644 --- a/metricbeat/module/jolokia/jmx/jmx.go +++ b/metricbeat/module/jolokia/jmx/jmx.go @@ -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. @@ -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, @@ -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