Skip to content

Commit

Permalink
add SendEventWithContext method (#824)
Browse files Browse the repository at this point in the history
* add SendEventWithContext method

this will let us pass a context.Context when creating a new request with
getRequestFromEvent.
  • Loading branch information
viglia committed May 6, 2024
1 parent 9a9f285 commit 3192e9f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
29 changes: 23 additions & 6 deletions transport.go
Expand Up @@ -2,6 +2,7 @@ package sentry

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
Expand Down Expand Up @@ -257,7 +258,7 @@ func envelopeFromBody(event *Event, dsn *Dsn, sentAt time.Time, body json.RawMes
return &b, nil
}

func getRequestFromEvent(event *Event, dsn *Dsn) (r *http.Request, err error) {
func getRequestFromEvent(ctx context.Context, event *Event, dsn *Dsn) (r *http.Request, err error) {
defer func() {
if r != nil {
r.Header.Set("User-Agent", fmt.Sprintf("%s/%s", event.Sdk.Name, event.Sdk.Version))
Expand All @@ -284,7 +285,13 @@ func getRequestFromEvent(event *Event, dsn *Dsn) (r *http.Request, err error) {
if err != nil {
return nil, err
}
return http.NewRequest(

if ctx == nil {
ctx = context.Background()
}

return http.NewRequestWithContext(
ctx,
http.MethodPost,
dsn.GetAPIURL().String(),
envelope,
Expand Down Expand Up @@ -395,8 +402,13 @@ func (t *HTTPTransport) Configure(options ClientOptions) {
})
}

// SendEvent assembles a new packet out of Event and sends it to remote server.
// SendEvent assembles a new packet out of Event and sends it to the remote server.
func (t *HTTPTransport) SendEvent(event *Event) {
t.SendEventWithContext(context.Background(), event)
}

// SendEventWithContext assembles a new packet out of Event and sends it to the remote server.
func (t *HTTPTransport) SendEventWithContext(ctx context.Context, event *Event) {
if t.dsn == nil {
return
}
Expand All @@ -407,7 +419,7 @@ func (t *HTTPTransport) SendEvent(event *Event) {
return
}

request, err := getRequestFromEvent(event, t.dsn)
request, err := getRequestFromEvent(ctx, event, t.dsn)
if err != nil {
return
}
Expand Down Expand Up @@ -625,8 +637,13 @@ func (t *HTTPSyncTransport) Configure(options ClientOptions) {
}
}

// SendEvent assembles a new packet out of Event and sends it to remote server.
// SendEvent assembles a new packet out of Event and sends it to the remote server.
func (t *HTTPSyncTransport) SendEvent(event *Event) {
t.SendEventWithContext(context.Background(), event)
}

// SendEventWithContext assembles a new packet out of Event and sends it to the remote server.
func (t *HTTPSyncTransport) SendEventWithContext(ctx context.Context, event *Event) {
if t.dsn == nil {
return
}
Expand All @@ -635,7 +652,7 @@ func (t *HTTPSyncTransport) SendEvent(event *Event) {
return
}

request, err := getRequestFromEvent(event, t.dsn)
request, err := getRequestFromEvent(ctx, event, t.dsn)
if err != nil {
return
}
Expand Down
3 changes: 2 additions & 1 deletion transport_test.go
Expand Up @@ -2,6 +2,7 @@ package sentry

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -339,7 +340,7 @@ func TestGetRequestFromEvent(t *testing.T) {
}

t.Run(test.testName, func(t *testing.T) {
req, err := getRequestFromEvent(test.event, dsn)
req, err := getRequestFromEvent(context.TODO(), test.event, dsn)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 3192e9f

Please sign in to comment.