forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solr.go
442 lines (404 loc) · 13.9 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package solr
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
)
const mbeansPath = "/admin/mbeans?stats=true&wt=json&cat=CORE&cat=QUERYHANDLER&cat=UPDATEHANDLER&cat=CACHE"
const adminCoresPath = "/solr/admin/cores?action=STATUS&wt=json"
type node struct {
Host string `json:"host"`
}
const sampleConfig = `
## specify a list of one or more Solr servers
servers = ["http://localhost:8983"]
## specify a list of one or more Solr cores (default - all)
# cores = ["main"]
`
// Solr is a plugin to read stats from one or many Solr servers
type Solr struct {
Local bool
Servers []string
HTTPTimeout internal.Duration
Cores []string
client *http.Client
}
// AdminCoresStatus is an exported type that
// contains a response with information about Solr cores.
type AdminCoresStatus struct {
Status map[string]struct {
Index struct {
SizeInBytes int64 `json:"sizeInBytes"`
NumDocs int64 `json:"numDocs"`
MaxDoc int64 `json:"maxDoc"`
DeletedDocs int64 `json:"deletedDocs"`
} `json:"index"`
} `json:"status"`
}
// MBeansData is an exported type that
// contains a response from Solr with metrics
type MBeansData struct {
Headers ResponseHeader `json:"responseHeader"`
SolrMbeans []json.RawMessage `json:"solr-mbeans"`
}
// ResponseHeader is an exported type that
// contains a response metrics: QTime and Status
type ResponseHeader struct {
QTime int64 `json:"QTime"`
Status int64 `json:"status"`
}
// Core is an exported type that
// contains Core metrics
type Core struct {
Stats struct {
DeletedDocs int64 `json:"deletedDocs"`
MaxDoc int64 `json:"maxDoc"`
NumDocs int64 `json:"numDocs"`
} `json:"stats"`
}
// QueryHandler is an exported type that
// contains query handler metrics
type QueryHandler struct {
Stats struct {
One5minRateReqsPerSecond float64 `json:"15minRateReqsPerSecond"`
FiveMinRateReqsPerSecond float64 `json:"5minRateReqsPerSecond"`
Seven5thPcRequestTime float64 `json:"75thPcRequestTime"`
Nine5thPcRequestTime float64 `json:"95thPcRequestTime"`
Nine99thPcRequestTime float64 `json:"999thPcRequestTime"`
Nine9thPcRequestTime float64 `json:"99thPcRequestTime"`
AvgRequestsPerSecond float64 `json:"avgRequestsPerSecond"`
AvgTimePerRequest float64 `json:"avgTimePerRequest"`
Errors int64 `json:"errors"`
HandlerStart int64 `json:"handlerStart"`
MedianRequestTime float64 `json:"medianRequestTime"`
Requests int64 `json:"requests"`
Timeouts int64 `json:"timeouts"`
TotalTime float64 `json:"totalTime"`
} `json:"stats"`
}
// UpdateHandler is an exported type that
// contains update handler metrics
type UpdateHandler struct {
Stats struct {
Adds int64 `json:"adds"`
AutocommitMaxDocs int64 `json:"autocommit maxDocs"`
AutocommitMaxTime string `json:"autocommit maxTime"`
Autocommits int64 `json:"autocommits"`
Commits int64 `json:"commits"`
CumulativeAdds int64 `json:"cumulative_adds"`
CumulativeDeletesByID int64 `json:"cumulative_deletesById"`
CumulativeDeletesByQuery int64 `json:"cumulative_deletesByQuery"`
CumulativeErrors int64 `json:"cumulative_errors"`
DeletesByID int64 `json:"deletesById"`
DeletesByQuery int64 `json:"deletesByQuery"`
DocsPending int64 `json:"docsPending"`
Errors int64 `json:"errors"`
ExpungeDeletes int64 `json:"expungeDeletes"`
Optimizes int64 `json:"optimizes"`
Rollbacks int64 `json:"rollbacks"`
SoftAutocommits int64 `json:"soft autocommits"`
} `json:"stats"`
}
// Hitratio is an helper interface
// so we can later on convert it to float64
type Hitratio interface{}
// Cache is an exported type that
// contains cache metrics
type Cache struct {
Stats struct {
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
func NewSolr() *Solr {
return &Solr{
HTTPTimeout: internal.Duration{Duration: time.Second * 5},
}
}
// SampleConfig returns sample configuration for this plugin.
func (s *Solr) SampleConfig() string {
return sampleConfig
}
// Description returns the plugin description.
func (s *Solr) Description() string {
return "Read stats from one or more Solr servers or cores"
}
// Gather reads the stats from Solr and writes it to the
// Accumulator.
func (s *Solr) Gather(acc telegraf.Accumulator) error {
if s.client == nil {
client := s.createHTTPClient()
s.client = client
}
var wg sync.WaitGroup
wg.Add(len(s.Servers))
for _, serv := range s.Servers {
go func(serv string, acc telegraf.Accumulator) {
defer wg.Done()
acc.AddError(s.gatherServerMetrics(serv, acc))
}(serv, acc)
}
wg.Wait()
return nil
}
// Gather all metrics from server
func (s *Solr) gatherServerMetrics(server string, acc telegraf.Accumulator) error {
measurementTime := time.Now()
adminCoresStatus := &AdminCoresStatus{}
if err := s.gatherData(s.adminURL(server), adminCoresStatus); err != nil {
return err
}
addAdminCoresStatusToAcc(acc, adminCoresStatus, measurementTime)
cores := s.filterCores(getCoresFromStatus(adminCoresStatus))
var wg sync.WaitGroup
wg.Add(len(cores))
for _, core := range cores {
go func(server string, core string, acc telegraf.Accumulator) {
defer wg.Done()
mBeansData := &MBeansData{}
acc.AddError(s.gatherData(s.mbeansURL(server, core), mBeansData))
acc.AddError(addCoreMetricsToAcc(acc, core, mBeansData, measurementTime))
acc.AddError(addQueryHandlerMetricsToAcc(acc, core, mBeansData, measurementTime))
acc.AddError(addUpdateHandlerMetricsToAcc(acc, core, mBeansData, measurementTime))
acc.AddError(addCacheMetricsToAcc(acc, core, mBeansData, measurementTime))
}(server, core, acc)
}
wg.Wait()
return nil
}
// Use cores from configuration if exists, else use cores from server
func (s *Solr) filterCores(serverCores []string) []string {
if len(s.Cores) == 0 {
return serverCores
}
return s.Cores
}
// Return list of cores from solr server
func getCoresFromStatus(adminCoresStatus *AdminCoresStatus) []string {
serverCores := []string{}
for coreName := range adminCoresStatus.Status {
serverCores = append(serverCores, coreName)
}
return serverCores
}
// Add core metrics from admin to accumulator
// This is the only point where size_in_bytes is available (as far as I checked)
func addAdminCoresStatusToAcc(acc telegraf.Accumulator, adminCoreStatus *AdminCoresStatus, time time.Time) {
for core, metrics := range adminCoreStatus.Status {
coreFields := map[string]interface{}{
"deleted_docs": metrics.Index.DeletedDocs,
"max_docs": metrics.Index.MaxDoc,
"num_docs": metrics.Index.NumDocs,
"size_in_bytes": metrics.Index.SizeInBytes,
}
acc.AddFields(
"solr_admin",
coreFields,
map[string]string{"core": core},
time,
)
}
}
// Add core metrics section to accumulator
func addCoreMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error {
var coreMetrics map[string]Core
if err := json.Unmarshal(mBeansData.SolrMbeans[1], &coreMetrics); err != nil {
return err
}
for name, metrics := range coreMetrics {
if strings.Contains(name, "@") {
continue
}
coreFields := map[string]interface{}{
"deleted_docs": metrics.Stats.DeletedDocs,
"max_docs": metrics.Stats.MaxDoc,
"num_docs": metrics.Stats.NumDocs,
}
acc.AddFields(
"solr_core",
coreFields,
map[string]string{
"core": core,
"handler": name},
time,
)
}
return nil
}
// Add query metrics section to accumulator
func addQueryHandlerMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error {
var queryMetrics map[string]QueryHandler
if err := json.Unmarshal(mBeansData.SolrMbeans[3], &queryMetrics); err != nil {
return err
}
for name, metrics := range queryMetrics {
coreFields := map[string]interface{}{
"15min_rate_reqs_per_second": metrics.Stats.One5minRateReqsPerSecond,
"5min_rate_reqs_per_second": metrics.Stats.FiveMinRateReqsPerSecond,
"75th_pc_request_time": metrics.Stats.Seven5thPcRequestTime,
"95th_pc_request_time": metrics.Stats.Nine5thPcRequestTime,
"999th_pc_request_time": metrics.Stats.Nine99thPcRequestTime,
"99th_pc_request_time": metrics.Stats.Nine9thPcRequestTime,
"avg_requests_per_second": metrics.Stats.AvgRequestsPerSecond,
"avg_time_per_request": metrics.Stats.AvgTimePerRequest,
"errors": metrics.Stats.Errors,
"handler_start": metrics.Stats.HandlerStart,
"median_request_time": metrics.Stats.MedianRequestTime,
"requests": metrics.Stats.Requests,
"timeouts": metrics.Stats.Timeouts,
"total_time": metrics.Stats.TotalTime,
}
acc.AddFields(
"solr_queryhandler",
coreFields,
map[string]string{
"core": core,
"handler": name},
time,
)
}
return nil
}
// Add update metrics section to accumulator
func addUpdateHandlerMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error {
var updateMetrics map[string]UpdateHandler
if err := json.Unmarshal(mBeansData.SolrMbeans[5], &updateMetrics); err != nil {
return err
}
for name, metrics := range updateMetrics {
var autoCommitMaxTime int64
if len(metrics.Stats.AutocommitMaxTime) > 2 {
autoCommitMaxTime, _ = strconv.ParseInt(metrics.Stats.AutocommitMaxTime[:len(metrics.Stats.AutocommitMaxTime)-2], 0, 64)
}
coreFields := map[string]interface{}{
"adds": metrics.Stats.Adds,
"autocommit_max_docs": metrics.Stats.AutocommitMaxDocs,
"autocommit_max_time": autoCommitMaxTime,
"autocommits": metrics.Stats.Autocommits,
"commits": metrics.Stats.Commits,
"cumulative_adds": metrics.Stats.CumulativeAdds,
"cumulative_deletes_by_id": metrics.Stats.CumulativeDeletesByID,
"cumulative_deletes_by_query": metrics.Stats.CumulativeDeletesByQuery,
"cumulative_errors": metrics.Stats.CumulativeErrors,
"deletes_by_id": metrics.Stats.DeletesByID,
"deletes_by_query": metrics.Stats.DeletesByQuery,
"docs_pending": metrics.Stats.DocsPending,
"errors": metrics.Stats.Errors,
"expunge_deletes": metrics.Stats.ExpungeDeletes,
"optimizes": metrics.Stats.Optimizes,
"rollbacks": metrics.Stats.Rollbacks,
"soft_autocommits": metrics.Stats.SoftAutocommits,
}
acc.AddFields(
"solr_updatehandler",
coreFields,
map[string]string{
"core": core,
"handler": name},
time,
)
}
return nil
}
// Get float64 from interface
func getFloat(unk interface{}) float64 {
switch i := unk.(type) {
case float64:
return i
case string:
f, _ := strconv.ParseFloat(i, 64)
return f
default:
return float64(0)
}
}
// Add cache metrics section to accumulator
func addCacheMetricsToAcc(acc telegraf.Accumulator, core string, mBeansData *MBeansData, time time.Time) error {
var cacheMetrics map[string]Cache
if err := json.Unmarshal(mBeansData.SolrMbeans[7], &cacheMetrics); err != nil {
return err
}
for name, metrics := range cacheMetrics {
cumulativeHits := getFloat(metrics.Stats.CumulativeHitratio)
hitratio := getFloat(metrics.Stats.Hitratio)
coreFields := map[string]interface{}{
"cumulative_evictions": metrics.Stats.CumulativeEvictions,
"cumulative_hitratio": cumulativeHits,
"cumulative_hits": metrics.Stats.CumulativeHits,
"cumulative_inserts": metrics.Stats.CumulativeInserts,
"cumulative_lookups": metrics.Stats.CumulativeLookups,
"evictions": metrics.Stats.Evictions,
"hitratio": hitratio,
"hits": metrics.Stats.Hits,
"inserts": metrics.Stats.Inserts,
"lookups": metrics.Stats.Lookups,
"size": metrics.Stats.Size,
"warmup_time": metrics.Stats.WarmupTime,
}
acc.AddFields(
"solr_cache",
coreFields,
map[string]string{
"core": core,
"handler": name},
time,
)
}
return nil
}
// Provide admin url
func (s *Solr) adminURL(server string) string {
return fmt.Sprintf("%s%s", server, adminCoresPath)
}
// Provide mbeans url
func (s *Solr) mbeansURL(server string, core string) string {
return fmt.Sprintf("%s/solr/%s%s", server, core, mbeansPath)
}
func (s *Solr) createHTTPClient() *http.Client {
tr := &http.Transport{
ResponseHeaderTimeout: s.HTTPTimeout.Duration,
}
client := &http.Client{
Transport: tr,
Timeout: s.HTTPTimeout.Duration,
}
return client
}
func (s *Solr) gatherData(url string, v interface{}) error {
r, err := s.client.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
return fmt.Errorf("solr: API responded with status-code %d, expected %d, url %s",
r.StatusCode, http.StatusOK, url)
}
if err = json.NewDecoder(r.Body).Decode(v); err != nil {
return err
}
return nil
}
func init() {
inputs.Add("solr", func() telegraf.Input {
return NewSolr()
})
}