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

Add debug to Jolokia JMX metricset #4341

Merged
merged 1 commit into from
May 17, 2017
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
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