Skip to content

Commit

Permalink
Add Flush() function
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Nov 30, 2016
1 parent 1080757 commit 7221715
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type SentryHook struct {
asynchronous bool
buf chan *raven.Packet
wg sync.WaitGroup
mu sync.RWMutex
}

// The Stacktracer interface allows an error type to return a raven.Stacktrace.
Expand Down Expand Up @@ -154,6 +155,8 @@ func setAsync(hook *SentryHook) *SentryHook {
// are extracted from entry.Data (if they are found)
// These fields are: error, logger, server_name, http_request, tags
func (hook *SentryHook) Fire(entry *logrus.Entry) error {
hook.mu.RLock() // Allow multiple go routines to log simultaneously
defer hook.mu.RUnlock()
packet := raven.NewPacket(entry.Message)
packet.Timestamp = raven.Timestamp(entry.Time)
packet.Level = severityMap[entry.Level]
Expand Down Expand Up @@ -227,6 +230,18 @@ func (hook *SentryHook) fire() {
}
}

// Flush waits for the log queue to empty. This function only does anything in
// asynchronous mode.
func (hook *SentryHook) Flush() {
if !hook.asynchronous {
return
}
hook.mu.Lock() // Claim exclusive access; any logging goroutines will block until the flush completes
defer hook.mu.Unlock()

hook.wg.Wait()
}

func (hook *SentryHook) sendPacket(packet *raven.Packet) error {
_, errCh := hook.client.Capture(packet, nil)
timeout := hook.Timeout
Expand Down

0 comments on commit 7221715

Please sign in to comment.