forked from bosun-monitor/bosun
/
elastic5.go
306 lines (290 loc) · 9.05 KB
/
elastic5.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
package expr
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"bosun.org/opentsdb"
elastic "gopkg.in/olivere/elastic.v5"
)
// InitClient sets up the elastic client. If the client has already been
// initialized it is a noop
func (e ElasticHosts) InitClient5(prefix string) error {
if _, ok := e.Hosts[prefix]; !ok {
prefixes := make([]string, len(e.Hosts))
i := 0
for k := range e.Hosts {
prefixes[i] = k
i++
}
return fmt.Errorf("prefix %v not defined, available prefixes are: %v", prefix, prefixes)
}
if c := esClients.m[prefix]; c != nil {
// client already initialized
return nil
}
// esClients.Lock()
var err error
if e.Hosts[prefix].SimpleClient {
// simple client enabled
esClients.m[prefix], err = elastic.NewSimpleClient(elastic.SetURL(e.Hosts[prefix].Hosts...), elastic.SetMaxRetries(10))
} else if len(e.Hosts[prefix].Hosts) == 0 {
// client option enabled
esClients.m[prefix], err = elastic.NewClient(e.Hosts[prefix].ClientOptionFuncs.([]elastic.ClientOptionFunc)...)
} else {
// default behavior
esClients.m[prefix], err = elastic.NewClient(elastic.SetURL(e.Hosts[prefix].Hosts...), elastic.SetMaxRetries(10))
}
// esClients.Unlock()
if err != nil {
return err
}
return nil
}
// getService returns an elasticsearch service based on the global client
func (e *ElasticHosts) getService5(prefix string) (*elastic.SearchService, error) {
esClients.Lock()
defer esClients.Unlock()
err := e.InitClient(prefix)
if err != nil {
return nil, err
}
return esClients.m[prefix].(*elastic.Client).Search(), nil
}
// Query takes a Logstash request, applies it a search service, and then queries
// elasticsearch.
func (e ElasticHosts) Query5(r *ElasticRequest5) (*elastic.SearchResult, error) {
s, err := e.getService5(r.HostKey)
if err != nil {
return nil, err
}
s.Index(r.Indices...)
// With IgnoreUnavailable there can be gaps in the indices (i.e. missing days) and we will not error
// If no indices match than there will be no successful shards and and error is returned in that case
s.IgnoreUnavailable(true)
res, err := s.SearchSource(r.Source).Do(context.Background())
if err != nil {
return nil, err
}
if res.Shards == nil {
return nil, fmt.Errorf("no shard info in reply, should not be here please file issue")
}
if res.Shards.Successful == 0 {
return nil, fmt.Errorf("no successful shards in result, perhaps the index does exist, total shards: %v, failed shards: %v", res.Shards.Total, res.Shards.Failed)
}
return res, nil
}
// ElasticRequest5 is a container for the information needed to query elasticsearch or a date
// histogram.
type ElasticRequest5 struct {
Indices []string
HostKey string
Start *time.Time
End *time.Time
Source *elastic.SearchSource // This the object that we build queries in
}
// CacheKey returns the text of the elastic query. That text is the indentifer for
// the query in the cache. It is a combination of the host key, indices queries and the json query content
func (r *ElasticRequest5) CacheKey() (string, error) {
s, err := r.Source.Source()
if err != nil {
return "", err
}
b, err := json.Marshal(s)
if err != nil {
return "", fmt.Errorf("failed to generate json representation of search source for cache key: %s", s)
}
return fmt.Sprintf("%s:%v\n%s", r.HostKey, r.Indices, b), nil
}
// timeESRequest execute the elasticsearch query (which may set or hit cache) and returns
// the search results.
func timeESRequest5(e *State, req *ElasticRequest5) (resp *elastic.SearchResult, err error) {
var source interface{}
source, err = req.Source.Source()
if err != nil {
return resp, fmt.Errorf("failed to get source of request while timing elastic request: %s", err)
}
b, err := json.MarshalIndent(source, "", " ")
if err != nil {
return resp, err
}
key, err := req.CacheKey()
if err != nil {
return nil, err
}
e.Timer.StepCustomTiming("elastic", "query", fmt.Sprintf("%s:%v\n%s", req.HostKey, req.Indices, b), func() {
getFn := func() (interface{}, error) {
return e.ElasticHosts.Query5(req)
}
var val interface{}
var hit bool
val, err, hit = e.Cache.Get(key, getFn)
collectCacheHit(e.Cache, "elastic", hit)
resp = val.(*elastic.SearchResult)
})
return
}
func ESDateHistogram5(prefix string, e *State, indexer ESIndexer, keystring string, filter elastic.Query, interval, sduration, eduration, stat_field, rstat string, size int) (r *Results, err error) {
r = new(Results)
req, err := ESBaseQuery5(e.now, indexer, filter, sduration, eduration, size, prefix)
if err != nil {
return nil, err
}
// Extended bounds and min doc count are required to get values back when the bucket value is 0
ts := elastic.NewDateHistogramAggregation().Field(indexer.TimeField).Interval(strings.Replace(interval, "M", "n", -1)).MinDocCount(0).ExtendedBoundsMin(req.Start).ExtendedBoundsMax(req.End).Format(elasticRFC3339)
if stat_field != "" {
ts = ts.SubAggregation("stats", elastic.NewExtendedStatsAggregation().Field(stat_field))
switch rstat {
case "avg", "min", "max", "sum", "sum_of_squares", "variance", "std_deviation":
default:
return r, fmt.Errorf("stat function %v not a valid option", rstat)
}
}
if keystring == "" {
req.Source = req.Source.Aggregation("ts", ts)
result, err := timeESRequest5(e, req)
if err != nil {
return nil, err
}
ts, found := result.Aggregations.DateHistogram("ts")
if !found {
return nil, fmt.Errorf("expected time series not found in elastic reply")
}
series := make(Series)
for _, v := range ts.Buckets {
val := processESBucketItem5(v, rstat)
if val != nil {
series[time.Unix(int64(v.Key)/1000, 0).UTC()] = *val
}
}
if len(series) == 0 {
return r, nil
}
r.Results = append(r.Results, &Result{
Value: series,
Group: make(opentsdb.TagSet),
})
return r, nil
}
keys := strings.Split(keystring, ",")
aggregation := elastic.NewTermsAggregation().Field(keys[len(keys)-1])
aggregation = aggregation.SubAggregation("ts", ts)
for i := len(keys) - 2; i > -1; i-- {
aggregation = elastic.NewTermsAggregation().Field(keys[i]).SubAggregation("g_"+keys[i+1], aggregation)
}
req.Source = req.Source.Aggregation("g_"+keys[0], aggregation)
result, err := timeESRequest5(e, req)
if err != nil {
return nil, err
}
top, ok := result.Aggregations.Terms("g_" + keys[0])
if !ok {
return nil, fmt.Errorf("top key g_%v not found in result", keys[0])
}
var desc func(*elastic.AggregationBucketKeyItem, opentsdb.TagSet, []string) error
desc = func(b *elastic.AggregationBucketKeyItem, tags opentsdb.TagSet, keys []string) error {
if ts, found := b.DateHistogram("ts"); found {
if e.Squelched(tags) {
return nil
}
series := make(Series)
for _, v := range ts.Buckets {
val := processESBucketItem5(v, rstat)
if val != nil {
series[time.Unix(int64(v.Key)/1000, 0).UTC()] = *val
}
}
if len(series) == 0 {
return nil
}
r.Results = append(r.Results, &Result{
Value: series,
Group: tags.Copy(),
})
return nil
}
if len(keys) < 1 {
return nil
}
n, _ := b.Aggregations.Terms("g_" + keys[0])
for _, item := range n.Buckets {
key := fmt.Sprint(item.Key)
tags[keys[0]] = key
if err := desc(item, tags.Copy(), keys[1:]); err != nil {
return err
}
}
return nil
}
for _, b := range top.Buckets {
tags := make(opentsdb.TagSet)
key := fmt.Sprint(b.Key)
tags[keys[0]] = key
if err := desc(b, tags, keys[1:]); err != nil {
return nil, err
}
}
return r, nil
}
// ESBaseQuery builds the base query that both ESCount and ESStat share
func ESBaseQuery5(now time.Time, indexer ESIndexer, filter elastic.Query, sduration, eduration string, size int, prefix string) (*ElasticRequest5, error) {
start, err := opentsdb.ParseDuration(sduration)
if err != nil {
return nil, err
}
var end opentsdb.Duration
if eduration != "" {
end, err = opentsdb.ParseDuration(eduration)
if err != nil {
return nil, err
}
}
st := now.Add(time.Duration(-start))
en := now.Add(time.Duration(-end))
indices := indexer.Generate(&st, &en)
r := ElasticRequest5{
Indices: indices,
HostKey: prefix,
Start: &st,
End: &en,
Source: elastic.NewSearchSource().Size(size),
}
var q elastic.Query
q = elastic.NewRangeQuery(indexer.TimeField).Gte(st).Lte(en).Format(elasticRFC3339)
r.Source = r.Source.Query(elastic.NewBoolQuery().Must(q, filter))
return &r, nil
}
func ScopeES5(ts opentsdb.TagSet, q elastic.Query) elastic.Query {
var filters []elastic.Query
for tagKey, tagValue := range ts {
filters = append(filters, elastic.NewTermQuery(tagKey, tagValue))
}
filters = append(filters, q)
b := elastic.NewBoolQuery().Must(filters...)
return b
}
func processESBucketItem5(b *elastic.AggregationBucketHistogramItem, rstat string) *float64 {
if stats, found := b.ExtendedStats("stats"); found {
var val *float64
switch rstat {
case "avg":
val = stats.Avg
case "min":
val = stats.Min
case "max":
val = stats.Max
case "sum":
val = stats.Sum
case "sum_of_squares":
val = stats.SumOfSquares
case "variance":
val = stats.Variance
case "std_deviation":
val = stats.StdDeviation
}
return val
}
v := float64(b.DocCount)
return &v
}