Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transports): Report User-Agent identifying SDK #357

Merged
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -144,7 +144,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 @@ -193,6 +193,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