-
Notifications
You must be signed in to change notification settings - Fork 153
/
to.go
457 lines (406 loc) · 12.7 KB
/
to.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package http
import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"runtime"
"sort"
"strings"
"time"
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/pkg/syncutil"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
protocol "github.com/influxdata/line-protocol"
"github.com/pkg/errors"
)
const (
ToHTTPKind = "toHTTP"
DefaultToHTTPTimeout = 1 * time.Second
)
func init() {
toHTTPSignature := flux.FunctionSignature(
map[string]semantic.PolyType{
"url": semantic.String,
"method": semantic.String,
"name": semantic.String,
"timeout": semantic.Duration,
"timeColumn": semantic.String,
"tagColumns": semantic.NewArrayPolyType(semantic.String),
"valueColumns": semantic.NewArrayPolyType(semantic.String),
},
[]string{"url"},
)
flux.RegisterPackageValue("http", "to", flux.FunctionValueWithSideEffect(ToHTTPKind, createToHTTPOpSpec, toHTTPSignature))
flux.RegisterOpSpec(ToHTTPKind, func() flux.OperationSpec { return &ToHTTPOpSpec{} })
plan.RegisterProcedureSpecWithSideEffect(ToHTTPKind, newToHTTPProcedure, ToHTTPKind)
execute.RegisterTransformation(ToHTTPKind, createToHTTPTransformation)
}
// DefaultToHTTPUserAgent is the default user agent used by ToHttp
var DefaultToHTTPUserAgent = "fluxd/dev"
func newToHTTPClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
},
}
}
var toHTTPKeepAliveClient = newToHTTPClient()
// this is used so we can get better validation on marshaling, innerToHTTPOpSpec and ToHTTPOpSpec
// need to have identical fields
type innerToHTTPOpSpec ToHTTPOpSpec
type ToHTTPOpSpec struct {
URL string `json:"url"`
Method string `json:"method"` // default behavior should be POST
Name string `json:"name"`
NameColumn string `json:"nameColumn"` // either name or name_column must be set, if none is set try to use the "_measurement" column.
Headers map[string]string `json:"headers"` // TODO: implement Headers after bug with keys and arrays and objects is fixed (new parser implemented, with string literals as keys)
URLParams map[string]string `json:"urlParams"` // TODO: implement URLParams after bug with keys and arrays and objects is fixed (new parser implemented, with string literals as keys)
Timeout time.Duration `json:"timeout"` // default to something reasonable if zero
NoKeepAlive bool `json:"noKeepAlive"`
TimeColumn string `json:"timeColumn"`
TagColumns []string `json:"tagColumns"`
ValueColumns []string `json:"valueColumns"`
}
// ReadArgs loads a flux.Arguments into ToHTTPOpSpec. It sets several default values.
// If the http method isn't set, it defaults to POST, it also uppercases the http method.
// If the time_column isn't set, it defaults to execute.TimeColLabel.
// If the value_column isn't set it defaults to a []string{execute.DefaultValueColLabel}.
func (o *ToHTTPOpSpec) ReadArgs(args flux.Arguments) error {
var err error
o.URL, err = args.GetRequiredString("url")
if err != nil {
return err
}
var ok bool
o.Name, ok, err = args.GetString("name")
if err != nil {
return err
}
if !ok {
o.NameColumn, ok, err = args.GetString("nameColumn")
if err != nil {
return err
}
if !ok {
o.NameColumn = "_measurement"
}
}
o.Method, ok, err = args.GetString("method")
if err != nil {
return err
}
if !ok {
o.Method = "POST"
}
o.Method = strings.ToUpper(o.Method)
timeout, ok, err := args.GetDuration("timeout")
if err != nil {
return err
}
if !ok {
o.Timeout = DefaultToHTTPTimeout
} else {
o.Timeout = time.Duration(timeout)
}
o.TimeColumn, ok, err = args.GetString("timeColumn")
if err != nil {
return err
}
if !ok {
o.TimeColumn = execute.DefaultTimeColLabel
}
tagColumns, ok, err := args.GetArray("tagColumns", semantic.String)
if err != nil {
return err
}
o.TagColumns = o.TagColumns[:0]
if ok {
for i := 0; i < tagColumns.Len(); i++ {
o.TagColumns = append(o.TagColumns, tagColumns.Get(i).Str())
}
sort.Strings(o.TagColumns)
}
valueColumns, ok, err := args.GetArray("valueColumns", semantic.String)
if err != nil {
return err
}
o.ValueColumns = o.ValueColumns[:0]
if !ok || valueColumns.Len() == 0 {
o.ValueColumns = append(o.ValueColumns, execute.DefaultValueColLabel)
} else {
for i := 0; i < valueColumns.Len(); i++ {
o.ValueColumns = append(o.ValueColumns, valueColumns.Get(i).Str())
}
sort.Strings(o.ValueColumns)
}
// TODO: get other headers working!
o.Headers = map[string]string{
"Content-Type": "application/vnd.influx",
"User-Agent": DefaultToHTTPUserAgent,
}
return err
}
func createToHTTPOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
s := new(ToHTTPOpSpec)
if err := s.ReadArgs(args); err != nil {
return nil, err
}
return s, nil
}
// UnmarshalJSON unmarshals and validates toHTTPOpSpec into JSON.
func (o *ToHTTPOpSpec) UnmarshalJSON(b []byte) (err error) {
if err = json.Unmarshal(b, (*innerToHTTPOpSpec)(o)); err != nil {
return err
}
u, err := url.ParseRequestURI(o.URL)
if err != nil {
return err
}
if !(u.Scheme == "https" || u.Scheme == "http" || u.Scheme == "") {
return fmt.Errorf("scheme must be http or https but was %s", u.Scheme)
}
return nil
}
func (ToHTTPOpSpec) Kind() flux.OperationKind {
return ToHTTPKind
}
type ToHTTPProcedureSpec struct {
plan.DefaultCost
Spec *ToHTTPOpSpec
}
func (o *ToHTTPProcedureSpec) Kind() plan.ProcedureKind {
return ToHTTPKind
}
func (o *ToHTTPProcedureSpec) Copy() plan.ProcedureSpec {
s := o.Spec
res := &ToHTTPProcedureSpec{
Spec: &ToHTTPOpSpec{
URL: s.URL,
Method: s.Method,
Name: s.Name,
NameColumn: s.NameColumn,
Headers: make(map[string]string, len(s.Headers)),
URLParams: make(map[string]string, len(s.URLParams)),
Timeout: s.Timeout,
NoKeepAlive: s.NoKeepAlive,
TimeColumn: s.TimeColumn,
TagColumns: append([]string(nil), s.TagColumns...),
ValueColumns: append([]string(nil), s.ValueColumns...),
},
}
for k, v := range s.Headers {
res.Spec.Headers[k] = v
}
for k, v := range s.URLParams {
res.Spec.URLParams[k] = v
}
return res
}
func newToHTTPProcedure(qs flux.OperationSpec, a plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*ToHTTPOpSpec)
if !ok && spec != nil {
return nil, fmt.Errorf("invalid spec type %T", qs)
}
return &ToHTTPProcedureSpec{Spec: spec}, nil
}
func createToHTTPTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*ToHTTPProcedureSpec)
if !ok {
return nil, nil, fmt.Errorf("invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
t := NewToHTTPTransformation(d, cache, s)
return t, d, nil
}
type ToHTTPTransformation struct {
d execute.Dataset
cache execute.TableBuilderCache
spec *ToHTTPProcedureSpec
}
func (t *ToHTTPTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
return t.d.RetractTable(key)
}
func NewToHTTPTransformation(d execute.Dataset, cache execute.TableBuilderCache, spec *ToHTTPProcedureSpec) *ToHTTPTransformation {
return &ToHTTPTransformation{
d: d,
cache: cache,
spec: spec,
}
}
type toHttpMetric struct {
tags []*protocol.Tag
fields []*protocol.Field
name string
t time.Time
}
func (m *toHttpMetric) TagList() []*protocol.Tag {
return m.tags
}
func (m *toHttpMetric) FieldList() []*protocol.Field {
return m.fields
}
func (m *toHttpMetric) truncateTagsAndFields() {
m.fields = m.fields[:0]
m.tags = m.tags[:0]
}
func (m *toHttpMetric) Name() string {
return m.name
}
func (m *toHttpMetric) Time() time.Time {
return m.t
}
type idxType struct {
Idx int
Type flux.ColType
}
func (t *ToHTTPTransformation) Process(id execute.DatasetID, tbl flux.Table) error {
pr, pw := io.Pipe() // TODO: replce the pipe with something faster
m := &toHttpMetric{}
e := protocol.NewEncoder(pw)
e.FailOnFieldErr(true)
e.SetFieldSortOrder(protocol.SortFields)
cols := tbl.Cols()
labels := make(map[string]idxType, len(cols))
for i, col := range cols {
labels[col.Label] = idxType{Idx: i, Type: col.Type}
}
// do time
timeColLabel := t.spec.Spec.TimeColumn
timeColIdx, ok := labels[timeColLabel]
if !ok {
return errors.New("Could not get time column")
}
if timeColIdx.Type != flux.TTime {
return fmt.Errorf("column %s is not of type %s", timeColLabel, timeColIdx.Type)
}
var measurementNameCol string
if t.spec.Spec.Name == "" {
measurementNameCol = t.spec.Spec.NameColumn
}
// check if each col is a tag or value and cache this value for the loop
colMetadatas := tbl.Cols()
isTag := make([]bool, len(colMetadatas))
isValue := make([]bool, len(colMetadatas))
for i, col := range colMetadatas {
valIdx := sort.SearchStrings(t.spec.Spec.ValueColumns, col.Label)
isValue[i] = valIdx < len(t.spec.Spec.ValueColumns) && t.spec.Spec.ValueColumns[valIdx] == col.Label
tagIdx := sort.SearchStrings(t.spec.Spec.TagColumns, col.Label)
isTag[i] = tagIdx < len(t.spec.Spec.TagColumns) && t.spec.Spec.TagColumns[tagIdx] == col.Label
}
builder, new := t.cache.TableBuilder(tbl.Key())
if new {
if err := execute.AddTableCols(tbl, builder); err != nil {
return err
}
}
var wg syncutil.WaitGroup
wg.Do(func() error {
m.name = t.spec.Spec.Name
err := tbl.Do(func(er flux.ColReader) error {
l := er.Len()
for i := 0; i < l; i++ {
m.truncateTagsAndFields()
for j, col := range er.Cols() {
switch {
case col.Label == timeColLabel:
m.t = values.Time(er.Times(j).Value(i)).Time()
case measurementNameCol != "" && measurementNameCol == col.Label:
if col.Type != flux.TString {
return errors.New("invalid type for measurement column")
}
m.name = er.Strings(j).ValueString(i)
case isTag[j]:
if col.Type != flux.TString {
return errors.New("invalid type for tag column")
}
m.tags = append(m.tags, &protocol.Tag{Key: col.Label, Value: er.Strings(j).ValueString(i)})
case isValue[j]:
switch col.Type {
case flux.TFloat:
m.fields = append(m.fields, &protocol.Field{Key: col.Label, Value: er.Floats(j).Value(i)})
case flux.TInt:
m.fields = append(m.fields, &protocol.Field{Key: col.Label, Value: er.Ints(j).Value(i)})
case flux.TUInt:
m.fields = append(m.fields, &protocol.Field{Key: col.Label, Value: er.UInts(j).Value(i)})
case flux.TString:
m.fields = append(m.fields, &protocol.Field{Key: col.Label, Value: er.Strings(j).ValueString(i)})
case flux.TTime:
m.fields = append(m.fields, &protocol.Field{Key: col.Label, Value: values.Time(er.Times(j).Value(i))})
case flux.TBool:
m.fields = append(m.fields, &protocol.Field{Key: col.Label, Value: er.Bools(j).Value(i)})
default:
return fmt.Errorf("invalid type for column %s", col.Label)
}
}
}
_, err := e.Encode(m)
if err != nil {
return err
}
if err := execute.AppendRecord(i, er, builder); err != nil {
return err
}
}
return nil
})
if e := pw.Close(); e != nil && err == nil {
err = e
}
return err
})
req, err := http.NewRequest(t.spec.Spec.Method, t.spec.Spec.URL, pr)
if err != nil {
return err
}
if t.spec.Spec.Timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), t.spec.Spec.Timeout)
req = req.WithContext(ctx)
defer cancel()
}
var resp *http.Response
if t.spec.Spec.NoKeepAlive {
resp, err = newToHTTPClient().Do(req)
} else {
resp, err = toHTTPKeepAliveClient.Do(req)
}
if err != nil {
return err
}
if err := wg.Wait(); err != nil {
return err
}
if err := resp.Body.Close(); err != nil {
return err
}
return req.Body.Close()
}
func (t *ToHTTPTransformation) UpdateWatermark(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateWatermark(pt)
}
func (t *ToHTTPTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
func (t *ToHTTPTransformation) Finish(id execute.DatasetID, err error) {
t.d.Finish(err)
}