Skip to content

Commit 97a00a4

Browse files
vaindcleptric
andauthored
refactor: use client.options internally to avoid copying (#651)
Co-authored-by: Michi Hoffmann <[email protected]>
1 parent 4a965bc commit 97a00a4

File tree

6 files changed

+41
-46
lines changed

6 files changed

+41
-46
lines changed

client.go

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,28 @@ type Client struct {
240240
// single goroutine) or hub methods (for concurrent programs, for example web
241241
// servers).
242242
func 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.
378399
func (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"

dynamic_sampling_context.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ func DynamicSamplingContextFromTransaction(span *Span) DynamicSamplingContext {
5252
}
5353
}
5454

55-
options := client.Options()
56-
5755
if traceID := span.TraceID.String(); traceID != "" {
5856
entries["trace_id"] = traceID
5957
}
@@ -66,10 +64,10 @@ func DynamicSamplingContextFromTransaction(span *Span) DynamicSamplingContext {
6664
entries["public_key"] = publicKey
6765
}
6866
}
69-
if release := options.Release; release != "" {
67+
if release := client.options.Release; release != "" {
7068
entries["release"] = release
7169
}
72-
if environment := options.Environment; environment != "" {
70+
if environment := client.options.Environment; environment != "" {
7371
entries["environment"] = environment
7472
}
7573

hub.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,16 @@ func (hub *Hub) AddBreadcrumb(breadcrumb *Breadcrumb, hint *BreadcrumbHint) {
280280
return
281281
}
282282

283-
options := client.Options()
284-
max := options.MaxBreadcrumbs
283+
max := client.options.MaxBreadcrumbs
285284
if max < 0 {
286285
return
287286
}
288287

289-
if options.BeforeBreadcrumb != nil {
288+
if client.options.BeforeBreadcrumb != nil {
290289
if hint == nil {
291290
hint = &BreadcrumbHint{}
292291
}
293-
if breadcrumb = options.BeforeBreadcrumb(breadcrumb, hint); breadcrumb == nil {
292+
if breadcrumb = client.options.BeforeBreadcrumb(breadcrumb, hint); breadcrumb == nil {
294293
Logger.Println("breadcrumb dropped due to BeforeBreadcrumb callback.")
295294
return
296295
}

integrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (iei *ignoreErrorsIntegration) Name() string {
130130
}
131131

132132
func (iei *ignoreErrorsIntegration) SetupOnce(client *Client) {
133-
iei.ignoreErrors = transformStringsIntoRegexps(client.Options().IgnoreErrors)
133+
iei.ignoreErrors = transformStringsIntoRegexps(client.options.IgnoreErrors)
134134
client.AddEventProcessor(iei.processor)
135135
}
136136

interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func NewRequest(r *http.Request) *Request {
175175
var env map[string]string
176176
headers := map[string]string{}
177177

178-
if client := CurrentHub().Client(); client != nil && client.Options().SendDefaultPII {
178+
if client := CurrentHub().Client(); client != nil && client.options.SendDefaultPII {
179179
// We read only the first Cookie header because of the specification:
180180
// https://tools.ietf.org/html/rfc6265#section-5.4
181181
// When the user agent generates an HTTP request, the user agent MUST NOT

span_recorder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type spanRecorder struct {
1717
func (r *spanRecorder) record(s *Span) {
1818
maxSpans := defaultMaxSpans
1919
if client := CurrentHub().Client(); client != nil {
20-
maxSpans = client.Options().MaxSpans
20+
maxSpans = client.options.MaxSpans
2121
}
2222
r.mu.Lock()
2323
defer r.mu.Unlock()

0 commit comments

Comments
 (0)