Skip to content

Commit

Permalink
Move sdk info into client struct
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Aug 9, 2023
1 parent 403d006 commit 290c881
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
28 changes: 23 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
// The identifier of the SDK.
var sdkIdentifier = "sentry.go"

// Version is the version of the SDK.
const sdkVersion = "0.23.0"

// maxErrorDepth is the maximum number of errors reported in a chain of errors.
// This protects the SDK from an arbitrarily long chain of wrapped errors.
//
Expand Down Expand Up @@ -226,10 +229,13 @@ type ClientOptions struct {
// Client is the underlying processor that is used by the main API and Hub
// instances. It must be created with NewClient.
type Client struct {
mu sync.RWMutex
options ClientOptions
dsn *Dsn
eventProcessors []EventProcessor
integrations []Integration
sdkIdentifier string
sdkVersion string
// Transport is read-only. Replacing the transport of an existing client is
// not supported, create a new client instead.
Transport Transport
Expand Down Expand Up @@ -326,8 +332,10 @@ func NewClient(options ClientOptions) (*Client, error) {
}

client := Client{
options: options,
dsn: dsn,
options: options,
dsn: dsn,
sdkIdentifier: sdkIdentifier,
sdkVersion: sdkVersion,
}

client.setupTransport()
Expand Down Expand Up @@ -565,11 +573,21 @@ func (client *Client) EventFromCheckIn(checkIn *CheckIn, monitorConfig *MonitorC
}

func (client *Client) SetSDKIdentifier(identifier string) {
sdkIdentifier = identifier
client.mu.Lock()
defer client.mu.Unlock()

client.sdkIdentifier = identifier
}

func (client *Client) GetSDKIdentifier() string {
return sdkIdentifier
client.mu.RLock()
defer client.mu.RUnlock()

return client.sdkIdentifier
}

func (client *Client) GetSDKVersion() string {
return client.sdkVersion
}

// reverse reverses the slice a in place.
Expand Down Expand Up @@ -658,7 +676,7 @@ func (client *Client) prepareEvent(event *Event, hint *EventHint, scope EventMod
event.Platform = "go"
event.Sdk = SdkInfo{
Name: client.GetSDKIdentifier(),
Version: SDKVersion,
Version: client.GetSDKVersion(),
Integrations: client.listIntegrations(),
Packages: []SdkPackage{{
Name: "sentry-go",
Expand Down
1 change: 1 addition & 0 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (dsn Dsn) GetAPIURL() *url.URL {
}

// RequestHeaders returns all the necessary headers that have to be used in the transport.
// Deprecated: To be removed in 0.25.0. Requests to /envelope are authenticated using the DSN in the envelope header itself.
func (dsn Dsn) RequestHeaders() map[string]string {
auth := fmt.Sprintf("Sentry sentry_version=%s, sentry_timestamp=%d, "+
"sentry_client=sentry.go/%s, sentry_key=%s", apiVersion, time.Now().Unix(), Version, dsn.publicKey)
Expand Down
3 changes: 3 additions & 0 deletions scripts/bump-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ function replace() {
grep "$2" $3 # verify that replacement was successful
}

# SDKVersion is deprecated, remove prior 0.25.0
replace "const SDKVersion = \"[\w.-]+\"" "const SDKVersion = \"$NEW_VERSION\"" ./sentry.go

replace "const sdkVersion = \"[\w.-]+\"" "const sdkVersion = \"$NEW_VERSION\"" ./client.go

# Replace root module versions in submodules
GO_MOD_FILES=$(find . -type f -name 'go.mod' -not -path ./go.mod)
for GO_MOD in ${GO_MOD_FILES}; do
Expand Down
12 changes: 6 additions & 6 deletions sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import (
"time"
)

// Deprecated: Use SDKVersion instead.
// The version of the SDK.
// Deprecated: To be removed in 0.25.0. Use Client.GetSDKVersion() instead.
const Version = SDKVersion

// Version is the version of the SDK.
// The version of the SDK.
// Deprecated: To be removed in 0.25.0. Use Client.GetSDKVersion() instead.
const SDKVersion = "0.23.0"

// The identifier of the SDK.
// Deprecated: Use Client.GetSDKIdentifier() instead.
// Deprecated: To be removed in 0.25.0. Use Client.GetSDKIdentifier() instead.
const SDKIdentifier = "sentry.go"

// apiVersion is the minimum version of the Sentry API compatible with the
// sentry-go SDK.
// Deprecated: To be removed in 0.25.0.
const apiVersion = "7"

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

// 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
11 changes: 2 additions & 9 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ func envelopeFromBody(event *Event, dsn *Dsn, sentAt time.Time, body json.RawMes
func getRequestFromEvent(event *Event, dsn *Dsn) (r *http.Request, err error) {
defer func() {
if r != nil {
r.Header.Set("User-Agent", userAgent)
r.Header.Set("User-Agent", fmt.Sprintf("%s/%s", event.Sdk.Name, event.Sdk.Version))
r.Header.Set("Content-Type", "x-sentry-envelope")
}
}()
body := getRequestBodyFromEvent(event)
Expand Down Expand Up @@ -348,10 +349,6 @@ func (t *HTTPTransport) SendEvent(event *Event) {
return
}

for headerKey, headerValue := range t.dsn.RequestHeaders() {
request.Header.Set(headerKey, headerValue)
}

// <-t.buffer is equivalent to acquiring a lock to access the current batch.
// A few lines below, t.buffer <- b releases the lock.
//
Expand Down Expand Up @@ -573,10 +570,6 @@ func (t *HTTPSyncTransport) SendEvent(event *Event) {
return
}

for headerKey, headerValue := range t.dsn.RequestHeaders() {
request.Header.Set(headerKey, headerValue)
}

var eventType string
if event.Type == transactionType {
eventType = "transaction"
Expand Down
1 change: 1 addition & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ func TestGetRequestFromEvent(t *testing.T) {
t.Errorf("Incorrect API URL. want: %s, got: %s", test.apiURL, req.URL.String())
}

userAgent := fmt.Sprintf("%s/%s", test.event.Sdk.Name, test.event.Sdk.Version)
if ua := req.UserAgent(); ua != userAgent {
t.Errorf("got User-Agent = %q, want %q", ua, userAgent)
}
Expand Down

0 comments on commit 290c881

Please sign in to comment.