Skip to content

Commit

Permalink
middleware: refactor sentry logger
Browse files Browse the repository at this point in the history
  • Loading branch information
pilinux committed Jul 25, 2024
1 parent 6a68d07 commit 4f9db84
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 38 deletions.
89 changes: 52 additions & 37 deletions lib/middleware/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,57 @@ import (
log "github.com/sirupsen/logrus"
)

// SentryCapture - capture errors and forward to sentry.io
// InitSentry - initialize sentry for middleware or separate goroutines
//
// required parameter (1st parameter): sentryDsn
//
// optional parameter (2nd parameter): environment (development or production)
//
// optional parameter (3rd parameter): release version or git commit number
//
// optional parameter (4th parameter): enableTracing (yes or no)
//
// optional parameter (5th parameter): tracesSampleRate (0.0 - 1.0)
func InitSentry(sentryDsn string, v ...string) (*sentry.Hook, error) {
sentryDebugMode := true
environment := "development" // default
release := ""
enableTracing := false
tracesSampleRate := 0.0
if len(v) >= 1 {
environment = v[0]
if environment == "production" {
sentryDebugMode = false
}
}
if len(v) > 1 {
release = v[1]
}
if len(v) > 2 {
if v[2] == "yes" {
enableTracing = true
}
}
if len(v) > 3 {
if enableTracing {
sampleRate, err := strconv.ParseFloat(v[3], 64)
if err == nil {
tracesSampleRate = sampleRate
}
}
}

return sentry.NewHook(sentry.Options{
Dsn: sentryDsn,
Debug: sentryDebugMode,
Environment: environment,
Release: release,
EnableTracing: enableTracing,
TracesSampleRate: tracesSampleRate,
})
}

// SentryCapture - sentry middleware to capture errors and forward to sentry.io
//
// required parameter (1st parameter): sentryDsn
//
Expand All @@ -33,42 +83,7 @@ func SentryCapture(sentryDsn string, v ...string) gin.HandlerFunc {
}
}()

sentryDebugMode := true
environment := "development" // default
release := ""
enableTracing := false
tracesSampleRate := 0.0
if len(v) >= 1 {
environment = v[0]
if environment == "production" {
sentryDebugMode = false
}
}
if len(v) > 1 {
release = v[1]
}
if len(v) > 2 {
if v[2] == "yes" {
enableTracing = true
}
}
if len(v) > 3 {
if enableTracing {
sampleRate, err := strconv.ParseFloat(v[3], 64)
if err == nil {
tracesSampleRate = sampleRate
}
}
}

sentryHook, err := sentry.NewHook(sentry.Options{
Dsn: sentryDsn,
Debug: sentryDebugMode,
Environment: environment,
Release: release,
EnableTracing: enableTracing,
TracesSampleRate: tracesSampleRate,
})
sentryHook, err := InitSentry(sentryDsn, v...)
if err != nil {
// middleware -> sentry NewHook failed
c.AbortWithStatusJSON(http.StatusInternalServerError, "internal server error")
Expand Down
48 changes: 47 additions & 1 deletion lib/middleware/sentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http/httptest"
"os"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/pilinux/gorest/lib/middleware"
Expand All @@ -25,10 +26,43 @@ func TestSentryCapture(t *testing.T) {
sentryDSN := os.Getenv("TEST_SENTRY_DSN")
router.Use(middleware.SentryCapture(sentryDSN, "production", "v0.0.1", "yes", "1.0"))

// check sentry in a separate goroutine
var GoroutineLogger *log.Logger
sentryHook, err := middleware.InitSentry(
sentryDSN,
"production",
"v0.0.1",
"yes",
"1.0",
)
if err != nil {
t.Errorf("failed to initialize sentry for separate goroutines")
}
if err == nil {
defer sentryHook.Flush()
GoroutineLogger = log.New()
GoroutineLogger.AddHook(sentryHook)
}
go func() {
if sentryDSN != "" {
GoroutineLogger.
WithFields(log.Fields{
"time": time.Now().Format(time.RFC3339),
"ref": "goroutine - 1",
}).
Info("testing sentry integration in a separate goroutine")
}
}()

// define test route
router.GET("/", func(c *gin.Context) {
// send log to sentry for testing
log.Info("testing sentry integration")
log.
WithFields(log.Fields{
"time": time.Now().Format(time.RFC3339),
"ref": "middleware",
}).
Info("testing sentry integration in the middleware")
c.Status(http.StatusOK)
})

Expand All @@ -49,4 +83,16 @@ func TestSentryCapture(t *testing.T) {
if res.Code != http.StatusOK {
t.Errorf("expected response code %v, got '%v'", http.StatusOK, res.Code)
}

// check sentry in another goroutine
go func() {
if sentryDSN != "" {
GoroutineLogger.
WithFields(log.Fields{
"time": time.Now().Format(time.RFC3339),
"ref": "goroutine - 2",
}).
Info("testing sentry integration in a separate goroutine")
}
}()
}

0 comments on commit 4f9db84

Please sign in to comment.