-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreport.go
More file actions
70 lines (56 loc) · 1.26 KB
/
report.go
File metadata and controls
70 lines (56 loc) · 1.26 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
package main
import (
"bytes"
"log"
"net/http"
"time"
)
var client *http.Client
const posterCount = 10
func init() {
tr := &http.Transport{
ResponseHeaderTimeout: time.Second * 5,
}
client = &http.Client{Transport: tr}
}
func reportPoster(ch logChannel) {
var active bool
url := "http://" + *flagreporthost + "/api/v1/store-result"
if len(*flagreporthost) > 0 {
active = true
}
var js []byte
var err error
for {
select {
case data := <-ch:
// log.Printf("got log data: '%#v'", data)
if !active {
log.Println("report poster not active")
continue
}
js, err = data.JSON()
if err != nil {
log.Println("Could not encode JSON: ", err)
continue
}
reader := bytes.NewReader(js)
req, err := http.NewRequest("POST", url, reader)
if err != nil {
log.Printf("could not setup POST request: %s", err)
continue
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Printf("Error posting data: %s", err)
continue
}
resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
log.Printf("Unhappy response: %d\n", resp.StatusCode)
time.Sleep(200 * time.Millisecond) // Slow down a tiny bit when we have errors
}
}
}
}