-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch.go
227 lines (191 loc) · 6.8 KB
/
elasticsearch.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
"golang.org/x/exp/slog"
)
// An ElasticSearchAggregateQuerier performs aggregate queries against an elasticsearch index
// The query should be a metric aggregation in the format '"aggregate function": { params }'
// The query should be unmarshable into the ElasticSearchAggregateQueryJSON type
// For example:
//
// { "cardinality": {"field": "peer"} }
//
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
type ElasticSearchAggregateQuerier struct {
api string
index string
username string
password string
}
var _ Querier = (*ElasticSearchAggregateQuerier)(nil)
func NewElasticSearchAggregateQuerier(api string, index string, username string, password string) (*ElasticSearchAggregateQuerier, error) {
u, err := url.Parse(api)
if err != nil {
return nil, fmt.Errorf("invalid api url: %w", err)
}
u.Path = fmt.Sprintf("/%s/_search", index)
return &ElasticSearchAggregateQuerier{
api: u.String(),
index: index,
username: username,
password: password,
}, nil
}
func (e *ElasticSearchAggregateQuerier) Execute(ctx context.Context, query string, fromTime, toTime time.Time, interval QueryInterval) ([]DataPoint, error) {
var qry ElasticSearchAggregateQueryJSON
if err := json.Unmarshal([]byte(query), &qry); err != nil {
return nil, fmt.Errorf("invalid query %q: %w", query, err)
}
var calendarInterval string
switch interval {
case QueryIntervalWeekly:
calendarInterval = "week"
case QueryIntervalDaily:
calendarInterval = "day"
case QueryIntervalHourly:
calendarInterval = "hour"
default:
return nil, fmt.Errorf("unsupported query interval: %q", interval)
}
in := &ElasticSearchAggregateRequestJSON{
Size: 0,
Query: ElasticSearchAggregateQueryParamsJSON{
Range: ElasticSearchAggregateRangeJSON{
Timestamp: ElasticSearchAggregateRangeTimestampJSON{
Gte: fromTime,
Lt: toTime,
},
},
},
Aggs: map[string]ElasticSearchAggregateAggJSON{
"A": {
DateHistogram: ElasticSearchAggregateDateHistogramJSON{
Field: "@timestamp",
CalendarInterval: calendarInterval,
Order: ElasticSearchAggregateDateHistogramOrderJSON{
Key: "desc",
},
},
Aggs: map[string]ElasticSearchAggregateQueryJSON{
"result": qry, // "result" corresponds to result field in ElasticSearchAggregateBucketJSON
},
},
},
}
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(in); err != nil {
return nil, fmt.Errorf("failed to encode query request: %w", err)
}
slog.Debug("sending request", "body", buf.String())
hc := http.Client{}
req, err := http.NewRequest("POST", e.api, buf)
if err != nil {
return nil, fmt.Errorf("failed to create new request: %w", err)
}
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(e.username, e.password)
resp, err := hc.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request failed: %s", resp.Status)
}
defer resp.Body.Close()
// read body fully so we have it for diagnosis during development
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read body request: %w", err)
}
slog.Debug("received response", "body", string(body))
var out ElasticSearchAggregateResponseJSON
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&out); err != nil {
return nil, fmt.Errorf("failed to decode query response: %w", err)
}
if out.TimedOut {
return nil, fmt.Errorf("query timed out")
}
agg, ok := out.Aggregations["A"]
if !ok {
return nil, fmt.Errorf(`expected aggregation "A" not found`)
}
if len(agg.Buckets) != 1 {
return nil, fmt.Errorf("unexpected number of aggregation buckets found: %d", len(agg.Buckets))
}
bucket := agg.Buckets[0]
valueTime, err := time.Parse("2006-01-02T15:04:05.999Z", bucket.KeyAsString)
if err != nil {
return nil, fmt.Errorf("invalid time in response %q: %w", bucket.KeyAsString, err)
}
if !valueTime.Equal(fromTime) {
return nil, fmt.Errorf("unexpected time in response %q (expected %q)", valueTime.Format("2006-01-02T15:04:05.999Z"), fromTime.Format("2006-01-02T15:04:05.999Z"))
}
point := DataPoint{
// elasticsearch returns the start of the range as the key, but our convention is to use the end time
Time: toTime,
}
switch tv := bucket.Result.Value.(type) {
case float64:
point.Value = tv
case int64:
point.Value = float64(tv)
default:
return nil, fmt.Errorf("unexpected value type in aggregation: %T", bucket.Result.Value)
}
return []DataPoint{point}, nil
}
type ElasticSearchAggregateRequestJSON struct {
Size int `json:"size"`
Query ElasticSearchAggregateQueryParamsJSON `json:"query"`
Aggs map[string]ElasticSearchAggregateAggJSON `json:"aggs"`
}
type ElasticSearchAggregateQueryParamsJSON struct {
Range ElasticSearchAggregateRangeJSON `json:"range"`
}
type ElasticSearchAggregateRangeJSON struct {
Timestamp ElasticSearchAggregateRangeTimestampJSON `json:"@timestamp"`
}
type ElasticSearchAggregateRangeTimestampJSON struct {
Gte time.Time `json:"gte,omitempty"`
Lt time.Time `json:"lt,omitempty"`
}
type ElasticSearchAggregateAggJSON struct {
DateHistogram ElasticSearchAggregateDateHistogramJSON `json:"date_histogram"`
Aggs map[string]ElasticSearchAggregateQueryJSON `json:"aggs"`
}
type ElasticSearchAggregateDateHistogramJSON struct {
Field string `json:"field"`
CalendarInterval string `json:"calendar_interval"`
Order ElasticSearchAggregateDateHistogramOrderJSON `json:"order"`
}
type ElasticSearchAggregateDateHistogramOrderJSON struct {
Key string `json:"_key"`
}
type ElasticSearchAggregateQueryJSON struct {
Cardinality map[string]any `json:"cardinality,omitempty"`
// TODO: support other aggregate query types such as max/min
// see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
}
type ElasticSearchAggregateResponseJSON struct {
TimedOut bool `json:"timed_out"`
Aggregations map[string]ElasticSearchAggregateJSON `json:"aggregations"`
}
type ElasticSearchAggregateJSON struct {
Buckets []ElasticSearchAggregateBucketJSON `json:"buckets"`
}
type ElasticSearchAggregateBucketJSON struct {
KeyAsString string `json:"key_as_string"`
Key any `json:"key"`
DocCount int `json:"doc_count"`
Result ElasticSearchAggregateResultJSON `json:"result"` // the name of this field is dynamic and set by the input query
}
type ElasticSearchAggregateResultJSON struct {
Value any `json:"value"`
}