-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhttp.go
82 lines (71 loc) · 1.81 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
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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/prometheus/common/log"
)
func extractErrorRate(reader io.Reader, config HTTPProbe) int {
var re = regexp.MustCompile(`(\d+)]]$`)
body, err := ioutil.ReadAll(reader)
if err != nil {
log.Errorf("Error reading HTTP body: %s", err)
return 0
}
var str = string(body)
matches := re.FindStringSubmatch(str)
value, err := strconv.Atoi(matches[1])
if err == nil {
return value
}
return 0
}
func probeHTTP(target string, w http.ResponseWriter, module Module) (success bool) {
config := module.HTTP
client := &http.Client{
Timeout: module.Timeout,
}
requestURL := config.Prefix + target + "/stats/"
request, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
log.Errorf("Error creating request for target %s: %s", target, err)
return
}
for key, value := range config.Headers {
if strings.Title(key) == "Host" {
request.Host = value
continue
}
request.Header.Set(key, value)
}
resp, err := client.Do(request)
// Err won't be nil if redirects were turned off. See https://github.com/golang/go/issues/3795
if err != nil && resp == nil {
log.Warnf("Error for HTTP request to %s: %s", target, err)
} else {
defer resp.Body.Close()
if len(config.ValidStatusCodes) != 0 {
for _, code := range config.ValidStatusCodes {
if resp.StatusCode == code {
success = true
break
}
}
} else if 200 <= resp.StatusCode && resp.StatusCode < 300 {
success = true
}
if success {
fmt.Fprintf(w, "probe_sentry_error_received %d\n", extractErrorRate(resp.Body, config))
}
}
if resp == nil {
resp = &http.Response{}
}
fmt.Fprintf(w, "probe_sentry_status_code %d\n", resp.StatusCode)
fmt.Fprintf(w, "probe_sentry_content_length %d\n", resp.ContentLength)
return
}