Skip to content

Commit

Permalink
add flushwithcontext methods on hub, client and global
Browse files Browse the repository at this point in the history
  • Loading branch information
ribice committed Dec 21, 2024
1 parent cc9bea5 commit 72dd675
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
16 changes: 16 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,22 @@ func (client *Client) Flush(timeout time.Duration) bool {
return client.Transport.Flush(timeout)
}

// FlushWithContext waits until the underlying Transport sends any buffered events
// to the Sentry server, blocking for at most the duration specified by the context.
// It returns false if the context is canceled before the events are sent. In such a case,
// some events may not be delivered.
//
// FlushWithContext should be called before terminating the program to ensure no
// events are unintentionally dropped.
//
// Avoid calling FlushWithContext indiscriminately after each call to CaptureEvent,
// CaptureException, or CaptureMessage. To send events synchronously over the network,
// configure the SDK to use HTTPSyncTransport during initialization with Init.

func (client *Client) FlushWithContext(ctx context.Context) bool {
return client.Transport.FlushWithContext(ctx)
}

// Close clean up underlying Transport resources.
//
// Close should be called after Flush and before terminating the program
Expand Down
22 changes: 22 additions & 0 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,28 @@ func (hub *Hub) Flush(timeout time.Duration) bool {
return client.Flush(timeout)
}

// FlushWithContext waits until the underlying Transport sends any buffered events
// to the Sentry server, blocking for at most the duration specified by the context.
// It returns false if the context is canceled before the events are sent. In such a case,
// some events may not be delivered.
//
// FlushWithContext should be called before terminating the program to ensure no
// events are unintentionally dropped.
//
// Avoid calling FlushWithContext indiscriminately after each call to CaptureEvent,
// CaptureException, or CaptureMessage. To send events synchronously over the network,
// configure the SDK to use HTTPSyncTransport during initialization with Init.

func (hub *Hub) FlushWithContext(ctx context.Context) bool {
client := hub.Client()

if client == nil {
return false
}

return client.FlushWithContext(ctx)
}

// GetTraceparent returns the current Sentry traceparent string, to be used as a HTTP header value
// or HTML meta tag value.
// This function is context aware, as in it either returns the traceparent based
Expand Down
7 changes: 6 additions & 1 deletion mocks_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sentry

import (
"context"
"sync"
"time"
)
Expand Down Expand Up @@ -37,10 +38,14 @@ func (t *TransportMock) SendEvent(event *Event) {
func (t *TransportMock) Flush(_ time.Duration) bool {
return true
}

func (t *TransportMock) FlushWithContext(_ context.Context) bool {
return true
}

func (t *TransportMock) Events() []*Event {
t.mu.Lock()
defer t.mu.Unlock()
return t.events
}
func (t *TransportMock) Close() {}

17 changes: 17 additions & 0 deletions sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ func Flush(timeout time.Duration) bool {
return hub.Flush(timeout)
}

// FlushWithContext waits until the underlying Transport sends any buffered events
// to the Sentry server, blocking for at most the duration specified by the context.
// It returns false if the context is canceled before the events are sent. In such a case,
// some events may not be delivered.
//
// FlushWithContext should be called before terminating the program to ensure no
// events are unintentionally dropped.
//
// Avoid calling FlushWithContext indiscriminately after each call to CaptureEvent,
// CaptureException, or CaptureMessage. To send events synchronously over the network,
// configure the SDK to use HTTPSyncTransport during initialization with Init.

func FlushWithContext(ctx context.Context) bool {
hub := CurrentHub()
return hub.FlushWithContext(ctx)
}

// LastEventID returns an ID of last captured event.
func LastEventID() EventID {
hub := CurrentHub()
Expand Down
1 change: 1 addition & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const maxDrainResponseBytes = 16 << 10
// Transport is used by the Client to deliver events to remote server.
type Transport interface {
Flush(timeout time.Duration) bool
FlushWithContext(ctx context.Context) bool
Configure(options ClientOptions)
SendEvent(event *Event)
Close()
Expand Down

0 comments on commit 72dd675

Please sign in to comment.