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: Use noopTransport when no DSN provided #27

Merged
merged 2 commits into from
Jul 10, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,13 @@ func NewClient(options ClientOptions) (*Client, error) {
options.Environment = os.Getenv("SENTRY_ENVIRONMENT")
}

dsn, err := NewDsn(options.Dsn)

if err != nil {
return nil, err
}

if dsn == nil {
Logger.Println("Sentry client initialized with an empty DSN")
var dsn *Dsn
if options.Dsn != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we look for SENTRY_DSN env var before hitting this?

var err error
dsn, err = NewDsn(options.Dsn)
if err != nil {
return nil, err
}
}

client := Client{
Expand All @@ -145,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 @@ -314,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 @@ -329,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
13 changes: 13 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import (
"testing"
)

func TestNewClientAllowsEmptyDSN(t *testing.T) {
transport := &TransportMock{}
client, err := NewClient(ClientOptions{
Transport: transport,
})
if err != nil {
t.Fatalf("expected no error when creating client without a DNS but got %v", err)
}

client.CaptureException(errors.New("custom error"), nil, &ScopeMock{})
assertEqual(t, transport.lastEvent.Exception[0].Value, "custom error")
}

type customComplexError struct {
Message string
}
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
}