Skip to content

Commit

Permalink
feat(transports): Report User-Agent identifying SDK (#357)
Browse files Browse the repository at this point in the history
The default User-Agent for outgoing requests using Go's HTTP client is
Go-http-client/1.1.

Instead of reporting this generic string that confuses with any other Go
program, identify the SDK and version used to send out requests.
  • Loading branch information
rhcarvalho committed May 21, 2021
1 parent 3b083ad commit 63c0632
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const Version = "0.10.0"
// sentry-go SDK.
const apiVersion = "7"

// userAgent is the User-Agent of outgoing HTTP requests.
const userAgent = "sentry-go/" + Version

// Init initializes the SDK with options. The returned error is non-nil if
// options is invalid, for instance if a malformed DSN is provided.
func Init(options ClientOptions) error {
Expand Down
7 changes: 6 additions & 1 deletion transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ func transactionEnvelopeFromBody(eventID EventID, sentAt time.Time, body json.Ra
return &b, nil
}

func getRequestFromEvent(event *Event, dsn *Dsn) (*http.Request, error) {
func getRequestFromEvent(event *Event, dsn *Dsn) (r *http.Request, err error) {
defer func() {
if r != nil {
r.Header.Set("User-Agent", userAgent)
}
}()
body := getRequestBodyFromEvent(event)
if body == nil {
return nil, errors.New("event could not be marshaled")
Expand Down
4 changes: 4 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ func TestGetRequestFromEvent(t *testing.T) {
if req.URL.String() != test.apiURL {
t.Errorf("Incorrect API URL. want: %s, got: %s", test.apiURL, req.URL.String())
}

if ua := req.UserAgent(); ua != userAgent {
t.Errorf("got User-Agent = %q, want %q", ua, userAgent)
}
})
}
}
Expand Down

0 comments on commit 63c0632

Please sign in to comment.