From 14ca87666607208d61cda18782cf119f377052bc Mon Sep 17 00:00:00 2001 From: pilinux Date: Thu, 25 Jul 2024 16:47:14 +0200 Subject: [PATCH] middleware: set sentry flush timeout 5 seconds --- example/main.go | 45 ++++++++++++++++++++++++++++++++ lib/middleware/sentry.go | 2 ++ lib/middleware/sentry_test.go | 49 +++++++++++++++++++++++------------ 3 files changed, 80 insertions(+), 16 deletions(-) diff --git a/example/main.go b/example/main.go index f379e3b..1b2fd3d 100644 --- a/example/main.go +++ b/example/main.go @@ -104,6 +104,51 @@ func main() { } } + // example of using sentry in separate goroutines + /* + var GoroutineLogger *log.Logger + sentryHook, err := middleware.InitSentry( + configure.Logger.SentryDsn, + configure.Server.ServerEnv, + configure.Version, + configure.Logger.PerformanceTracing, + configure.Logger.TracesSampleRate, + ) + if err != nil { + fmt.Println(err) + } + if err == nil { + sentryHook.SetFlushTimeout(5 * time.Second) + defer sentryHook.Flush() + GoroutineLogger = log.New() + GoroutineLogger.AddHook(sentryHook) + } + if GoroutineLogger == nil { + fmt.Println("failed to create a logger for separate goroutines") + } + if GoroutineLogger != nil { + if configure.Logger.SentryDsn != "" { + i := 0 + for { + i++ + ref := fmt.Sprintf("goroutine - %d", i) + fmt.Println("ref:", ref) + go func() { + fmt.Println("testing sentry integration in a separate goroutine") + GoroutineLogger. + WithFields(log.Fields{ + "time": time.Now().Format(time.RFC3339), + "ref": ref, + }). + Info("testing sentry integration in a separate goroutine") + }() + + time.Sleep(5 * time.Second) + } + } + } + */ + r, err := router.SetupRouter(configure) if err != nil { fmt.Println(err) diff --git a/lib/middleware/sentry.go b/lib/middleware/sentry.go index 54fa947..997e3bd 100644 --- a/lib/middleware/sentry.go +++ b/lib/middleware/sentry.go @@ -7,6 +7,7 @@ package middleware import ( "net/http" "strconv" + "time" "github.com/gin-gonic/gin" "github.com/pilinux/logrus/sentry" @@ -94,6 +95,7 @@ func SentryCapture(sentryDsn string, v ...string) gin.HandlerFunc { sentryHook.AddTag("host", c.Request.Host) sentryHook.AddTag("remote.addr", c.Request.RemoteAddr) sentryHook.AddTag("user.agent", c.Request.UserAgent()) + sentryHook.SetFlushTimeout(5 * time.Second) defer sentryHook.Flush() log.AddHook(sentryHook) diff --git a/lib/middleware/sentry_test.go b/lib/middleware/sentry_test.go index 98b6906..0dd6b5a 100644 --- a/lib/middleware/sentry_test.go +++ b/lib/middleware/sentry_test.go @@ -4,6 +4,7 @@ import ( "net/http" "net/http/httptest" "os" + "sync" "testing" "time" @@ -27,6 +28,7 @@ func TestSentryCapture(t *testing.T) { router.Use(middleware.SentryCapture(sentryDSN, "production", "v0.0.1", "yes", "1.0")) // check sentry in a separate goroutine + var wg sync.WaitGroup var GoroutineLogger *log.Logger sentryHook, err := middleware.InitSentry( sentryDSN, @@ -39,20 +41,30 @@ func TestSentryCapture(t *testing.T) { t.Errorf("failed to initialize sentry for separate goroutines") } if err == nil { + sentryHook.SetFlushTimeout(5 * time.Second) defer sentryHook.Flush() GoroutineLogger = log.New() GoroutineLogger.AddHook(sentryHook) } - go func() { + if GoroutineLogger == nil { + t.Errorf("failed to create a logger for separate goroutines") + } + + if GoroutineLogger != nil { if sentryDSN != "" { - GoroutineLogger. - WithFields(log.Fields{ - "time": time.Now().Format(time.RFC3339), - "ref": "goroutine - 1", - }). - Info("testing sentry integration in a separate goroutine") + wg.Add(1) + go func() { + defer wg.Done() + GoroutineLogger. + WithFields(log.Fields{ + "time": time.Now().Format(time.RFC3339), + "ref": "goroutine - 1", + }). + Info("testing sentry integration in a separate goroutine") + }() + wg.Wait() } - }() + } // define test route router.GET("/", func(c *gin.Context) { @@ -85,14 +97,19 @@ func TestSentryCapture(t *testing.T) { } // check sentry in another goroutine - go func() { + if GoroutineLogger != nil { if sentryDSN != "" { - GoroutineLogger. - WithFields(log.Fields{ - "time": time.Now().Format(time.RFC3339), - "ref": "goroutine - 2", - }). - Info("testing sentry integration in a separate goroutine") + wg.Add(1) + go func() { + defer wg.Done() + GoroutineLogger. + WithFields(log.Fields{ + "time": time.Now().Format(time.RFC3339), + "ref": "goroutine - 2", + }). + Info("testing sentry integration in a separate goroutine") + }() + wg.Wait() } - }() + } }