Skip to content

Commit

Permalink
middleware: set sentry flush timeout 5 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
pilinux committed Jul 25, 2024
1 parent 10e0714 commit 14ca876
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
45 changes: 45 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/middleware/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package middleware
import (
"net/http"
"strconv"
"time"

"github.com/gin-gonic/gin"
"github.com/pilinux/logrus/sentry"
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 33 additions & 16 deletions lib/middleware/sentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
"time"

Expand All @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
}
}()
}
}

0 comments on commit 14ca876

Please sign in to comment.