-
-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy pathhttp_job.go
More file actions
179 lines (156 loc) · 4.51 KB
/
Copy pathhttp_job.go
File metadata and controls
179 lines (156 loc) · 4.51 KB
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
package job
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/cenkalti/backoff/v5"
"github.com/google/uuid"
v1 "github.com/openstatushq/openstatus/apps/checker/proto/private_location/v1"
"github.com/openstatushq/openstatus/apps/checker/request"
)
func HTTPJob(monitor *v1.HTTPMonitor) (*HttpPingData, error) {
ctx := context.Background()
retry := monitor.Retry
if retry == 0 {
retry = 3
}
requestClient := &http.Client{
Timeout: time.Duration(monitor.Timeout) * time.Millisecond,
}
defer requestClient.CloseIdleConnections()
if !monitor.FollowRedirects {
requestClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
} else {
requestClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return http.ErrUseLastResponse
}
return nil
}
}
var degradedAfter int64
if monitor.DegradedAt != nil {
degradedAfter = *monitor.DegradedAt
}
headers := make([]struct {
Key string `json:"key"`
Value string `json:"value"`
}, 0)
if monitor.Headers != nil {
for _, header := range monitor.Headers {
headers = append(headers, struct {
Key string `json:"key"`
Value string `json:"value"`
}{
Key: header.Key,
Value: header.Value,
})
}
}
req := request.HttpCheckerRequest{
URL: monitor.Url,
MonitorID: monitor.Id,
Method: monitor.Method,
Body: monitor.Body,
Retry: monitor.Retry,
Timeout: monitor.Timeout,
DegradedAfter: degradedAfter,
FollowRedirects: monitor.FollowRedirects,
Headers: headers,
}
var called int
op := func() (*HttpPingData, error) {
called++
res, err := checker.Http(ctx, requestClient, req)
if err != nil {
return nil, fmt.Errorf("unable to ping: %w", err)
}
timingBytes, err := json.Marshal(res.Timing)
if err != nil {
return nil, fmt.Errorf("error while parsing timing data %s: %w", req.URL, err)
}
headersBytes, err := json.Marshal(res.Headers)
if err != nil {
return nil, fmt.Errorf("error while parsing headers %s: %w", req.URL, err)
}
id, err := uuid.NewV7()
if err != nil {
return nil, fmt.Errorf("error while generating uuid: %w", err)
}
status := statusCode(res.Status)
isSuccessful := status.IsSuccessful()
if len(monitor.HeaderAssertions) > 0 {
headersAsString, err := json.Marshal(res.Headers)
if err != nil {
return nil, fmt.Errorf("error while parsing headers %s: %w", req.URL, err)
}
for _, assertion := range monitor.HeaderAssertions {
assert := assertions.HeaderTarget{
Comparator: request.StringComparator(assertion.Comparator.String()),
Target: assertion.Target,
Key: assertion.Key,
}
assert.HeaderEvaluate(string(headersAsString))
}
}
if len(monitor.StatusCodeAssertions) > 0 {
for _, assertion := range monitor.StatusCodeAssertions {
assert := assertions.StatusTarget{
Comparator: request.NumberComparator(assertion.Comparator.String()),
Target: assertion.Target,
}
isSuccessful = isSuccessful && assert.StatusEvaluate(int64(res.Status))
}
}
if len(monitor.BodyAssertions) > 0 {
for _, assertion := range monitor.BodyAssertions {
assert := assertions.StringTargetType{
Comparator: request.StringComparator(assertion.Comparator.String()),
Target: assertion.Target,
}
isSuccessful = isSuccessful && assert.StringEvaluate(res.Body)
}
}
requestStatus := "success"
if !isSuccessful {
requestStatus = "error"
} else if req.DegradedAfter > 0 && res.Latency > req.DegradedAfter {
requestStatus = "degraded"
}
data := HttpPingData{
ID: id.String(),
Latency: res.Latency,
StatusCode: res.Status,
Timestamp: res.Timestamp,
CronTimestamp: req.CronTimestamp,
URL: req.URL,
Method: req.Method,
Timing: string(timingBytes),
Headers: string(headersBytes),
Body: "",
RequestStatus: requestStatus,
Error: 0,
}
if isSuccessful {
if req.DegradedAfter != 0 && res.Latency > req.DegradedAfter {
data.Body = res.Body
}
} else {
data.Error = 1
if called < int(retry) {
return nil, fmt.Errorf("unable to ping: %v with status %v", res, res.Status)
}
}
fmt.Println(data)
return &data, nil
}
resp, err := backoff.Retry(context.TODO(), op, backoff.WithMaxTries(uint(retry)), backoff.WithBackOff(backoff.NewExponentialBackOff()))
if err != nil {
return nil, err
}
return resp, nil
}