Skip to content

Commit

Permalink
fix: resolve jolokia2 panic on null response (#11397)
Browse files Browse the repository at this point in the history
(cherry picked from commit eb77bdd)
  • Loading branch information
PapaPiya authored and Sven Rebhan committed Jul 5, 2022
1 parent c675bd0 commit fc76cbb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 7 additions & 1 deletion plugins/inputs/jolokia2/common/gatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions plugins/inputs/jolokia2/common/point_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down

0 comments on commit fc76cbb

Please sign in to comment.