forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_post.go
170 lines (144 loc) · 3.94 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
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
package kapacitor
import (
"encoding/json"
"fmt"
"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, d NodeDiagnostic) (*HTTPPostNode, error) {
hn := &HTTPPostNode{
node: node{Node: n, et: et, diag: d},
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{}, nil, nil)
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) {
body := n.bp.Get()
defer n.bp.Put(body)
var contentType string
if n.endpoint.RowTemplate() != nil {
mr := newMappedRow(row)
n.endpoint.RowTemplate().Execute(body, mr)
} else {
result := new(models.Result)
result.Series = []*models.Row{row}
err := json.NewEncoder(body).Encode(result)
if err != nil {
n.diag.Error("failed to marshal row data json", err)
return
}
contentType = "application/json"
}
req, err := n.endpoint.NewHTTPRequest(body)
if err != nil {
n.diag.Error("failed to marshal row data json", err)
return
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
for k, v := range n.c.Headers {
req.Header.Set(k, v)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
n.diag.Error("failed to POST row data", err)
return
}
resp.Body.Close()
}
type mappedRow struct {
Name string
Tags map[string]string
Values []map[string]interface{}
}
func newMappedRow(row *models.Row) *mappedRow {
values := make([]map[string]interface{}, len(row.Values))
for i, v := range row.Values {
values[i] = make(map[string]interface{}, len(row.Columns))
for c, col := range row.Columns {
values[i][col] = v[c]
}
}
return &mappedRow{
Name: row.Name,
Tags: row.Tags,
Values: values,
}
}