ソースを参照

feat: support for sentry

Yeuoly 7 ヶ月 前
コミット
9ef9f16e6e
共有6 個のファイルを変更した38 個の追加2 個の削除を含む
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 7 0
      internal/server/http_server.go
  4. 12 1
      internal/server/server.go
  5. 7 0
      internal/types/app/config.go
  6. 9 1
      internal/utils/routine/pool.go

+ 1 - 0
go.mod

@@ -44,6 +44,7 @@ require (
 	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
 	github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
 	github.com/fsnotify/fsnotify v1.7.0 // indirect
+	github.com/getsentry/sentry-go v0.30.0 // indirect
 	github.com/go-git/go-git v4.7.0+incompatible // indirect
 	github.com/hashicorp/hcl v1.0.0 // indirect
 	github.com/inconshreveable/mousetrap v1.1.0 // indirect

+ 2 - 0
go.sum

@@ -85,6 +85,8 @@ github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy
 github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
 github.com/gammazero/deque v0.2.1 h1:qSdsbG6pgp6nL7A0+K/B7s12mcCY/5l5SIUpMOl+dC0=
 github.com/gammazero/deque v0.2.1/go.mod h1:LFroj8x4cMYCukHJDbxFCkT+r9AndaJnFMuZDV34tuU=
+github.com/getsentry/sentry-go v0.30.0 h1:lWUwDnY7sKHaVIoZ9wYqRHJ5iEmoc0pqcRqFkosKzBo=
+github.com/getsentry/sentry-go v0.30.0/go.mod h1:WU9B9/1/sHDqeV8T+3VwwbjeR5MSXs/6aqG3mqZrezA=
 github.com/gin-contrib/gzip v1.0.1 h1:HQ8ENHODeLY7a4g1Au/46Z92bdGFl74OhxcZble9WJE=
 github.com/gin-contrib/gzip v1.0.1/go.mod h1:njt428fdUNRvjuJf16tZMYZ2Yl+WQB53X5wmhDwXvC4=
 github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=

+ 7 - 0
internal/server/http_server.go

@@ -13,10 +13,17 @@ import (
 	"github.com/langgenius/dify-plugin-daemon/internal/service"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/app"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
+
+	sentrygin "github.com/getsentry/sentry-go/gin"
 )
 
 func (app *App) server(config *app.Config) func() {
 	engine := gin.Default()
+	if config.SentryEnabled {
+		engine.Use(sentrygin.New(sentrygin.Options{
+			Repanic: true,
+		}))
+	}
 	engine.GET("/health/check", controllers.HealthCheck)
 
 	app.endpointGroup(engine.Group("/e"), config)

+ 12 - 1
internal/server/server.go

@@ -1,6 +1,7 @@
 package server
 
 import (
+	"github.com/getsentry/sentry-go"
 	"github.com/langgenius/dify-plugin-daemon/internal/cluster"
 	"github.com/langgenius/dify-plugin-daemon/internal/core/persistence"
 	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
@@ -38,7 +39,17 @@ func initOSS(config *app.Config) oss.OSS {
 
 func (app *App) Run(config *app.Config) {
 	// init routine pool
-	routine.InitPool(config.RoutinePoolSize)
+	if config.SentryEnabled {
+		routine.InitPool(config.RoutinePoolSize, sentry.ClientOptions{
+			Dsn:              config.SentryDSN,
+			AttachStacktrace: config.SentryAttachStacktrace,
+			TracesSampleRate: config.SentryTracesSampleRate,
+			SampleRate:       config.SentrySampleRate,
+			EnableTracing:    config.SentryTracingEnabled,
+		})
+	} else {
+		routine.InitPool(config.RoutinePoolSize)
+	}
 
 	// init db
 	db.Init(config)

+ 7 - 0
internal/types/app/config.go

@@ -86,6 +86,13 @@ type Config struct {
 	DisplayClusterLog bool `envconfig:"DISPLAY_CLUSTER_LOG"`
 
 	PPROFEnabled bool `envconfig:"PPROF_ENABLED"`
+
+	SentryEnabled          bool    `envconfig:"SENTRY_ENABLED"`
+	SentryDSN              string  `envconfig:"SENTRY_DSN"`
+	SentryAttachStacktrace bool    `envconfig:"SENTRY_ATTACH_STACKTRACE"`
+	SentryTracingEnabled   bool    `envconfig:"SENTRY_TRACING_ENABLED"`
+	SentryTracesSampleRate float64 `envconfig:"SENTRY_TRACES_SAMPLE_RATE"`
+	SentrySampleRate       float64 `envconfig:"SENTRY_SAMPLE_RATE"`
 }
 
 func (c *Config) Validate() error {

+ 9 - 1
internal/utils/routine/pool.go

@@ -6,6 +6,7 @@ import (
 	"sync"
 	"sync/atomic"
 
+	"github.com/getsentry/sentry-go"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
 	"github.com/panjf2000/ants"
 )
@@ -21,7 +22,7 @@ func IsInit() bool {
 	return p != nil
 }
 
-func InitPool(size int) {
+func InitPool(size int, sentryOption ...sentry.ClientOptions) {
 	l.Lock()
 	defer l.Unlock()
 	if p != nil {
@@ -29,6 +30,12 @@ func InitPool(size int) {
 	}
 	log.Info("init routine pool, size: %d", size)
 	p, _ = ants.NewPool(size, ants.WithNonblocking(false))
+
+	if len(sentryOption) > 0 {
+		if err := sentry.Init(sentryOption[0]); err != nil {
+			log.Error("init sentry failed, error: %v", err)
+		}
+	}
 }
 
 func Submit(labels map[string]string, f func()) {
@@ -44,6 +51,7 @@ func Submit(labels map[string]string, f func()) {
 			}
 		}
 		pprof.Do(context.Background(), pprof.Labels(label...), func(ctx context.Context) {
+			defer sentry.Recover()
 			f()
 		})
 	})