@@ -15,6 +15,10 @@ import (
1515
1616 "github.com/getsentry/sentry-go/internal/debug"
1717 "github.com/getsentry/sentry-go/internal/debuglog"
18+ httpInternal "github.com/getsentry/sentry-go/internal/http"
19+ "github.com/getsentry/sentry-go/internal/protocol"
20+ "github.com/getsentry/sentry-go/internal/ratelimit"
21+ "github.com/getsentry/sentry-go/internal/telemetry"
1822)
1923
2024// The identifier of the SDK.
@@ -252,6 +256,8 @@ type ClientOptions struct {
252256 // IMPORTANT: to not ignore any status codes, the option should be an empty slice and not nil. The nil option is
253257 // used for defaulting to 404 ignores.
254258 TraceIgnoreStatusCodes [][]int
259+ // DisableTelemetryBuffer disables the telemetry buffer layer for prioritizing events and uses the old transport layer.
260+ DisableTelemetryBuffer bool
255261}
256262
257263// Client is the underlying processor that is used by the main API and Hub
@@ -266,8 +272,9 @@ type Client struct {
266272 sdkVersion string
267273 // Transport is read-only. Replacing the transport of an existing client is
268274 // not supported, create a new client instead.
269- Transport Transport
270- batchLogger * BatchLogger
275+ Transport Transport
276+ batchLogger * BatchLogger
277+ telemetryBuffer * telemetry.Buffer
271278}
272279
273280// NewClient creates and returns an instance of Client configured using
@@ -371,12 +378,15 @@ func NewClient(options ClientOptions) (*Client, error) {
371378 sdkVersion : SDKVersion ,
372379 }
373380
374- if options .EnableLogs {
381+ client .setupTransport ()
382+
383+ if ! options .DisableTelemetryBuffer {
384+ client .setupTelemetryBuffer ()
385+ } else if options .EnableLogs {
375386 client .batchLogger = NewBatchLogger (& client )
376387 client .batchLogger .Start ()
377388 }
378389
379- client .setupTransport ()
380390 client .setupIntegrations ()
381391
382392 return & client , nil
@@ -398,6 +408,52 @@ func (client *Client) setupTransport() {
398408 client .Transport = transport
399409}
400410
411+ func (client * Client ) setupTelemetryBuffer () {
412+ if client .options .DisableTelemetryBuffer {
413+ return
414+ }
415+
416+ if client .dsn == nil {
417+ debuglog .Println ("Telemetry buffer disabled: no DSN configured" )
418+ return
419+ }
420+
421+ // We currently disallow using custom Transport with the new Telemetry Buffer, due to the difference in transport signatures.
422+ // The option should be enabled when the new Transport interface signature changes.
423+ if client .options .Transport != nil {
424+ debuglog .Println ("Cannot enable Telemetry Buffer with custom Transport: fallback to old transport" )
425+ if client .options .EnableLogs {
426+ client .batchLogger = NewBatchLogger (client )
427+ client .batchLogger .Start ()
428+ }
429+ return
430+ }
431+
432+ transport := httpInternal .NewAsyncTransport (httpInternal.TransportOptions {
433+ Dsn : client .options .Dsn ,
434+ HTTPClient : client .options .HTTPClient ,
435+ HTTPTransport : client .options .HTTPTransport ,
436+ HTTPProxy : client .options .HTTPProxy ,
437+ HTTPSProxy : client .options .HTTPSProxy ,
438+ CaCerts : client .options .CaCerts ,
439+ })
440+ client .Transport = & internalAsyncTransportAdapter {transport : transport }
441+
442+ storage := map [ratelimit.Category ]telemetry.Storage [protocol.EnvelopeItemConvertible ]{
443+ ratelimit .CategoryError : telemetry .NewRingBuffer [protocol.EnvelopeItemConvertible ](ratelimit .CategoryError , 100 , telemetry .OverflowPolicyDropOldest , 1 , 0 ),
444+ ratelimit .CategoryTransaction : telemetry .NewRingBuffer [protocol.EnvelopeItemConvertible ](ratelimit .CategoryTransaction , 1000 , telemetry .OverflowPolicyDropOldest , 1 , 0 ),
445+ ratelimit .CategoryLog : telemetry .NewRingBuffer [protocol.EnvelopeItemConvertible ](ratelimit .CategoryLog , 10 * 100 , telemetry .OverflowPolicyDropOldest , 100 , 5 * time .Second ),
446+ ratelimit .CategoryMonitor : telemetry .NewRingBuffer [protocol.EnvelopeItemConvertible ](ratelimit .CategoryMonitor , 100 , telemetry .OverflowPolicyDropOldest , 1 , 0 ),
447+ }
448+
449+ sdkInfo := & protocol.SdkInfo {
450+ Name : client .sdkIdentifier ,
451+ Version : client .sdkVersion ,
452+ }
453+
454+ client .telemetryBuffer = telemetry .NewBuffer (storage , transport , & client .dsn .Dsn , sdkInfo )
455+ }
456+
401457func (client * Client ) setupIntegrations () {
402458 integrations := []Integration {
403459 new (contextifyFramesIntegration ),
@@ -538,7 +594,7 @@ func (client *Client) RecoverWithContext(
538594// the network synchronously, configure it to use the HTTPSyncTransport in the
539595// call to Init.
540596func (client * Client ) Flush (timeout time.Duration ) bool {
541- if client .batchLogger != nil {
597+ if client .batchLogger != nil || client . telemetryBuffer != nil {
542598 ctx , cancel := context .WithTimeout (context .Background (), timeout )
543599 defer cancel ()
544600 return client .FlushWithContext (ctx )
@@ -562,6 +618,9 @@ func (client *Client) FlushWithContext(ctx context.Context) bool {
562618 if client .batchLogger != nil {
563619 client .batchLogger .Flush (ctx .Done ())
564620 }
621+ if client .telemetryBuffer != nil {
622+ return client .telemetryBuffer .FlushWithContext (ctx )
623+ }
565624 return client .Transport .FlushWithContext (ctx )
566625}
567626
@@ -570,6 +629,12 @@ func (client *Client) FlushWithContext(ctx context.Context) bool {
570629// Close should be called after Flush and before terminating the program
571630// otherwise some events may be lost.
572631func (client * Client ) Close () {
632+ if client .telemetryBuffer != nil {
633+ client .telemetryBuffer .Close (5 * time .Second )
634+ }
635+ if client .batchLogger != nil {
636+ client .batchLogger .Shutdown ()
637+ }
573638 client .Transport .Close ()
574639}
575640
@@ -690,7 +755,13 @@ func (client *Client) processEvent(event *Event, hint *EventHint, scope EventMod
690755 }
691756 }
692757
693- client .Transport .SendEvent (event )
758+ if client .telemetryBuffer != nil {
759+ if ! client .telemetryBuffer .Add (event ) {
760+ debuglog .Println ("Event dropped: telemetry buffer full or unavailable" )
761+ }
762+ } else {
763+ client .Transport .SendEvent (event )
764+ }
694765
695766 return & event .EventID
696767}
0 commit comments