Add support for solr 7 to the solr input (#4271)

This commit is contained in:
Sambhav Kothari 2018-06-13 02:26:13 +05:30 committed by Daniel Nelson
parent d2e00a3205
commit a789f97feb
4 changed files with 112 additions and 30 deletions

View File

@ -5,7 +5,7 @@ The [solr](http://lucene.apache.org/solr/) plugin collects stats via the
More about [performance statistics](https://cwiki.apache.org/confluence/display/solr/Performance+Statistics+Reference) More about [performance statistics](https://cwiki.apache.org/confluence/display/solr/Performance+Statistics+Reference)
Tested from 3.5 to 6.* Tested from 3.5 to 7.*
### Configuration: ### Configuration:

View File

@ -113,20 +113,7 @@ type Hitratio interface{}
// Cache is an exported type that // Cache is an exported type that
// contains cache metrics // contains cache metrics
type Cache struct { type Cache struct {
Stats struct { Stats map[string]interface{} `json:"stats"`
CumulativeEvictions int64 `json:"cumulative_evictions"`
CumulativeHitratio Hitratio `json:"cumulative_hitratio"`
CumulativeHits int64 `json:"cumulative_hits"`
CumulativeInserts int64 `json:"cumulative_inserts"`
CumulativeLookups int64 `json:"cumulative_lookups"`
Evictions int64 `json:"evictions"`
Hitratio Hitratio `json:"hitratio"`
Hits int64 `json:"hits"`
Inserts int64 `json:"inserts"`
Lookups int64 `json:"lookups"`
Size int64 `json:"size"`
WarmupTime int64 `json:"warmupTime"`
} `json:"stats"`
} }
// NewSolr return a new instance of Solr // NewSolr return a new instance of Solr
@ -424,21 +411,30 @@ func addCacheMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBe
return err return err
} }
for name, metrics := range cacheMetrics { for name, metrics := range cacheMetrics {
cumulativeHits := getFloat(metrics.Stats.CumulativeHitratio) coreFields := make(map[string]interface{})
hitratio := getFloat(metrics.Stats.Hitratio) for key, value := range metrics.Stats {
coreFields := map[string]interface{}{ splitKey := strings.Split(key, ".")
"cumulative_evictions": metrics.Stats.CumulativeEvictions, newKey := splitKey[len(splitKey)-1]
"cumulative_hitratio": cumulativeHits, switch newKey {
"cumulative_hits": metrics.Stats.CumulativeHits, case "cumulative_evictions",
"cumulative_inserts": metrics.Stats.CumulativeInserts, "cumulative_hits",
"cumulative_lookups": metrics.Stats.CumulativeLookups, "cumulative_inserts",
"evictions": metrics.Stats.Evictions, "cumulative_lookups",
"hitratio": hitratio, "eviction",
"hits": metrics.Stats.Hits, "hits",
"inserts": metrics.Stats.Inserts, "inserts",
"lookups": metrics.Stats.Lookups, "lookups",
"size": metrics.Stats.Size, "size",
"warmup_time": metrics.Stats.WarmupTime, "evictions":
coreFields[newKey] = getInt(value)
case "hitratio",
"cumulative_hitratio":
coreFields[newKey] = getFloat(value)
case "warmupTime":
coreFields["warmup_time"] = getInt(value)
default:
continue
}
} }
acc.AddFields( acc.AddFields(
"solr_cache", "solr_cache",

View File

@ -43,6 +43,17 @@ func TestGatherStats(t *testing.T) {
map[string]string{"core": "main", "handler": "filterCache"}) map[string]string{"core": "main", "handler": "filterCache"})
} }
func TestSolr7MbeansStats(t *testing.T) {
ts := createMockSolr7Server()
solr := NewSolr()
solr.Servers = []string{ts.URL}
var acc testutil.Accumulator
require.NoError(t, solr.Gather(&acc))
acc.AssertContainsTaggedFields(t, "solr_cache",
solr7CacheExpected,
map[string]string{"core": "main", "handler": "documentCache"})
}
func TestSolr3GatherStats(t *testing.T) { func TestSolr3GatherStats(t *testing.T) {
ts := createMockSolr3Server() ts := createMockSolr3Server()
solr := NewSolr() solr := NewSolr()
@ -150,3 +161,18 @@ func createMockSolr3Server() *httptest.Server {
} }
})) }))
} }
func createMockSolr7Server() *httptest.Server {
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, mBeansSolr7Response)
} else {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, "nope")
}
}))
}

View File

@ -0,0 +1,60 @@
package solr
const mBeansSolr7Response = `
{
"responseHeader":{
"status":0,
"QTime":2
},
"solr-mbeans":[
"CORE",
{
},
"QUERYHANDLER",
{
},
"UPDATEHANDLER",
{
},
"CACHE",
{
"documentCache":{
"class":"org.apache.solr.search.LRUCache",
"description":"LRU Cache(maxSize=16384, initialSize=4096)",
"stats":{
"CACHE.searcher.documentCache.evictions": 141485,
"CACHE.searcher.documentCache.cumulative_lookups": 265132,
"CACHE.searcher.documentCache.hitratio": 0.44,
"CACHE.searcher.documentCache.size": 8192,
"CACHE.searcher.documentCache.cumulative_hitratio": 0.42,
"CACHE.searcher.documentCache.lookups": 1234,
"CACHE.searcher.documentCache.warmupTime": 1,
"CACHE.searcher.documentCache.inserts": 987,
"CACHE.searcher.documentCache.hits": 1111,
"CACHE.searcher.documentCache.cumulative_hits": 115364,
"CACHE.searcher.documentCache.cumulative_inserts": 149768,
"CACHE.searcher.documentCache.cumulative_evictions": 141486
}
}
}
]
}
`
var solr7CacheExpected = map[string]interface{}{
"evictions": int64(141485),
"cumulative_evictions": int64(141486),
"cumulative_hitratio": float64(0.42),
"cumulative_hits": int64(115364),
"cumulative_inserts": int64(149768),
"cumulative_lookups": int64(265132),
"hitratio": float64(0.44),
"hits": int64(1111),
"inserts": int64(987),
"lookups": int64(1234),
"size": int64(8192),
"warmup_time": int64(1),
}