Skip to content

Commit eb77bdd

Browse files
authored
fix: resolve jolokia2 panic on null response (#11397)
1 parent b89a254 commit eb77bdd

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

plugins/inputs/jolokia2/common/gatherer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ func (g *Gatherer) generatePoints(metric Metric, responses []ReadResponse) ([]po
9494
}
9595

9696
pb := NewPointBuilder(metric, response.RequestAttributes, response.RequestPath)
97-
for _, point := range pb.Build(metric.Mbean, response.Value) {
97+
ps, err := pb.Build(metric.Mbean, response.Value)
98+
if err != nil {
99+
errors = append(errors, err)
100+
continue
101+
}
102+
103+
for _, point := range ps {
98104
if response.RequestTarget != "" {
99105
point.Tags["jolokia_agent_url"] = response.RequestTarget
100106
}

plugins/inputs/jolokia2/common/point_builder.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func NewPointBuilder(metric Metric, attributes []string, path string) *pointBuil
2727
}
2828

2929
// Build generates a point for a given mbean name/pattern and value object.
30-
func (pb *pointBuilder) Build(mbean string, value interface{}) []point {
30+
func (pb *pointBuilder) Build(mbean string, value interface{}) ([]point, error) {
3131
hasPattern := strings.Contains(mbean, "*")
32-
if !hasPattern {
32+
if !hasPattern || value == nil {
3333
value = map[string]interface{}{mbean: value}
3434
}
3535

3636
valueMap, ok := value.(map[string]interface{})
37-
if !ok { // FIXME: log it and move on.
38-
panic(fmt.Sprintf("There should be a map here for %s!\n", mbean))
37+
if !ok {
38+
return nil, fmt.Errorf("the response of %s's value should be a map", mbean)
3939
}
4040

4141
points := make([]point, 0)
@@ -46,7 +46,7 @@ func (pb *pointBuilder) Build(mbean string, value interface{}) []point {
4646
})
4747
}
4848

49-
return compactPoints(points)
49+
return compactPoints(points), nil
5050
}
5151

5252
// extractTags generates the map of tags for a given mbean name/pattern.

0 commit comments

Comments
 (0)