From 63c0632b1263834350be38cd7c6760eabe25078a Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Fri, 21 May 2021 15:53:27 +0200 Subject: [PATCH] feat(transports): Report User-Agent identifying SDK (#357) 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. --- sentry.go | 3 +++ transport.go | 7 ++++++- transport_test.go | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sentry.go b/sentry.go index 742ff12e2..62d1b599b 100644 --- a/sentry.go +++ b/sentry.go @@ -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 { diff --git a/transport.go b/transport.go index 2d5a22af2..6cdf0750e 100644 --- a/transport.go +++ b/transport.go @@ -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") diff --git a/transport_test.go b/transport_test.go index efcee7d2e..c0d5a1b14 100644 --- a/transport_test.go +++ b/transport_test.go @@ -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) + } }) } }