99 "net/http"
1010 "os"
1111
12- sentryhttpclient "github.com/getsentry/sentry-go/httpclient"
13-
1412 "github.com/getsentry/gib-potato/internal/event"
1513 "github.com/getsentry/sentry-go"
1614)
@@ -19,6 +17,10 @@ func SendRequest(ctx context.Context, e event.PotalEvent) error {
1917 url := os .Getenv ("POTAL_URL" )
2018
2119 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 ()
2224
2325 body , jsonErr := json .Marshal (e )
2426 if jsonErr != nil {
@@ -27,32 +29,49 @@ func SendRequest(ctx context.Context, e event.PotalEvent) error {
2729 return jsonErr
2830 }
2931
30- r , newReqErr := http .NewRequestWithContext ( ctx , "POST" , url , bytes .NewBuffer (body ))
32+ r , newReqErr := http .NewRequest ( "POST" , url , bytes .NewBuffer (body ))
3133 if newReqErr != nil {
3234 hub .CaptureException (newReqErr )
3335 log .Printf ("An Error Occured %v" , newReqErr )
3436 return newReqErr
3537 }
3638
3739 r .Header .Add ("Content-Type" , "application/json" )
40+ r .Header .Add ("Sentry-Trace" , span .ToSentryTrace ())
41+ r .Header .Add ("Baggage" , span .ToBaggage ())
3842 r .Header .Add ("Authorization" , os .Getenv ("POTAL_TOKEN" ))
3943
40- client := & http.Client {
41- Transport : sentryhttpclient .NewSentryRoundTripper (nil ),
42- }
43-
44+ client := & http.Client {}
4445 res , reqErr := client .Do (r )
4546 if reqErr != nil {
4647 hub .CaptureException (reqErr )
48+ span .Status = sentry .SpanStatusInternalError
49+
4750 log .Printf ("An Error Occured %v" , reqErr )
4851 return reqErr
4952 }
5053 defer func () {
51- _ = res .Body .Close ()
54+ if err := res .Body .Close (); err != nil {
55+ log .Printf ("Failed to close response body: %v" , err )
56+ }
5257 }()
5358
54- if res .StatusCode == http .StatusOK {
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
5566 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
5675 }
5776
5877 msg := fmt .Sprintf ("GibPotato API: Got %s response" , res .Status )
0 commit comments