-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
71 lines (59 loc) · 1.65 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/getsentry/sentry-go"
)
func sendRequest(e Event, hub *sentry.Hub, transaction *sentry.Span) {
url := os.Getenv("API_URL")
span := transaction.StartChild("http.client")
span.Description = fmt.Sprintf("POST %s", url)
defer span.Finish()
body, jsonErr := json.Marshal(e)
if jsonErr != nil {
hub.CaptureException(jsonErr)
log.Printf("An Error Occured %v", jsonErr)
return
}
r, newReqErr := http.NewRequest("POST", url, bytes.NewBuffer(body))
if newReqErr != nil {
hub.CaptureException(newReqErr)
log.Printf("An Error Occured %v", newReqErr)
return
}
r.Header.Add("Content-Type", "application/json")
r.Header.Add("Sentry-Trace", span.ToSentryTrace())
r.Header.Add("Baggage", transaction.ToBaggage())
r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("API_TOKEN")))
client := &http.Client{}
res, reqErr := client.Do(r)
if reqErr != nil {
hub.CaptureException(reqErr)
span.Status = sentry.SpanStatusInternalError
log.Printf("An Error Occured %v", reqErr)
return
}
defer res.Body.Close()
switch res.StatusCode {
case http.StatusOK:
span.Status = sentry.SpanStatusOK
return
case http.StatusUnauthorized:
fallthrough
case http.StatusForbidden:
span.Status = sentry.SpanStatusPermissionDenied
case http.StatusNotFound:
span.Status = sentry.SpanStatusNotFound
case http.StatusInternalServerError:
span.Status = sentry.SpanStatusInternalError
}
msg := fmt.Sprintf("GibPotato API: Got %s response", res.Status)
hub.ConfigureScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.LevelFatal)
})
hub.CaptureMessage(msg)
}