From 5df4b882d06b8506a1e5b4f15ff2651b68423c26 Mon Sep 17 00:00:00 2001 From: Roberto Bampi Date: Tue, 25 Nov 2014 13:36:08 +0100 Subject: [PATCH] timeout is now configurable and documented --- hooks/sentry/README.md | 19 +++++++++++++++++++ hooks/sentry/sentry.go | 27 ++++++++++++++++----------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/hooks/sentry/README.md b/hooks/sentry/README.md index ba64ed4..a409f3b 100644 --- a/hooks/sentry/README.md +++ b/hooks/sentry/README.md @@ -40,3 +40,22 @@ When logs are sent to sentry these fields are treated differently. is logging the event (hostname.example.com) - logger is the part of the application which is logging the event. In go this usually means setting it to the name of the package. + +## Timeout + +`Timeout` is the time the sentry hook will wait for a response +from the sentry server. + +If this time elapses with no response from +the server an error will be returned. + +If `Timeout` is set to 0 the SentryHook will not wait for a reply +and will assume a correct delivery. + +The SentryHook has a default timeout of `100 milliseconds` when created +with a call to `NewSentryHook`. This can be changed by assigning a value to the `Timeout` field: + +```go +hook, _ := logrus_sentry.NewSentryHook(...) +hook.Timeout = 20*time.Seconds +``` diff --git a/hooks/sentry/sentry.go b/hooks/sentry/sentry.go index c91a4b6..379f281 100644 --- a/hooks/sentry/sentry.go +++ b/hooks/sentry/sentry.go @@ -8,10 +8,6 @@ import ( "github.com/getsentry/raven-go" ) -const ( - timeout = 100 * time.Millisecond -) - var ( severityMap = map[logrus.Level]raven.Severity{ logrus.DebugLevel: raven.DEBUG, @@ -42,18 +38,24 @@ func getAndDel(d logrus.Fields, key string) (string, bool) { // SentryHook delivers logs to a sentry server. type SentryHook struct { + // Timeout sets the time to wait for a delivery error from the sentry server. + // If this is set to zero the server will not wait for any response and will + // consider the message correctly sent + Timeout time.Duration + client *raven.Client levels []logrus.Level } // NewSentryHook creates a hook to be added to an instance of logger // and initializes the raven client. +// This method sets the timeout to 100 milliseconds. func NewSentryHook(DSN string, levels []logrus.Level) (*SentryHook, error) { client, err := raven.NewClient(DSN, nil) if err != nil { return nil, err } - return &SentryHook{client, levels}, nil + return &SentryHook{100 * time.Millisecond, client, levels}, nil } // Called when an event should be sent to sentry @@ -79,12 +81,15 @@ func (hook *SentryHook) Fire(entry *logrus.Entry) error { packet.Extra = map[string]interface{}(d) _, errCh := hook.client.Capture(packet, nil) - timeoutCh := time.After(timeout) - select { - case err := <-errCh: - return err - case <-timeoutCh: - return fmt.Errorf("no response from sentry server in %s", timeout) + timeout := hook.Timeout + if timeout != 0 { + timeoutCh := time.After(timeout) + select { + case err := <-errCh: + return err + case <-timeoutCh: + return fmt.Errorf("no response from sentry server in %s", timeout) + } } return nil }