Skip to content

Commit b6005c8

Browse files
giortzisgcleptric
andauthored
feat(go): test http-client integration (#362)
Co-authored-by: Michi Hoffmann <[email protected]>
1 parent c28bbe8 commit b6005c8

File tree

4 files changed

+22
-45
lines changed

4 files changed

+22
-45
lines changed

potal/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module github.com/getsentry/gib-potato
22

33
go 1.25.0
44

5+
// Test httpclient integration, PR #876
6+
replace github.com/getsentry/sentry-go => github.com/aldy505/sentry-go v0.0.0-20251120002518-9de99efea3a2
7+
58
require (
69
github.com/getsentry/sentry-go v0.36.3-0.20251028153640-e2badbb7694c
710
github.com/google/go-cmp v0.5.9

potal/go.sum

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1+
github.com/aldy505/sentry-go v0.0.0-20251120002518-9de99efea3a2 h1:JQ+3yeRe9T53bH29i2bqVRj2dbJsOrAaQMaQP59dnhc=
2+
github.com/aldy505/sentry-go v0.0.0-20251120002518-9de99efea3a2/go.mod h1:eRXCoh3uvmjQLY6qu63BjUZnaBu5L5WhMV1RwYO8W5s=
13
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
24
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/getsentry/sentry-go v0.36.0 h1:UkCk0zV28PiGf+2YIONSSYiYhxwlERE5Li3JPpZqEns=
4-
github.com/getsentry/sentry-go v0.36.0/go.mod h1:p5Im24mJBeruET8Q4bbcMfCQ+F+Iadc4L48tB1apo2c=
5-
github.com/getsentry/sentry-go v0.36.2-0.20251024113619-cda84dbb8532 h1:j71Y2rcZmVoi3TQqO79Xn4JPwNLVw9PKhCLZDQSKFOI=
6-
github.com/getsentry/sentry-go v0.36.2-0.20251024113619-cda84dbb8532/go.mod h1:p5Im24mJBeruET8Q4bbcMfCQ+F+Iadc4L48tB1apo2c=
7-
github.com/getsentry/sentry-go v0.36.2 h1:uhuxRPTrUy0dnSzTd0LrYXlBYygLkKY0hhlG5LXarzM=
8-
github.com/getsentry/sentry-go v0.36.2/go.mod h1:p5Im24mJBeruET8Q4bbcMfCQ+F+Iadc4L48tB1apo2c=
9-
github.com/getsentry/sentry-go v0.36.3-0.20251028153640-e2badbb7694c h1:lEVNr8EyF7wu8URA7vlhVUPkg8ea57q3ZIrCS8zvwvI=
10-
github.com/getsentry/sentry-go v0.36.3-0.20251028153640-e2badbb7694c/go.mod h1:p5Im24mJBeruET8Q4bbcMfCQ+F+Iadc4L48tB1apo2c=
115
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
126
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
137
github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=

potal/internal/potalhttp/client.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"net/http"
1010
"os"
1111

12+
sentryhttpclient "github.com/getsentry/sentry-go/httpclient"
13+
1214
"github.com/getsentry/gib-potato/internal/event"
1315
"github.com/getsentry/sentry-go"
1416
)
@@ -17,10 +19,6 @@ func SendRequest(ctx context.Context, e event.PotalEvent) error {
1719
url := os.Getenv("POTAL_URL")
1820

1921
hub := sentry.GetHubFromContext(ctx)
20-
txn := sentry.TransactionFromContext(ctx)
21-
22-
span := txn.StartChild("http.client", sentry.WithDescription(fmt.Sprintf("POST %s", url)))
23-
defer span.Finish()
2422

2523
body, jsonErr := json.Marshal(e)
2624
if jsonErr != nil {
@@ -29,49 +27,32 @@ func SendRequest(ctx context.Context, e event.PotalEvent) error {
2927
return jsonErr
3028
}
3129

32-
r, newReqErr := http.NewRequest("POST", url, bytes.NewBuffer(body))
30+
r, newReqErr := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(body))
3331
if newReqErr != nil {
3432
hub.CaptureException(newReqErr)
3533
log.Printf("An Error Occured %v", newReqErr)
3634
return newReqErr
3735
}
3836

3937
r.Header.Add("Content-Type", "application/json")
40-
r.Header.Add("Sentry-Trace", span.ToSentryTrace())
41-
r.Header.Add("Baggage", span.ToBaggage())
4238
r.Header.Add("Authorization", os.Getenv("POTAL_TOKEN"))
4339

44-
client := &http.Client{}
40+
client := &http.Client{
41+
Transport: sentryhttpclient.NewSentryRoundTripper(nil),
42+
}
43+
4544
res, reqErr := client.Do(r)
4645
if reqErr != nil {
4746
hub.CaptureException(reqErr)
48-
span.Status = sentry.SpanStatusInternalError
49-
5047
log.Printf("An Error Occured %v", reqErr)
5148
return reqErr
5249
}
5350
defer func() {
54-
if err := res.Body.Close(); err != nil {
55-
log.Printf("Failed to close response body: %v", err)
56-
}
51+
_ = res.Body.Close()
5752
}()
5853

59-
span.Data = map[string]interface{}{
60-
"http.response.status_code": res.StatusCode,
61-
}
62-
63-
switch res.StatusCode {
64-
case http.StatusOK:
65-
span.Status = sentry.SpanStatusOK
54+
if res.StatusCode == http.StatusOK {
6655
return nil
67-
case http.StatusUnauthorized:
68-
fallthrough
69-
case http.StatusForbidden:
70-
span.Status = sentry.SpanStatusPermissionDenied
71-
case http.StatusNotFound:
72-
span.Status = sentry.SpanStatusNotFound
73-
case http.StatusInternalServerError:
74-
span.Status = sentry.SpanStatusInternalError
7556
}
7657

7758
msg := fmt.Sprintf("GibPotato API: Got %s response", res.Status)

potal/main.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ var slackClient *slack.Client
1616

1717
func main() {
1818
sentryErr := sentry.Init(sentry.ClientOptions{
19-
Dsn: os.Getenv("SENTRY_POTAL_DSN"),
20-
Release: os.Getenv("RELEASE"),
21-
Environment: os.Getenv("ENVIRONMENT"),
22-
AttachStacktrace: true,
23-
SendDefaultPII: true,
24-
EnableTracing: true,
25-
TracesSampleRate: 1.0,
26-
EnableLogs: true,
27-
EnableTelemetryBuffer: true,
19+
Dsn: os.Getenv("SENTRY_POTAL_DSN"),
20+
Release: os.Getenv("RELEASE"),
21+
Environment: os.Getenv("ENVIRONMENT"),
22+
AttachStacktrace: true,
23+
SendDefaultPII: true,
24+
EnableTracing: true,
25+
TracesSampleRate: 1.0,
26+
EnableLogs: true,
2827
})
2928
if sentryErr != nil {
3029
log.Fatalf("An Error Occured: %v", sentryErr)

0 commit comments

Comments
 (0)