-
Notifications
You must be signed in to change notification settings - Fork 150
/
solr.go
335 lines (307 loc) · 9.19 KB
/
solr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package mpsolr
import (
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
mp "github.com/mackerelio/go-mackerel-plugin-helper"
"github.com/mackerelio/golib/logging"
)
var (
logger = logging.GetLogger("metrics.plugin.solr")
coreStatKeys = []string{"numDocs", "deletedDocs", "indexHeapUsageBytes", "version",
"segmentCount", "sizeInBytes"}
handlerPaths = []string{"/update/json", "/select", "/update/json/docs", "/get",
"/update/csv", "/replication", "/update", "/dataimport"}
// Solr5 ... "5minRateReqsPerSecond", "15minRateReqsPerSecond"
// Solr6 ... "5minRateRequestsPerSecond", "15minRateRequestsPerSecond"
legacyHandlerStatKeys = []string{"requests", "errors", "timeouts", "avgRequestsPerSecond",
"5minRateReqsPerSecond", "5minRateRequestsPerSecond", "15minRateReqsPerSecond",
"15minRateRequestsPerSecond", "avgTimePerRequest", "medianRequestTime", "75thPcRequestTime",
"95thPcRequestTime", "99thPcRequestTime", "999thPcRequestTime"}
handlerStatKeys = []string{"requests", "errors", "timeouts", "clientErrors",
"serverErrors", "requestTimes"}
cacheTypes = []string{"filterCache", "perSegFilter", "queryResultCache",
"documentCache", "fieldValueCache"}
cacheStatKeys = []string{"lookups", "hits", "hitratio", "inserts", "evictions",
"size", "warmupTime"}
legacyMbeanHandlerKeys = []string{"QUERYHANDLER", "UPDATEHANDLER", "REPLICATION"}
mbeanHandlerKeys = []string{"QUERY", "UPDATE", "REPLICATION"}
)
// SolrPlugin mackerel plugin for Solr
type SolrPlugin struct {
Protocol string
Host string
Port string
BaseURL string
Version string
Cores []string
Prefix string
Stats map[string](map[string]float64)
Tempfile string
}
func (s *SolrPlugin) greaterThanOrEqualToMajorVersion(minVer int) bool {
currentVer, err := strconv.Atoi(strings.Split(s.Version, ".")[0])
if err != nil {
logger.Errorf("Failed to parse major version %s", err)
}
return currentVer >= minVer
}
func fetchJSONData(url string) (map[string]interface{}, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "mackerel-plugin-solr")
resp, err := http.DefaultClient.Do(req)
if err != nil {
logger.Errorf("Failed to %s", err)
return nil, err
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
var stats map[string]interface{}
err = dec.Decode(&stats)
if err != nil {
logger.Errorf("Failed to %s", err)
return nil, err
}
return stats, nil
}
func (s *SolrPlugin) loadStatsCore(core string, values interface{}) error {
coreStats := values.(map[string]interface{})["index"].(map[string]interface{})
for _, k := range coreStatKeys {
v, ok := coreStats[k].(float64)
if !ok {
logger.Errorf("Failed to cast from %s to %s", coreStats[k], "float64")
return errors.New("type assersion error")
}
s.Stats[core][k] = v
}
return nil
}
func (s *SolrPlugin) setStatsMbean(core string, stats map[string]interface{}, allowKeys []string, keyIndex int) {
for _, values := range stats["solr-mbeans"].([]interface{}) {
switch values.(type) {
case string:
continue
default:
for key, value := range values.(map[string]interface{}) {
for k, v := range value.(map[string]interface{}) {
if k != "stats" {
continue
}
if v == nil {
continue
}
statValues := v.(map[string]interface{})
if s.greaterThanOrEqualToMajorVersion(7) {
keyConvertedStatValues := make(map[string]interface{})
var keyParts []string
for k, v := range statValues {
keyParts = strings.Split(k, ".")
if len(keyParts) > keyIndex {
keyConvertedStatValues[keyParts[keyIndex]] = v
}
}
statValues = keyConvertedStatValues
}
for _, allowKey := range allowKeys {
if statValues[allowKey] == nil {
continue
}
s.Stats[core][allowKey+"_"+escapeSlash(key)] = statValues[allowKey].(float64)
}
}
}
}
break // if QUERYHANDLER and QUERY or UPDATEHANDLER and UPDATE
}
}
func (s *SolrPlugin) loadStatsMbeanHandler(core string, cat string) error {
uri := s.BaseURL + "/" + core + "/admin/mbeans?stats=true&wt=json&cat=" + cat
for _, path := range handlerPaths {
uri += fmt.Sprintf("&key=%s", url.QueryEscape(path))
}
stats, err := fetchJSONData(uri)
if err != nil {
return err
}
var sKeys []string
if s.greaterThanOrEqualToMajorVersion(7) {
sKeys = handlerStatKeys
} else {
sKeys = legacyHandlerStatKeys
}
s.setStatsMbean(core, stats, sKeys, 2)
return nil
}
func (s *SolrPlugin) loadStatsMbeanCache(core string) error {
stats, err := fetchJSONData(s.BaseURL + "/" + core + "/admin/mbeans?stats=true&wt=json&cat=CACHE&key=filterCache&key=perSegFilter&key=queryResultCache&key=documentCache&key=fieldValueCache")
if err != nil {
return err
}
s.setStatsMbean(core, stats, cacheStatKeys, 3)
return nil
}
func (s *SolrPlugin) loadVersion() error {
stats, err := fetchJSONData(s.BaseURL + "/admin/info/system?wt=json")
if err != nil {
return err
}
lucene, ok := stats["lucene"].(map[string]interface{})
if !ok {
return errors.New("type assersion error")
}
solrVersion, ok := lucene["solr-spec-version"].(string)
if !ok {
return errors.New("type assersion error")
}
s.Version = solrVersion
return nil
}
func (s *SolrPlugin) loadStats() error {
s.Stats = map[string](map[string]float64){}
stats, err := fetchJSONData(s.BaseURL + "/admin/cores?wt=json")
if err != nil {
return err
}
s.Cores = []string{}
for core, values := range stats["status"].(map[string]interface{}) {
s.Cores = append(s.Cores, core)
s.Stats[core] = map[string]float64{}
err := s.loadStatsCore(core, values)
if err != nil {
return err
}
var mKeys []string
if s.greaterThanOrEqualToMajorVersion(7) {
mKeys = mbeanHandlerKeys
} else {
mKeys = legacyMbeanHandlerKeys
}
for _, mKey := range mKeys {
err = s.loadStatsMbeanHandler(core, mKey)
if err != nil {
return err
}
}
err = s.loadStatsMbeanCache(core)
if err != nil {
return err
}
}
return nil
}
func escapeSlash(slashIncludedString string) (str string) {
str = strings.Replace(slashIncludedString, "/", "", -1)
return
}
// FetchMetrics interface for mackerelplugin
func (s SolrPlugin) FetchMetrics() (map[string]interface{}, error) {
stat := make(map[string]interface{})
for core, stats := range s.Stats {
for k, v := range stats {
stat[core+"_"+k] = v
}
}
return stat, nil
}
// GraphDefinition interface for mackerelplugin
func (s SolrPlugin) GraphDefinition() map[string]mp.Graphs {
graphdef := make(map[string]mp.Graphs)
for _, core := range s.Cores {
graphdef[fmt.Sprintf("%s.%s.docsCount", s.Prefix, core)] = mp.Graphs{
Label: fmt.Sprintf("%s DocsCount", core),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: core + "_numDocs", Label: "NumDocs"},
{Name: core + "_deletedDocs", Label: "DeletedDocs"},
},
}
for _, key := range []string{"indexHeapUsageBytes", "segmentCount", "sizeInBytes"} {
metricLabel := strings.Title(key)
graphdef[fmt.Sprintf("%s.%s.%s", s.Prefix, core, key)] = mp.Graphs{
Label: fmt.Sprintf("%s %s", core, metricLabel),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: core + "_" + key, Label: metricLabel},
},
}
}
var sKeys []string
if s.greaterThanOrEqualToMajorVersion(7) {
sKeys = handlerStatKeys
} else {
sKeys = legacyHandlerStatKeys
}
for _, key := range sKeys {
var metrics []mp.Metrics
for _, path := range handlerPaths {
path = escapeSlash(path)
metricLabel := strings.Title(path)
diff := false
if key == "requests" || key == "errors" || key == "timeouts" ||
key == "clientErrors" || key == "serverErrors" || key == "requestTimes" {
diff = true
}
metrics = append(metrics,
mp.Metrics{Name: fmt.Sprintf("%s_%s_%s", core, key, path), Label: metricLabel, Diff: diff},
)
}
unit := "float"
if key == "requests" || key == "errors" || key == "timeouts" {
unit = "integer"
}
graphLabel := fmt.Sprintf("%s %s", core, strings.Title(key))
graphdef[fmt.Sprintf("%s.%s.%s", s.Prefix, core, key)] = mp.Graphs{
Label: graphLabel,
Unit: unit,
Metrics: metrics,
}
}
for _, key := range cacheStatKeys {
var metrics []mp.Metrics
for _, cacheType := range cacheTypes {
metricLabel := strings.Title(cacheType)
metrics = append(metrics,
mp.Metrics{Name: fmt.Sprintf("%s_%s_%s", core, key, cacheType), Label: metricLabel},
)
}
unit := "integer"
if key == "hitratio" {
unit = "float"
}
graphLabel := fmt.Sprintf("%s %s", core, strings.Title(key))
graphdef[fmt.Sprintf("%s.%s.%s", s.Prefix, core, key)] = mp.Graphs{
Label: graphLabel,
Unit: unit,
Metrics: metrics,
}
}
}
return graphdef
}
// Do the plugin
func Do() {
optHost := flag.String("host", "localhost", "Hostname")
optPort := flag.String("port", "8983", "Port")
optTempfile := flag.String("tempfile", "", "Temp file name")
flag.Parse()
solr := SolrPlugin{
Protocol: "http",
Host: *optHost,
Port: *optPort,
Prefix: "solr",
}
solr.BaseURL = fmt.Sprintf("%s://%s:%s/solr", solr.Protocol, solr.Host, solr.Port)
solr.loadVersion()
solr.loadStats()
helper := mp.NewMackerelPlugin(solr)
helper.Tempfile = *optTempfile
helper.Run()
}