-
Notifications
You must be signed in to change notification settings - Fork 5
/
reporter.go
74 lines (57 loc) · 1.53 KB
/
reporter.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
package iopipe
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
func getCollectorURL(region string) string {
supportedRegions := map[string]struct{}{
"ap-northeast-1": struct{}{},
"ap-southeast-2": struct{}{},
"eu-west-1": struct{}{},
"us-east-2": struct{}{},
"us-west-1": struct{}{},
"us-west-2": struct{}{},
}
if region == "mock" {
return os.Getenv("MOCK_SERVER")
}
if _, exists := supportedRegions[region]; exists {
return fmt.Sprintf("https://metrics-api.%s.iopipe.com/v0/event", region)
}
return "https://metrics-api.iopipe.com/v0/event"
}
func sendReport(report *Report) error {
var (
err error
networkTimeout = 1 * time.Second
)
tr := &http.Transport{
DisableKeepAlives: false,
MaxIdleConns: 1, // is this equivalent to the maxCachedSessions in the js implementation
}
httpsClient := http.Client{Transport: tr, Timeout: networkTimeout}
reportJSONBytes, _ := json.Marshal(report) //.MarshalIndent(report, "", " ")
report.agent.log.Debug("Sending report:\n", string(reportJSONBytes))
uRL := getCollectorURL(os.Getenv("AWS_REGION"))
req, err := http.NewRequest("POST", uRL, bytes.NewReader(reportJSONBytes))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
res, err := httpsClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
resbody, err := ioutil.ReadAll(res.Body)
report.agent.log.Debug("IOpipe response: ", string(resbody))
if err != nil {
return err
}
return nil
}