-
Notifications
You must be signed in to change notification settings - Fork 21
/
sink.go
204 lines (177 loc) · 6.59 KB
/
sink.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
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package sink
import (
"bytes"
"container/ring"
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/xmidt-org/caduceus/internal/metrics"
"github.com/xmidt-org/retry"
"github.com/xmidt-org/retry/retryhttp"
"github.com/xmidt-org/wrp-go/v3"
"github.com/xmidt-org/wrp-go/v3/wrphttp"
"go.uber.org/zap"
)
type Sink interface {
Update(Listener) error
Send(*ring.Ring, string, string, *wrp.Message) error
}
type WebhookV1 struct {
id string
deliveryInterval time.Duration
deliveryRetries int
logger *zap.Logger
//TODO: need to determine best way to add client and client middleware to WebhooV1
// client http.Client
// clientMiddleware func(http.Client) http.Client
}
func NewWebhookV1(s *sender) {
v1 := &WebhookV1{
id: s.id,
deliveryInterval: s.deliveryInterval,
deliveryRetries: s.deliveryRetries,
logger: s.logger,
}
s.sink = v1
}
func (v1 *WebhookV1) Update(l Listener) (err error) {
v1.id = l.GetId()
return nil
}
// worker is the routine that actually takes the queued messages and delivers
// them to the listeners outside webpa
func (v1 *WebhookV1) Send(urls *ring.Ring, secret, acceptType string, msg *wrp.Message) error {
defer func() {
if r := recover(); nil != r {
// s.droppedPanic.Add(1.0)
v1.logger.Error("goroutine send() panicked", zap.String("id", v1.id), zap.Any("panic", r))
}
// s.workers.Release()
// s.currentWorkersGauge.Add(-1.0)
}()
payload := msg.Payload
body := payload
var payloadReader *bytes.Reader
// Use the internal content type unless the accept type is wrp
contentType := msg.ContentType
switch acceptType {
case "wrp", wrp.MimeTypeMsgpack, wrp.MimeTypeWrp:
// WTS - We should pass the original, raw WRP event instead of
// re-encoding it.
contentType = wrp.MimeTypeMsgpack
buffer := bytes.NewBuffer([]byte{})
encoder := wrp.NewEncoder(buffer, wrp.Msgpack)
encoder.Encode(msg)
body = buffer.Bytes()
}
payloadReader = bytes.NewReader(body)
req, err := http.NewRequest("POST", urls.Value.(string), payloadReader)
if err != nil {
// Report drop
// s.droppedInvalidConfig.Add(1.0)
v1.logger.Error("Invalid URL", zap.String(metrics.UrlLabel, urls.Value.(string)), zap.String("id", v1.id), zap.Error(err))
return err
}
req.Header.Set("Content-Type", contentType)
// Add x-Midt-* headers
wrphttp.AddMessageHeaders(req.Header, msg)
// Provide the old headers for now
req.Header.Set("X-Webpa-Event", strings.TrimPrefix(msg.Destination, "event:"))
req.Header.Set("X-Webpa-Transaction-Id", msg.TransactionUUID)
// Add the device id without the trailing service
id, _ := wrp.ParseDeviceID(msg.Source)
req.Header.Set("X-Webpa-Device-Id", string(id))
req.Header.Set("X-Webpa-Device-Name", string(id))
// Apply the secret
if secret != "" {
s := hmac.New(sha1.New, []byte(secret))
s.Write(body)
sig := fmt.Sprintf("sha1=%s", hex.EncodeToString(s.Sum(nil)))
req.Header.Set("X-Webpa-Signature", sig)
}
// find the event "short name"
event := msg.FindEventStringSubMatch()
// Send it
v1.logger.Debug("attempting to send event", zap.String("event.source", msg.Source), zap.String("event.destination", msg.Destination))
client, _ := retryhttp.NewClient(
// retryhttp.WithHTTPClient(s.clientMiddleware(s.client)),
retryhttp.WithRunner(v1.addRunner(req, event)),
retryhttp.WithRequesters(v1.updateRequest(urls)),
)
resp, err := client.Do(req)
var deliveryCounterLabels []string
code := metrics.MessageDroppedCode
reason := metrics.NoErrReason
logger := v1.logger
if err != nil {
// Report failure
//TODO: add droppedMessage to webhook metrics and remove from sink sender?
// v1.droppedMessage.Add(1.0)
reason = metrics.GetDoErrReason(err)
if resp != nil {
code = strconv.Itoa(resp.StatusCode)
}
logger = v1.logger.With(zap.String(metrics.ReasonLabel, reason), zap.Error(err))
deliveryCounterLabels = []string{metrics.UrlLabel, req.URL.String(), metrics.ReasonLabel, reason, metrics.CodeLabel, code, metrics.EventLabel, event}
fmt.Print(deliveryCounterLabels)
// v1.droppedMessage.With(metrics.UrlLabel, req.URL.String(), metrics.ReasonLabel, reason).Add(1)
logger.Error("Dropped Network Error", zap.Error(err))
return err
} else {
// Report Result
code = strconv.Itoa(resp.StatusCode)
// read until the response is complete before closing to allow
// connection reuse
if resp.Body != nil {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
deliveryCounterLabels = []string{metrics.UrlLabel, req.URL.String(), metrics.ReasonLabel, reason, metrics.CodeLabel, code, metrics.EventLabel, event}
}
fmt.Print(deliveryCounterLabels)
//TODO: do we add deliveryCounter to webhook metrics and remove from sink sender?
// v1.deliveryCounter.With(prometheus.Labels{deliveryCounterLabels}).Add(1.0)
logger.Debug("event sent-ish", zap.String("event.source", msg.Source), zap.String("event.destination", msg.Destination), zap.String(metrics.CodeLabel, code), zap.String(metrics.UrlLabel, req.URL.String()))
return nil
}
func (v1 *WebhookV1) addRunner(request *http.Request, event string) retry.Runner[*http.Response] {
runner, _ := retry.NewRunner[*http.Response](
retry.WithPolicyFactory[*http.Response](retry.Config{
Interval: v1.deliveryInterval,
MaxRetries: v1.deliveryRetries,
}),
retry.WithOnAttempt[*http.Response](v1.onAttempt(request, event)),
)
return runner
}
func (v1 *WebhookV1) updateRequest(urls *ring.Ring) func(*http.Request) *http.Request {
return func(request *http.Request) *http.Request {
urls = urls.Next()
tmp, err := url.Parse(urls.Value.(string))
if err != nil {
v1.logger.Error("failed to update url", zap.String(metrics.UrlLabel, urls.Value.(string)), zap.Error(err))
//TODO: do we add droppedMessage metric to webhook and remove from sink sender?
// v1.droppedMessage.With(metrics.UrlLabel, request.URL.String(), metrics.ReasonLabel, metrics.UpdateRequestURLFailedReason).Add(1)
}
request.URL = tmp
return request
}
}
func (v1 *WebhookV1) onAttempt(request *http.Request, event string) retry.OnAttempt[*http.Response] {
return func(attempt retry.Attempt[*http.Response]) {
if attempt.Retries > 0 {
fmt.Print(event)
// s.deliveryRetryCounter.With(prometheus.Labels{UrlLabel: v1.id, EventLabel: event}).Add(1.0)
v1.logger.Debug("retrying HTTP transaction", zap.String(metrics.UrlLabel, request.URL.String()), zap.Error(attempt.Err), zap.Int("retry", attempt.Retries+1), zap.Int("statusCode", attempt.Result.StatusCode))
}
}
}