Fix index out of bounds error in solr input plugin (#3683)

This commit is contained in:
Michael Boudreau 2018-01-17 14:37:34 -08:00 committed by Daniel Nelson
parent 4da881d646
commit 20f5741d44
2 changed files with 55 additions and 0 deletions

View File

@ -246,6 +246,9 @@ func addAdminCoresStatusToAcc(acc telegraf.Accumulator, adminCoreStatus *AdminCo
// Add core metrics section to accumulator // Add core metrics section to accumulator
func addCoreMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error { func addCoreMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error {
var coreMetrics map[string]Core var coreMetrics map[string]Core
if len(mBeansData.SolrMbeans) < 2 {
return fmt.Errorf("no core metric data to unmarshall")
}
if err := json.Unmarshal(mBeansData.SolrMbeans[1], &coreMetrics); err != nil { if err := json.Unmarshal(mBeansData.SolrMbeans[1], &coreMetrics); err != nil {
return err return err
} }
@ -274,9 +277,14 @@ func addCoreMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBea
func addQueryHandlerMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error { func addQueryHandlerMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error {
var queryMetrics map[string]QueryHandler var queryMetrics map[string]QueryHandler
if len(mBeansData.SolrMbeans) < 4 {
return fmt.Errorf("no query handler metric data to unmarshall")
}
if err := json.Unmarshal(mBeansData.SolrMbeans[3], &queryMetrics); err != nil { if err := json.Unmarshal(mBeansData.SolrMbeans[3], &queryMetrics); err != nil {
return err return err
} }
for name, metrics := range queryMetrics { for name, metrics := range queryMetrics {
coreFields := map[string]interface{}{ coreFields := map[string]interface{}{
"15min_rate_reqs_per_second": metrics.Stats.One5minRateReqsPerSecond, "15min_rate_reqs_per_second": metrics.Stats.One5minRateReqsPerSecond,
@ -310,6 +318,9 @@ func addQueryHandlerMetricsToAcc(acc telegraf.Accumulator, core string, mBeansDa
func addUpdateHandlerMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error { func addUpdateHandlerMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error {
var updateMetrics map[string]UpdateHandler var updateMetrics map[string]UpdateHandler
if len(mBeansData.SolrMbeans) < 6 {
return fmt.Errorf("no update handler metric data to unmarshall")
}
if err := json.Unmarshal(mBeansData.SolrMbeans[5], &updateMetrics); err != nil { if err := json.Unmarshal(mBeansData.SolrMbeans[5], &updateMetrics); err != nil {
return err return err
} }
@ -364,6 +375,9 @@ func getFloat(unk interface{}) float64 {
// Add cache metrics section to accumulator // Add cache metrics section to accumulator
func addCacheMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error { func addCacheMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error {
if len(mBeansData.SolrMbeans) < 8 {
return fmt.Errorf("no cache metric data to unmarshall")
}
var cacheMetrics map[string]Cache var cacheMetrics map[string]Cache
if err := json.Unmarshal(mBeansData.SolrMbeans[7], &cacheMetrics); err != nil { if err := json.Unmarshal(mBeansData.SolrMbeans[7], &cacheMetrics); err != nil {
return err return err

View File

@ -60,3 +60,44 @@ func createMockServer() *httptest.Server {
} }
})) }))
} }
func TestNoCoreDataHandling(t *testing.T) {
ts := createMockNoCoreDataServer()
solr := NewSolr()
solr.Servers = []string{ts.URL}
var acc testutil.Accumulator
require.NoError(t, solr.Gather(&acc))
acc.AssertContainsTaggedFields(t, "solr_admin",
solrAdminMainCoreStatusExpected,
map[string]string{"core": "main"})
acc.AssertContainsTaggedFields(t, "solr_admin",
solrAdminCore1StatusExpected,
map[string]string{"core": "core1"})
acc.AssertDoesNotContainMeasurement(t, "solr_core")
acc.AssertDoesNotContainMeasurement(t, "solr_queryhandler")
acc.AssertDoesNotContainMeasurement(t, "solr_updatehandler")
acc.AssertDoesNotContainMeasurement(t, "solr_handler")
}
func createMockNoCoreDataServer() *httptest.Server {
var nodata string
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "/solr/admin/cores") {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, statusResponse)
} else if strings.Contains(r.URL.Path, "solr/main/admin") {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, nodata)
} else if strings.Contains(r.URL.Path, "solr/core1/admin") {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, nodata)
} else {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, "nope")
}
}))
}