-
Notifications
You must be signed in to change notification settings - Fork 493
/
http_post.go
117 lines (104 loc) · 2.68 KB
/
http_post.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
package kapacitor
import (
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
"github.com/influxdata/kapacitor/bufpool"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/services/httppost"
)
type HTTPPostNode struct {
node
c *pipeline.HTTPPostNode
endpoint *httppost.Endpoint
mu sync.RWMutex
bp *bufpool.Pool
}
// Create a new HTTPPostNode which submits received items via POST to an HTTP endpoint
func newHTTPPostNode(et *ExecutingTask, n *pipeline.HTTPPostNode, l *log.Logger) (*HTTPPostNode, error) {
hn := &HTTPPostNode{
node: node{Node: n, et: et, logger: l},
c: n,
bp: bufpool.New(),
}
// Should only ever be 0 or 1 from validation of n
if len(n.URLs) == 1 {
e := httppost.NewEndpoint(n.URLs[0], nil, httppost.BasicAuth{})
hn.endpoint = e
}
// Should only ever be 0 or 1 from validation of n
if len(n.Endpoints) == 1 {
endpointName := n.Endpoints[0]
e, ok := et.tm.HTTPPostService.Endpoint(endpointName)
if !ok {
return nil, fmt.Errorf("endpoint '%s' does not exist", endpointName)
}
hn.endpoint = e
}
hn.node.runF = hn.runPost
return hn, nil
}
func (h *HTTPPostNode) runPost([]byte) error {
switch h.Wants() {
case pipeline.StreamEdge:
for p, ok := h.ins[0].NextPoint(); ok; p, ok = h.ins[0].NextPoint() {
h.timer.Start()
row := models.PointToRow(p)
h.postRow(p.Group, row)
h.timer.Stop()
for _, child := range h.outs {
err := child.CollectPoint(p)
if err != nil {
return err
}
}
}
case pipeline.BatchEdge:
for b, ok := h.ins[0].NextBatch(); ok; b, ok = h.ins[0].NextBatch() {
h.timer.Start()
row := models.BatchToRow(b)
h.postRow(b.Group, row)
h.timer.Stop()
for _, child := range h.outs {
err := child.CollectBatch(b)
if err != nil {
return err
}
}
}
}
return nil
}
// Update the result structure with a row.
func (h *HTTPPostNode) postRow(group models.GroupID, row *models.Row) {
result := new(models.Result)
result.Series = []*models.Row{row}
body := h.bp.Get()
defer h.bp.Put(body)
err := json.NewEncoder(body).Encode(result)
if err != nil {
h.incrementErrorCount()
h.logger.Printf("E! failed to marshal row data json: %v", err)
return
}
req, err := h.endpoint.NewHTTPRequest(body)
if err != nil {
h.incrementErrorCount()
h.logger.Printf("E! failed to marshal row data json: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
for k, v := range h.c.Headers {
req.Header.Set(k, v)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
h.incrementErrorCount()
h.logger.Printf("E! failed to POST row data: %v", err)
return
}
resp.Body.Close()
}