forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_post.go
142 lines (120 loc) · 3.39 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
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
package kapacitor
import (
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
"github.com/influxdata/kapacitor/bufpool"
"github.com/influxdata/kapacitor/edge"
"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 (n *HTTPPostNode) runPost([]byte) error {
consumer := edge.NewGroupedConsumer(
n.ins[0],
n,
)
n.statMap.Set(statCardinalityGauge, consumer.CardinalityVar())
return consumer.Consume()
}
func (n *HTTPPostNode) NewGroup(group edge.GroupInfo, first edge.PointMeta) (edge.Receiver, error) {
g := &httpPostGroup{
n: n,
buffer: new(edge.BatchBuffer),
}
return edge.NewReceiverFromForwardReceiverWithStats(
n.outs,
edge.NewTimedForwardReceiver(n.timer, g),
), nil
}
type httpPostGroup struct {
n *HTTPPostNode
buffer *edge.BatchBuffer
}
func (g *httpPostGroup) BeginBatch(begin edge.BeginBatchMessage) (edge.Message, error) {
return nil, g.buffer.BeginBatch(begin)
}
func (g *httpPostGroup) BatchPoint(bp edge.BatchPointMessage) (edge.Message, error) {
return nil, g.buffer.BatchPoint(bp)
}
func (g *httpPostGroup) EndBatch(end edge.EndBatchMessage) (edge.Message, error) {
return g.BufferedBatch(g.buffer.BufferedBatchMessage(end))
}
func (g *httpPostGroup) BufferedBatch(batch edge.BufferedBatchMessage) (edge.Message, error) {
row := batch.ToRow()
g.n.postRow(row)
return batch, nil
}
func (g *httpPostGroup) Point(p edge.PointMessage) (edge.Message, error) {
row := p.ToRow()
g.n.postRow(row)
return p, nil
}
func (g *httpPostGroup) Barrier(b edge.BarrierMessage) (edge.Message, error) {
return b, nil
}
func (g *httpPostGroup) DeleteGroup(d edge.DeleteGroupMessage) (edge.Message, error) {
return d, nil
}
func (n *HTTPPostNode) postRow(row *models.Row) {
result := new(models.Result)
result.Series = []*models.Row{row}
body := n.bp.Get()
defer n.bp.Put(body)
err := json.NewEncoder(body).Encode(result)
if err != nil {
n.incrementErrorCount()
n.logger.Printf("E! failed to marshal row data json: %v", err)
return
}
req, err := n.endpoint.NewHTTPRequest(body)
if err != nil {
n.incrementErrorCount()
n.logger.Printf("E! failed to marshal row data json: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
for k, v := range n.c.Headers {
req.Header.Set(k, v)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
n.incrementErrorCount()
n.logger.Printf("E! failed to POST row data: %v", err)
return
}
resp.Body.Close()
}