Skip to content

Commit

Permalink
feat: Use noopTransport when no DSN provided
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Jul 10, 2019
1 parent f51b5a7 commit d92f0f2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
12 changes: 7 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ func NewClient(options ClientOptions) (*Client, error) {
if err != nil {
return nil, err
}
} else {
Logger.Println("Sentry client initialized with an empty DSN")
}

client := Client{
Expand All @@ -146,7 +144,11 @@ func (client *Client) setupTransport() {
transport := client.options.Transport

if transport == nil {
transport = NewHTTPTransport()
if client.options.Dsn == "" {
transport = new(noopTransport)
} else {
transport = NewHTTPTransport()
}
}

transport.Configure(client.options)
Expand Down Expand Up @@ -315,7 +317,7 @@ func (client *Client) processEvent(event *Event, hint *EventHint, scope EventMod
if options.SampleRate != 0.0 {
randomFloat := rand.New(rand.NewSource(time.Now().UnixNano())).Float32()
if randomFloat > options.SampleRate {
Logger.Println("Event dropped due to SampleRate hit")
Logger.Println("Event dropped due to SampleRate hit.")
return nil
}
}
Expand All @@ -330,7 +332,7 @@ func (client *Client) processEvent(event *Event, hint *EventHint, scope EventMod
h = hint
}
if event = options.BeforeSend(event, h); event == nil {
Logger.Println("Event dropped due to BeforeSend callback")
Logger.Println("Event dropped due to BeforeSend callback.")
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (hub *Hub) AddBreadcrumb(breadcrumb *Breadcrumb, hint *BreadcrumbHint) {
h = hint
}
if breadcrumb = options.BeforeBreadcrumb(breadcrumb, h); breadcrumb == nil {
Logger.Println("breadcrumb dropped due to BeforeBreadcrumb callback")
Logger.Println("breadcrumb dropped due to BeforeBreadcrumb callback.")
return
}
}
Expand Down
26 changes: 23 additions & 3 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (t *HTTPTransport) SendEvent(event *Event) {
)
default:
t.wg.Done()
Logger.Println("Event dropped due to transport buffer being full")
Logger.Println("Event dropped due to transport buffer being full.")
// worker would block, drop the packet
}
}
Expand All @@ -173,10 +173,10 @@ func (t *HTTPTransport) Flush(timeout time.Duration) bool {

select {
case <-c:
Logger.Println("Buffer flushed successfully")
Logger.Println("Buffer flushed successfully.")
return true
case <-time.After(timeout):
Logger.Println("Buffer flushing reached the timeout")
Logger.Println("Buffer flushing reached the timeout.")
return false
}
}
Expand Down Expand Up @@ -294,3 +294,23 @@ func (t *HTTPSyncTransport) SendEvent(event *Event) {
func (t *HTTPSyncTransport) Flush(_ time.Duration) bool {
return true
}

// ================================
// noopTransport
// ================================

// noopTransport is an implementation of `Transport` interface which drops all the events.
// Only used internally when an empty DSN is provided, which effectively disables the SDK.
type noopTransport struct{}

func (t *noopTransport) Configure(options ClientOptions) {
Logger.Println("Sentry client initialized with an empty DSN. Using noopTransport. No events will be delivered.")
}

func (t *noopTransport) SendEvent(event *Event) {
Logger.Println("Event dropped due to noopTransport usage.")
}

func (t *noopTransport) Flush(_ time.Duration) bool {
return true
}

0 comments on commit d92f0f2

Please sign in to comment.