From eb77bddde2f4aea6e890452213df3140de37d9d5 Mon Sep 17 00:00:00 2001 From: papapiya <402561078@qq.com> Date: Wed, 29 Jun 2022 21:17:33 +0800 Subject: [PATCH] fix: resolve jolokia2 panic on null response (#11397) --- plugins/inputs/jolokia2/common/gatherer.go | 8 +++++++- plugins/inputs/jolokia2/common/point_builder.go | 10 +++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/plugins/inputs/jolokia2/common/gatherer.go b/plugins/inputs/jolokia2/common/gatherer.go index 270e74730..5b426bb01 100644 --- a/plugins/inputs/jolokia2/common/gatherer.go +++ b/plugins/inputs/jolokia2/common/gatherer.go @@ -94,7 +94,13 @@ func (g *Gatherer) generatePoints(metric Metric, responses []ReadResponse) ([]po } pb := NewPointBuilder(metric, response.RequestAttributes, response.RequestPath) - for _, point := range pb.Build(metric.Mbean, response.Value) { + ps, err := pb.Build(metric.Mbean, response.Value) + if err != nil { + errors = append(errors, err) + continue + } + + for _, point := range ps { if response.RequestTarget != "" { point.Tags["jolokia_agent_url"] = response.RequestTarget } diff --git a/plugins/inputs/jolokia2/common/point_builder.go b/plugins/inputs/jolokia2/common/point_builder.go index ae45fdaa7..b094653eb 100644 --- a/plugins/inputs/jolokia2/common/point_builder.go +++ b/plugins/inputs/jolokia2/common/point_builder.go @@ -27,15 +27,15 @@ func NewPointBuilder(metric Metric, attributes []string, path string) *pointBuil } // Build generates a point for a given mbean name/pattern and value object. -func (pb *pointBuilder) Build(mbean string, value interface{}) []point { +func (pb *pointBuilder) Build(mbean string, value interface{}) ([]point, error) { hasPattern := strings.Contains(mbean, "*") - if !hasPattern { + if !hasPattern || value == nil { value = map[string]interface{}{mbean: value} } valueMap, ok := value.(map[string]interface{}) - if !ok { // FIXME: log it and move on. - panic(fmt.Sprintf("There should be a map here for %s!\n", mbean)) + if !ok { + return nil, fmt.Errorf("the response of %s's value should be a map", mbean) } points := make([]point, 0) @@ -46,7 +46,7 @@ func (pb *pointBuilder) Build(mbean string, value interface{}) []point { }) } - return compactPoints(points) + return compactPoints(points), nil } // extractTags generates the map of tags for a given mbean name/pattern.