forked from bigcommerce/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_out.go
138 lines (123 loc) · 2.97 KB
/
http_out.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
package kapacitor
import (
"encoding/json"
"log"
"net/http"
"path"
"sync"
"github.com/influxdata/kapacitor/expvar"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/services/httpd"
)
type HTTPOutNode struct {
node
c *pipeline.HTTPOutNode
result *models.Result
groupSeriesIdx map[models.GroupID]int
endpoint string
routes []httpd.Route
mu sync.RWMutex
}
// Create a new HTTPOutNode which caches the most recent item and exposes it over the HTTP API.
func newHTTPOutNode(et *ExecutingTask, n *pipeline.HTTPOutNode, l *log.Logger) (*HTTPOutNode, error) {
hn := &HTTPOutNode{
node: node{Node: n, et: et, logger: l},
c: n,
groupSeriesIdx: make(map[models.GroupID]int),
result: new(models.Result),
}
et.registerOutput(hn.c.Endpoint, hn)
hn.node.runF = hn.runOut
hn.node.stopF = hn.stopOut
return hn, nil
}
func (h *HTTPOutNode) Endpoint() string {
return h.endpoint
}
func (h *HTTPOutNode) runOut([]byte) error {
valueF := func() int64 {
h.mu.RLock()
l := len(h.groupSeriesIdx)
h.mu.RUnlock()
return int64(l)
}
h.statMap.Set(statCardinalityGauge, expvar.NewIntFuncGauge(valueF))
hndl := func(w http.ResponseWriter, req *http.Request) {
h.mu.RLock()
defer h.mu.RUnlock()
if b, err := json.Marshal(h.result); err != nil {
httpd.HttpError(
w,
err.Error(),
true,
http.StatusInternalServerError,
)
} else {
_, _ = w.Write(b)
}
}
p := path.Join("/tasks/", h.et.Task.ID, h.c.Endpoint)
r := []httpd.Route{{
Method: "GET",
Pattern: p,
HandlerFunc: hndl,
}}
h.endpoint = h.et.tm.HTTPDService.URL() + p
func() {
h.mu.Lock()
defer h.mu.Unlock()
h.routes = r
}()
err := h.et.tm.HTTPDService.AddRoutes(r)
if err != nil {
return err
}
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.updateResultWithRow(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.updateResultWithRow(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 *HTTPOutNode) updateResultWithRow(group models.GroupID, row *models.Row) {
h.mu.Lock()
defer h.mu.Unlock()
idx, ok := h.groupSeriesIdx[group]
if !ok {
idx = len(h.result.Series)
h.groupSeriesIdx[group] = idx
h.result.Series = append(h.result.Series, row)
} else {
h.result.Series[idx] = row
}
}
func (h *HTTPOutNode) stopOut() {
h.mu.Lock()
defer h.mu.Unlock()
h.et.tm.HTTPDService.DelRoutes(h.routes)
}