-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpclient.go
48 lines (39 loc) · 898 Bytes
/
httpclient.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
package sentry
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
var reportURL = "http://127.0.0.1:50001/agent/api/putMetrics"
type AgentDataPoint struct {
Metric string `json:"metric"`
Tags map[string]string `json:"tags"`
Timestamp int64 `json:"timestamp"`
Value float64 `json:"value"`
}
func Send(dps []AgentDataPoint) error {
data, err := json.Marshal(dps)
if err != nil {
return err
}
client := &http.Client{
Timeout: 10 * time.Second,
}
body := bytes.NewReader(data)
req, err := http.NewRequest("POST", reportURL, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http status error: %d", resp.StatusCode)
}
return nil
}