-
Notifications
You must be signed in to change notification settings - Fork 260
feat: add noop client implementation #1365
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -303,6 +303,7 @@ type ClientOptions struct { | |
| // instances. It must be created with NewClient. | ||
| type Client struct { | ||
| mu sync.RWMutex | ||
| disabled bool | ||
| options ClientOptions | ||
| dsn *protocol.Dsn | ||
| eventProcessors []EventProcessor | ||
|
|
@@ -413,7 +414,7 @@ func NewClient(options ClientOptions) (*Client, error) { | |
| var err error | ||
| dsn, err = protocol.NewDsn(options.Dsn) | ||
| if err != nil { | ||
| return nil, err | ||
| return NewNoopClient(), err | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -459,6 +460,39 @@ func NewClient(options ClientOptions) (*Client, error) { | |
| return &client, nil | ||
| } | ||
|
|
||
| // NewNoopClient returns a non-nil client that safely discards telemetry. | ||
| func NewNoopClient() *Client { | ||
| return &Client{ | ||
| disabled: true, | ||
| options: ClientOptions{ | ||
| DisableLogs: true, | ||
| DisableMetrics: true, | ||
| DisableTelemetryBuffer: true, | ||
| MaxErrorDepth: maxErrorDepth, | ||
| MaxSpans: defaultMaxSpans, | ||
| TraceIgnoreStatusCodes: [][]int{{404}}, | ||
| }, | ||
| sdkIdentifier: sdkIdentifier, | ||
| sdkVersion: SDKVersion, | ||
| Transport: new(noopTransport), | ||
| reportRecorder: report.NoopRecorder(), | ||
| reportProvider: report.NoopProvider(), | ||
| } | ||
| } | ||
|
|
||
| // IsEnabled reports whether the client processes telemetry. | ||
| func (client *Client) IsEnabled() bool { | ||
| return client != nil && !client.disabled | ||
| } | ||
|
|
||
| // normalizeClient guarantees a non-nil client for internal and Hub callers. | ||
| func normalizeClient(client *Client) *Client { | ||
| if client == nil { | ||
| return NewNoopClient() | ||
| } | ||
| return client | ||
| } | ||
|
|
||
| func (client *Client) setupTransport() { | ||
| opts := client.options | ||
| transport := opts.Transport | ||
|
|
@@ -605,7 +639,7 @@ func (client *Client) Options() ClientOptions { | |
| // GetDataCollection returns a copy of the resolved data collection | ||
| // configuration used by the client. | ||
| func (client *Client) GetDataCollection() DataCollection { | ||
| if client == nil || client.options.DataCollection == nil { | ||
| if !client.IsEnabled() || client.options.DataCollection == nil { | ||
| return DataCollection{} | ||
| } | ||
| return *cloneDataCollection(client.options.DataCollection) | ||
|
|
@@ -639,11 +673,14 @@ func (client *Client) CaptureCheckIn(checkIn *CheckIn, monitorConfig *MonitorCon | |
| // the utility methods like CaptureException. The return value is the | ||
| // event ID. In case Sentry is disabled or event was dropped, the return value will be nil. | ||
| func (client *Client) CaptureEvent(event *Event, hint *EventHint, scope EventModifier) *EventID { | ||
| if !client.IsEnabled() { | ||
| return nil | ||
| } | ||
| return client.processEvent(event, hint, scope) | ||
| } | ||
|
|
||
| func (client *Client) captureLog(log *Log, _ *Scope) bool { | ||
| if log == nil { | ||
| if !client.IsEnabled() || log == nil { | ||
| return false | ||
| } | ||
|
|
||
|
|
@@ -677,7 +714,7 @@ func (client *Client) captureLog(log *Log, _ *Scope) bool { | |
| } | ||
|
|
||
| func (client *Client) captureMetric(metric *Metric, _ *Scope) bool { | ||
| if metric == nil { | ||
| if !client.IsEnabled() || metric == nil { | ||
| return false | ||
| } | ||
|
|
||
|
|
@@ -710,6 +747,9 @@ func (client *Client) captureMetric(metric *Metric, _ *Scope) bool { | |
| // Recover captures a panic. | ||
| // Returns EventID if successfully, or nil if there's no error to recover from. | ||
| func (client *Client) Recover(err any, hint *EventHint, scope EventModifier) *EventID { | ||
| if !client.IsEnabled() { | ||
| return nil | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disabled Recover skips panic recoveryMedium Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 8fc2ecf. Configure here. |
||
| if err == nil { | ||
| err = recover() | ||
| } | ||
|
|
@@ -770,6 +810,9 @@ func (client *Client) RecoverWithContext( | |
| // the network synchronously, configure it to use the HTTPSyncTransport in the | ||
| // call to Init. | ||
| func (client *Client) Flush(timeout time.Duration) bool { | ||
| if !client.IsEnabled() { | ||
| return true | ||
| } | ||
| if client.batchLogger != nil || client.batchMeter != nil || client.telemetryProcessor != nil { | ||
| ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
| defer cancel() | ||
|
|
@@ -791,6 +834,9 @@ func (client *Client) Flush(timeout time.Duration) bool { | |
| // configure the SDK to use HTTPSyncTransport during initialization with Init. | ||
|
|
||
| func (client *Client) FlushWithContext(ctx context.Context) bool { | ||
| if !client.IsEnabled() { | ||
| return true | ||
| } | ||
| if client.batchLogger != nil { | ||
| client.batchLogger.Flush(ctx.Done()) | ||
| } | ||
|
|
@@ -808,6 +854,9 @@ func (client *Client) FlushWithContext(ctx context.Context) bool { | |
| // Close should be called after Flush and before terminating the program | ||
| // otherwise some events may be lost. | ||
| func (client *Client) Close() { | ||
| if !client.IsEnabled() { | ||
| return | ||
| } | ||
| if client.telemetryProcessor != nil { | ||
| client.telemetryProcessor.Close(5 * time.Second) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ const ( | |
| ) | ||
|
|
||
| // currentHub is the initial Hub with no Client bound and an empty Scope. | ||
| var currentHub = NewHub(nil, NewScope()) | ||
| var currentHub = NewHub(NewNoopClient(), NewScope()) | ||
|
|
||
| // Hub is the central object that manages scopes and clients. | ||
| // | ||
|
|
@@ -51,14 +51,14 @@ type layer struct { | |
| func (l *layer) Client() *Client { | ||
| l.mu.RLock() | ||
| defer l.mu.RUnlock() | ||
| return l.client | ||
| return normalizeClient(l.client) | ||
| } | ||
|
|
||
| // SetClient sets the layer's client. Safe for concurrent use. | ||
| func (l *layer) SetClient(c *Client) { | ||
| l.mu.Lock() | ||
| defer l.mu.Unlock() | ||
| l.client = c | ||
| l.client = normalizeClient(c) | ||
| } | ||
|
|
||
| type stack []*layer | ||
|
|
@@ -67,7 +67,7 @@ type stack []*layer | |
| func NewHub(client *Client, scope *Scope) *Hub { | ||
| hub := Hub{ | ||
| stack: &stack{{ | ||
| client: client, | ||
| client: normalizeClient(client), | ||
| scope: scope, | ||
| }}, | ||
| } | ||
|
|
@@ -218,7 +218,7 @@ func (hub *Hub) CaptureEvent(event *Event) *EventID { | |
| // CaptureEventWithHint is like CaptureEvent but additionally accepts an EventHint. | ||
| func (hub *Hub) CaptureEventWithHint(event *Event, hint *EventHint) *EventID { | ||
| client, scope := hub.Client(), hub.Scope() | ||
| if client == nil || scope == nil { | ||
| if scope == nil { | ||
| return nil | ||
| } | ||
| eventID := client.CaptureEvent(event, hint, scope) | ||
|
|
@@ -236,7 +236,7 @@ func (hub *Hub) CaptureEventWithHint(event *Event, hint *EventHint) *EventID { | |
| // Returns EventID if successfully, or nil if there's no Scope or Client available. | ||
| func (hub *Hub) CaptureMessage(message string) *EventID { | ||
| client, scope := hub.Client(), hub.Scope() | ||
| if client == nil || scope == nil { | ||
| if scope == nil { | ||
| return nil | ||
| } | ||
| eventID := client.CaptureMessage(message, nil, scope) | ||
|
|
@@ -254,7 +254,7 @@ func (hub *Hub) CaptureMessage(message string) *EventID { | |
| // Returns EventID if successfully, or nil if there's no Scope or Client available. | ||
| func (hub *Hub) CaptureException(exception error) *EventID { | ||
| client, scope := hub.Client(), hub.Scope() | ||
| if client == nil || scope == nil { | ||
| if scope == nil { | ||
| return nil | ||
| } | ||
| eventID := client.CaptureException(exception, &EventHint{OriginalException: exception}, scope) | ||
|
|
@@ -272,10 +272,6 @@ func (hub *Hub) CaptureException(exception error) *EventID { | |
| // Returns CheckInID if the check-in was captured successfully, or nil otherwise. | ||
| func (hub *Hub) CaptureCheckIn(checkIn *CheckIn, monitorConfig *MonitorConfig) *EventID { | ||
| client, scope := hub.Client(), hub.Scope() | ||
| if client == nil { | ||
| return nil | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check-in returns ID when disabledMedium Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 7ecc977. Configure here. |
||
| } | ||
|
|
||
| return client.CaptureCheckIn(checkIn, monitorConfig, scope) | ||
| } | ||
|
|
||
|
|
@@ -286,12 +282,6 @@ func (hub *Hub) CaptureCheckIn(checkIn *CheckIn, monitorConfig *MonitorConfig) * | |
| func (hub *Hub) AddBreadcrumb(breadcrumb *Breadcrumb, hint *BreadcrumbHint) { | ||
| client := hub.Client() | ||
|
|
||
| // If there's no client, just store it on the scope straight away | ||
| if client == nil { | ||
| hub.Scope().AddBreadcrumb(breadcrumb, defaultMaxBreadcrumbs) | ||
| return | ||
| } | ||
|
|
||
| limit := client.options.MaxBreadcrumbs | ||
| switch { | ||
| case limit < 0: | ||
|
|
@@ -321,7 +311,7 @@ func (hub *Hub) Recover(err interface{}) *EventID { | |
| err = recover() | ||
| } | ||
| client, scope := hub.Client(), hub.Scope() | ||
| if client == nil || scope == nil { | ||
| if scope == nil { | ||
| return nil | ||
| } | ||
| return client.Recover(err, &EventHint{RecoveredException: err}, scope) | ||
|
|
@@ -335,7 +325,7 @@ func (hub *Hub) RecoverWithContext(ctx context.Context, err interface{}) *EventI | |
| err = recover() | ||
| } | ||
| client, scope := hub.Client(), hub.Scope() | ||
| if client == nil || scope == nil { | ||
| if scope == nil { | ||
| return nil | ||
| } | ||
| return client.RecoverWithContext(ctx, err, &EventHint{RecoveredException: err}, scope) | ||
|
|
@@ -353,13 +343,7 @@ func (hub *Hub) RecoverWithContext(ctx context.Context, err interface{}) *EventI | |
| // the network synchronously, configure it to use the HTTPSyncTransport in the | ||
| // call to Init. | ||
| func (hub *Hub) Flush(timeout time.Duration) bool { | ||
| client := hub.Client() | ||
|
|
||
| if client == nil { | ||
| return false | ||
| } | ||
|
|
||
| return client.Flush(timeout) | ||
| return hub.Client().Flush(timeout) | ||
| } | ||
|
|
||
| // FlushWithContext waits until the underlying Transport sends any buffered events | ||
|
|
@@ -375,13 +359,7 @@ func (hub *Hub) Flush(timeout time.Duration) bool { | |
| // configure the SDK to use HTTPSyncTransport during initialization with Init. | ||
|
|
||
| func (hub *Hub) FlushWithContext(ctx context.Context) bool { | ||
| client := hub.Client() | ||
|
|
||
| if client == nil { | ||
| return false | ||
| } | ||
|
|
||
| return client.FlushWithContext(ctx) | ||
| return hub.Client().FlushWithContext(ctx) | ||
| } | ||
|
|
||
| // GetTraceparent returns the current Sentry traceparent string, to be used as a HTTP header value | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check-in succeeds on disabled client
Medium Severity
CaptureCheckInstill builds a check-in and returns its ID after the hub nil-client guard was removed.CaptureEventnow drops the payload when the client is disabled, so callers get a non-nil ID even though nothing was captured. Cron flows that treat a returned ID as success will look healthy while monitors never receive the check-in.Additional Locations (1)
hub.go#L272-L276Reviewed by Cursor Bugbot for commit 8fc2ecf. Configure here.