fix: resolve jolokia2 panic on null response (#11397)

This commit is contained in:
papapiya 2022-06-29 21:17:33 +08:00 committed by GitHub
parent b89a254ace
commit eb77bddde2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 6 deletions

View File

@ -94,7 +94,13 @@ func (g *Gatherer) generatePoints(metric Metric, responses []ReadResponse) ([]po
} }
pb := NewPointBuilder(metric, response.RequestAttributes, response.RequestPath) 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 != "" { if response.RequestTarget != "" {
point.Tags["jolokia_agent_url"] = response.RequestTarget point.Tags["jolokia_agent_url"] = response.RequestTarget
} }

View File

@ -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. // 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, "*") hasPattern := strings.Contains(mbean, "*")
if !hasPattern { if !hasPattern || value == nil {
value = map[string]interface{}{mbean: value} value = map[string]interface{}{mbean: value}
} }
valueMap, ok := value.(map[string]interface{}) valueMap, ok := value.(map[string]interface{})
if !ok { // FIXME: log it and move on. if !ok {
panic(fmt.Sprintf("There should be a map here for %s!\n", mbean)) return nil, fmt.Errorf("the response of %s's value should be a map", mbean)
} }
points := make([]point, 0) 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. // extractTags generates the map of tags for a given mbean name/pattern.