@@ -240,6 +240,28 @@ type Client struct {
240240// single goroutine) or hub methods (for concurrent programs, for example web
241241// servers).
242242func NewClient (options ClientOptions ) (* Client , error ) {
243+ // The default error event sample rate for all SDKs is 1.0 (send all).
244+ //
245+ // In Go, the zero value (default) for float64 is 0.0, which means that
246+ // constructing a client with NewClient(ClientOptions{}), or, equivalently,
247+ // initializing the SDK with Init(ClientOptions{}) without an explicit
248+ // SampleRate would drop all events.
249+ //
250+ // To retain the desired default behavior, we exceptionally flip SampleRate
251+ // from 0.0 to 1.0 here. Setting the sample rate to 0.0 is not very useful
252+ // anyway, and the same end result can be achieved in many other ways like
253+ // not initializing the SDK, setting the DSN to the empty string or using an
254+ // event processor that always returns nil.
255+ //
256+ // An alternative API could be such that default options don't need to be
257+ // the same as Go's zero values, for example using the Functional Options
258+ // pattern. That would either require a breaking change if we want to reuse
259+ // the obvious NewClient name, or a new function as an alternative
260+ // constructor.
261+ if options .SampleRate == 0.0 {
262+ options .SampleRate = 1.0
263+ }
264+
243265 if options .Debug {
244266 debugWriter := options .DebugWriter
245267 if debugWriter == nil {
@@ -374,8 +396,8 @@ func (client *Client) AddEventProcessor(processor EventProcessor) {
374396}
375397
376398// Options return ClientOptions for the current Client.
377- // TODO don't access this internally to avoid creating a copy each time.
378399func (client Client ) Options () ClientOptions {
400+ // Note: internally, consider using `client.options` instead of `client.Options()` to avoid copying the object each time.
379401 return client .options
380402}
381403
@@ -476,7 +498,7 @@ func (client *Client) EventFromMessage(message string, level Level) *Event {
476498 event .Level = level
477499 event .Message = message
478500
479- if client .Options () .AttachStacktrace {
501+ if client .options .AttachStacktrace {
480502 event .Threads = []Thread {{
481503 Stacktrace : NewStacktrace (),
482504 Crashed : false ,
@@ -516,34 +538,10 @@ func (client *Client) processEvent(event *Event, hint *EventHint, scope EventMod
516538 return client .CaptureException (err , hint , scope )
517539 }
518540
519- options := client .Options ()
520-
521- // The default error event sample rate for all SDKs is 1.0 (send all).
522- //
523- // In Go, the zero value (default) for float64 is 0.0, which means that
524- // constructing a client with NewClient(ClientOptions{}), or, equivalently,
525- // initializing the SDK with Init(ClientOptions{}) without an explicit
526- // SampleRate would drop all events.
527- //
528- // To retain the desired default behavior, we exceptionally flip SampleRate
529- // from 0.0 to 1.0 here. Setting the sample rate to 0.0 is not very useful
530- // anyway, and the same end result can be achieved in many other ways like
531- // not initializing the SDK, setting the DSN to the empty string or using an
532- // event processor that always returns nil.
533- //
534- // An alternative API could be such that default options don't need to be
535- // the same as Go's zero values, for example using the Functional Options
536- // pattern. That would either require a breaking change if we want to reuse
537- // the obvious NewClient name, or a new function as an alternative
538- // constructor.
539- if options .SampleRate == 0.0 {
540- options .SampleRate = 1.0
541- }
542-
543541 // Transactions are sampled by options.TracesSampleRate or
544542 // options.TracesSampler when they are started. All other events
545543 // (errors, messages) are sampled here.
546- if event .Type != transactionType && ! sample (options .SampleRate ) {
544+ if event .Type != transactionType && ! sample (client . options .SampleRate ) {
547545 Logger .Println ("Event dropped due to SampleRate hit." )
548546 return nil
549547 }
@@ -556,15 +554,15 @@ func (client *Client) processEvent(event *Event, hint *EventHint, scope EventMod
556554 if hint == nil {
557555 hint = & EventHint {}
558556 }
559- if event .Type == transactionType && options .BeforeSendTransaction != nil {
557+ if event .Type == transactionType && client . options .BeforeSendTransaction != nil {
560558 // Transaction events
561- if event = options .BeforeSendTransaction (event , hint ); event == nil {
559+ if event = client . options .BeforeSendTransaction (event , hint ); event == nil {
562560 Logger .Println ("Transaction dropped due to BeforeSendTransaction callback." )
563561 return nil
564562 }
565- } else if event .Type != transactionType && options .BeforeSend != nil {
563+ } else if event .Type != transactionType && client . options .BeforeSend != nil {
566564 // All other events
567- if event = options .BeforeSend (event , hint ); event == nil {
565+ if event = client . options .BeforeSend (event , hint ); event == nil {
568566 Logger .Println ("Event dropped due to BeforeSend callback." )
569567 return nil
570568 }
@@ -590,23 +588,23 @@ func (client *Client) prepareEvent(event *Event, hint *EventHint, scope EventMod
590588 }
591589
592590 if event .ServerName == "" {
593- event .ServerName = client .Options () .ServerName
591+ event .ServerName = client .options .ServerName
594592
595593 if event .ServerName == "" {
596594 event .ServerName = hostname
597595 }
598596 }
599597
600598 if event .Release == "" {
601- event .Release = client .Options () .Release
599+ event .Release = client .options .Release
602600 }
603601
604602 if event .Dist == "" {
605- event .Dist = client .Options () .Dist
603+ event .Dist = client .options .Dist
606604 }
607605
608606 if event .Environment == "" {
609- event .Environment = client .Options () .Environment
607+ event .Environment = client .options .Environment
610608 }
611609
612610 event .Platform = "go"
0 commit comments