-
-
Notifications
You must be signed in to change notification settings - Fork 588
/
http.go
49 lines (43 loc) · 1.13 KB
/
http.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
package observer
import (
"fmt"
"net/http"
"time"
)
// HTTPProbe is the exported handler object for monitors configured to
// perform HTTP requests
type HTTPProbe struct {
URL string
RCodes []int
}
// Name returns a name that uniquely identifies the monitor
func (p HTTPProbe) Name() string {
return fmt.Sprintf("%s-%d", p.URL, p.RCodes)
}
// Kind returns a name that uniquely identifies the `Kind` of `Prober`.
// Used for metrics and logging
func (p HTTPProbe) Kind() string {
return "HTTP"
}
// expectedRCode returns true when `got` matches one in `p.RCodes`, else
// returns false
func (p HTTPProbe) expectedRCode(got int) bool {
for _, c := range p.RCodes {
if got == c {
return true
}
}
return false
}
// Probe attempts the configured HTTP request
func (p HTTPProbe) Probe(timeout time.Duration) (bool, time.Duration) {
client := http.Client{Timeout: timeout}
start := time.Now()
// TODO(@beautifulentropy): add support for more than HTTP GET
resp, err := client.Get(p.URL)
if err != nil {
return false, time.Since(start)
}
// check response code and return
return p.expectedRCode(resp.StatusCode), time.Since(start)
}