-
Notifications
You must be signed in to change notification settings - Fork 777
/
webhookPublisher.go
161 lines (141 loc) · 4.28 KB
/
webhookPublisher.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
/*
Copyright 2017 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package publisher
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"time"
otelUtils "github.com/fission/fission/pkg/utils/otel"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
"golang.org/x/net/context/ctxhttp"
)
type (
// WebhookPublisher for a single URL. Satisfies the Publisher interface.
WebhookPublisher struct {
logger *zap.Logger
requestChannel chan *publishRequest
maxRetries int
retryDelay time.Duration
baseURL string
timeout time.Duration
}
publishRequest struct {
ctx context.Context
body string
headers map[string]string
target string
retries int
retryDelay time.Duration
}
)
// MakeWebhookPublisher creates a WebhookPublisher object for the given baseURL
func MakeWebhookPublisher(logger *zap.Logger, baseURL string) *WebhookPublisher {
p := &WebhookPublisher{
logger: logger.Named("webhook_publisher"),
baseURL: baseURL,
requestChannel: make(chan *publishRequest, 32), // buffered channel
// TODO make this configurable
timeout: 60 * time.Minute,
// TODO make this configurable
maxRetries: 10,
retryDelay: 500 * time.Millisecond,
}
go p.svc()
return p
}
// Publish sends a request to the target with payload having given body and headers
func (p *WebhookPublisher) Publish(ctx context.Context, body string, headers map[string]string, target string) {
tracer := otel.Tracer("WebhookPublisher")
ctx, span := tracer.Start(ctx, "WebhookPublisher/Publish")
defer span.End()
// serializing the request gives user a guarantee that the request is sent in sequence order
p.requestChannel <- &publishRequest{
ctx: ctx,
body: body,
headers: headers,
target: target,
retries: p.maxRetries,
retryDelay: p.retryDelay,
}
}
func (p *WebhookPublisher) svc() {
for {
r := <-p.requestChannel
p.makeHTTPRequest(r)
}
}
func (p *WebhookPublisher) makeHTTPRequest(r *publishRequest) {
url := p.baseURL + "/" + strings.TrimPrefix(r.target, "/")
msg := "making HTTP request"
level := zap.ErrorLevel
fields := []zap.Field{zap.String("url", url), zap.String("type", "publish_request")}
// log once for this request
defer func() {
if ce := otelUtils.LoggerWithTraceID(r.ctx, p.logger).Check(level, msg); ce != nil {
ce.Write(fields...)
}
}()
var buf bytes.Buffer
buf.WriteString(r.body)
// Create request
req, err := http.NewRequest(http.MethodPost, url, &buf)
if err != nil {
fields = append(fields, zap.Error(err))
return
}
for k, v := range r.headers {
req.Header.Set(k, v)
}
// Make the request
ctx, cancel := context.WithTimeoutCause(r.ctx, p.timeout, fmt.Errorf("webhook request timed out (%f)s exceeded ", p.timeout.Seconds()))
defer cancel()
resp, err := ctxhttp.Do(ctx, otelhttp.DefaultClient, req)
if err != nil {
fields = append(fields, zap.Error(err), zap.Any("request", r))
} else {
var body []byte
body, err = io.ReadAll(resp.Body)
if err != nil {
fields = append(fields, zap.Error(err), zap.Any("request", r))
msg = "read response body error"
} else {
fields = append(fields, zap.Int("status_code", resp.StatusCode), zap.String("body", string(body)))
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
level = zap.InfoLevel
} else if resp.StatusCode >= 400 && resp.StatusCode < 500 {
msg = "request returned bad request status code"
level = zap.WarnLevel
} else {
msg = "request returned failure status code"
}
return
}
}
// Schedule a retry, or give up if out of retries
r.retries--
if r.retries > 0 {
r.retryDelay *= time.Duration(2)
time.AfterFunc(r.retryDelay, func() {
p.requestChannel <- r
})
} else {
msg = "final retry failed, giving up"
// Event dropped
}
}