forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sideload.go
251 lines (235 loc) · 5.86 KB
/
sideload.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
package kapacitor
import (
"fmt"
"log"
"net/url"
"strconv"
"text/template"
text "text/template"
"github.com/influxdata/kapacitor/bufpool"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/services/httppost"
"github.com/influxdata/kapacitor/services/sideload"
"github.com/pkg/errors"
)
type SideloadNode struct {
node
s *pipeline.SideloadNode
source sideload.Source
orderTmpls []orderTmpl
Endpoint *httppost.Endpoint
order []string
bufferPool *bufpool.Pool
}
// Create a new SideloadNode which loads fields and tags from external sources.
func newSideloadNode(et *ExecutingTask, n *pipeline.SideloadNode, d NodeDiagnostic) (*SideloadNode, error) {
var e *httppost.Endpoint
var ok bool
sn := &SideloadNode{
node: node{Node: n, et: et, diag: d},
s: n,
bufferPool: bufpool.New(),
order: make([]string, len(n.OrderList)),
orderTmpls: make([]orderTmpl, len(n.OrderList)),
}
u, err := url.Parse(n.Source)
if err != nil {
log.Fatal(err)
}
if u.Scheme == "" {
e, ok = et.tm.HTTPPostService.Endpoint(n.Source)
if !ok {
log.Fatal("Specified endpoint does not exist: " + n.Source)
}
} else {
conf := httppost.Config{URLTemplate: n.Source}
e = &httppost.Endpoint{}
if err := e.Update(conf); err != nil {
return nil, err
}
}
sn.Endpoint = e
src, err := et.tm.SideloadService.Source(e)
if err != nil {
return nil, err
}
sn.source = src
for i, o := range n.OrderList {
op, err := newOrderTmpl(o, sn.bufferPool)
if err != nil {
return nil, err
}
sn.orderTmpls[i] = op
}
sn.node.runF = sn.runSideload
sn.node.stopF = sn.stopSideload
return sn, nil
}
func (n *SideloadNode) runSideload([]byte) error {
consumer := edge.NewConsumerWithReceiver(
n.ins[0],
edge.NewReceiverFromForwardReceiverWithStats(
n.outs,
edge.NewTimedForwardReceiver(n.timer, n),
),
)
return consumer.Consume()
}
func (n *SideloadNode) stopSideload() {
n.source.Close()
}
type orderTmpl struct {
raw string
tmpl *text.Template
bufferPool *bufpool.Pool
}
func newOrderTmpl(text string, bp *bufpool.Pool) (orderTmpl, error) {
t, err := template.New("order").Parse(text)
if err != nil {
return orderTmpl{}, err
}
return orderTmpl{
raw: text,
tmpl: t,
bufferPool: bp,
}, nil
}
func (t orderTmpl) Path(tags models.Tags) (string, error) {
buf := t.bufferPool.Get()
defer t.bufferPool.Put(buf)
err := t.tmpl.Execute(buf, tags)
if err != nil {
return "", err
}
return buf.String(), nil
}
func (n *SideloadNode) doSideload(p edge.FieldsTagsTimeSetter) {
for i, o := range n.orderTmpls {
p, err := o.Path(p.Tags())
if err != nil {
n.diag.Error("failed to evaluate order template", err, keyvalue.KV("order", o.raw))
return
}
n.order[i] = p
}
if len(n.s.Fields) > 0 {
fields := p.Fields().Copy()
for key, dflt := range n.s.Fields {
value := n.source.Lookup(n.order, key)
if value == nil {
// Use default
fields[key] = dflt
} else {
v, err := convertType(value, dflt)
if err != nil {
n.diag.Error("failed to load key", err, keyvalue.KV("key", key), keyvalue.KV("expected", fmt.Sprintf("%T", dflt)), keyvalue.KV("got", fmt.Sprintf("%T", value)))
fields[key] = dflt
} else {
fields[key] = v
}
}
}
p.SetFields(fields)
}
if len(n.s.Tags) > 0 {
tags := p.Tags().Copy()
for key, dflt := range n.s.Tags {
value := n.source.Lookup(n.order, key)
if value == nil {
tags[key] = dflt
} else {
v, err := convertType(value, dflt)
if err != nil {
n.diag.Error("failed to load key", err, keyvalue.KV("key", key), keyvalue.KV("expected", "string"), keyvalue.KV("got", fmt.Sprintf("%T", value)))
tags[key] = dflt
} else {
tags[key] = v.(string)
}
}
}
p.SetTags(tags)
}
}
func (n *SideloadNode) BeginBatch(begin edge.BeginBatchMessage) (edge.Message, error) {
begin = begin.ShallowCopy()
return begin, nil
}
func (n *SideloadNode) BatchPoint(bp edge.BatchPointMessage) (edge.Message, error) {
bp = bp.ShallowCopy()
n.doSideload(bp)
return bp, nil
}
func (n *SideloadNode) EndBatch(end edge.EndBatchMessage) (edge.Message, error) {
return end, nil
}
func (n *SideloadNode) Point(p edge.PointMessage) (edge.Message, error) {
p = p.ShallowCopy()
n.doSideload(p)
return p, nil
}
func (n *SideloadNode) Barrier(b edge.BarrierMessage) (edge.Message, error) {
return b, nil
}
func (n *SideloadNode) DeleteGroup(d edge.DeleteGroupMessage) (edge.Message, error) {
return d, nil
}
func (n *SideloadNode) Done() {}
func convertType(src, dflt interface{}) (interface{}, error) {
switch dflt.(type) {
case int64:
switch src := src.(type) {
case int64:
return src, nil
case float64:
i := int64(src)
if float64(i) == src {
return i, nil
}
case string:
i, err := strconv.Atoi(src)
if err != nil {
return nil, errors.Wrap(err, "cannot convert string to int64")
}
return i, nil
}
case float64:
switch src := src.(type) {
case int64:
return float64(src), nil
case float64:
return src, nil
case string:
f, err := strconv.ParseFloat(src, 64)
if err != nil {
return nil, errors.Wrap(err, "cannot convert string to float64")
}
return f, nil
}
case bool:
switch src := src.(type) {
case bool:
return src, nil
case string:
b, err := strconv.ParseBool(src)
if err != nil {
return nil, errors.Wrap(err, "cannot convert string to bool")
}
return b, nil
}
case string:
switch src := src.(type) {
case int64:
return strconv.FormatInt(src, 10), nil
case float64:
return strconv.FormatFloat(src, 'f', -1, 64), nil
case bool:
return strconv.FormatBool(src), nil
case string:
return src, nil
}
}
return nil, fmt.Errorf("cannot convert value of type %T to type %T", src, dflt)
}