Skip to content

Commit 39f5cc4

Browse files
committed
feat: 后端集成sentry
1 parent 07f8d08 commit 39f5cc4

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

backend/config/config.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import (
88
)
99

1010
type Config struct {
11-
Log LogConfig `mapstructure:"log"`
12-
HTTP HTTPConfig `mapstructure:"http"`
13-
AdminPassword string `mapstructure:"admin_password"`
14-
PG PGConfig `mapstructure:"pg"`
15-
MQ MQConfig `mapstructure:"mq"`
16-
RAG RAGConfig `mapstructure:"rag"`
17-
Redis RedisConfig `mapstructure:"redis"`
18-
Auth AuthConfig `mapstructure:"auth"`
19-
S3 S3Config `mapstructure:"s3"`
20-
CaddyAPI string `mapstructure:"caddy_api"`
21-
SubnetPrefix string `mapstructure:"subnet_prefix"`
11+
Log LogConfig `mapstructure:"log"`
12+
HTTP HTTPConfig `mapstructure:"http"`
13+
AdminPassword string `mapstructure:"admin_password"`
14+
PG PGConfig `mapstructure:"pg"`
15+
MQ MQConfig `mapstructure:"mq"`
16+
RAG RAGConfig `mapstructure:"rag"`
17+
Redis RedisConfig `mapstructure:"redis"`
18+
Auth AuthConfig `mapstructure:"auth"`
19+
S3 S3Config `mapstructure:"s3"`
20+
Sentry SentryConfig `mapstructure:"sentry"`
21+
CaddyAPI string `mapstructure:"caddy_api"`
22+
SubnetPrefix string `mapstructure:"subnet_prefix"`
2223
}
2324

2425
type LogConfig struct {
@@ -74,6 +75,11 @@ type S3Config struct {
7475
SecretKey string `mapstructure:"secret_key"`
7576
}
7677

78+
type SentryConfig struct {
79+
Enabled bool `mapstructure:"enabled"`
80+
DSN string `mapstructure:"dsn"`
81+
}
82+
7783
func NewConfig() (*Config, error) {
7884
// set default config
7985
SUBNET_PREFIX := os.Getenv("SUBNET_PREFIX")
@@ -119,6 +125,10 @@ func NewConfig() (*Config, error) {
119125
AccessKey: "s3panda-wiki",
120126
SecretKey: "",
121127
},
128+
Sentry: SentryConfig{
129+
Enabled: false,
130+
DSN: "",
131+
},
122132
CaddyAPI: "/app/run/caddy-admin.sock",
123133
SubnetPrefix: "169.254.15",
124134
}
@@ -190,6 +200,13 @@ func overrideWithEnv(c *Config) {
190200
if env := os.Getenv("S3_ENDPOINT"); env != "" {
191201
c.S3.Endpoint = env
192202
}
203+
// sentry
204+
if env := os.Getenv("SENTRY_ENABLED"); env != "" {
205+
c.Sentry.Enabled = env == "true"
206+
}
207+
if env := os.Getenv("SENTRY_DSN"); env != "" {
208+
c.Sentry.DSN = env
209+
}
193210
}
194211

195212
func (*Config) GetString(key string) string {

backend/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ require (
1818
github.com/chaitin/pandawiki/sdk/rag v0.0.0-20250821111634-e0b93366d1c0
1919
github.com/cloudwego/eino v0.3.51
2020
github.com/cloudwego/eino-ext/components/model/deepseek v0.0.0-20250522060253-ddb617598b09
21+
github.com/getsentry/sentry-go v0.35.1
22+
github.com/getsentry/sentry-go/echo v0.35.1
2123
github.com/go-ldap/ldap/v3 v3.4.11
2224
github.com/go-playground/validator v9.31.0+incompatible
2325
github.com/golang-jwt/jwt/v5 v5.3.0

backend/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,18 @@ github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8
197197
github.com/getkin/kin-openapi v0.118.0 h1:z43njxPmJ7TaPpMSCQb7PN0dEYno4tyBPQcrFdHoLuM=
198198
github.com/getkin/kin-openapi v0.118.0/go.mod h1:l5e9PaFUo9fyLJCPGQeXI2ML8c3P8BHOEV2VaAVf/pc=
199199
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
200+
github.com/getsentry/sentry-go v0.35.1 h1:iopow6UVLE2aXu46xKVIs8Z9D/YZkJrHkgozrxa+tOQ=
201+
github.com/getsentry/sentry-go v0.35.1/go.mod h1:C55omcY9ChRQIUcVcGcs+Zdy4ZpQGvNJ7JYHIoSWOtE=
202+
github.com/getsentry/sentry-go/echo v0.35.1 h1:MIhSUyo7cpCdcw0/lIeAw5fukrDt3x9G7qbiyjbVllI=
203+
github.com/getsentry/sentry-go/echo v0.35.1/go.mod h1:IjdEzgvwlP2/7A32tWk75UmSUsBqvKFdpkN6WhB1e6M=
200204
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
201205
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
202206
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667 h1:BP4M0CvQ4S3TGls2FvczZtj5Re/2ZzkV9VwqPHH/3Bo=
203207
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
204208
github.com/go-check/check v0.0.0-20180628173108-788fd7840127 h1:0gkP6mzaMqkmpcJYCFOLkIBwI7xFExG03bbkOkCvUPI=
205209
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
210+
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
211+
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
206212
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
207213
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
208214
github.com/go-ldap/ldap/v3 v3.4.11 h1:4k0Yxweg+a3OyBLjdYn5OKglv18JNvfDykSoI8bW0gU=
@@ -475,6 +481,8 @@ github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8
475481
github.com/perimeterx/marshmallow v1.1.4/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
476482
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
477483
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
484+
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
485+
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
478486
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
479487
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
480488
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

backend/handler/share/captcha.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55

66
gocap "github.com/ackcoder/go-cap"
7+
"github.com/getsentry/sentry-go"
78
"github.com/labstack/echo/v4"
89

910
"github.com/chaitin/panda-wiki/consts"
@@ -77,6 +78,7 @@ func (h *ShareCaptchaHandler) RedeemCaptcha(c echo.Context) error {
7778
}
7879
data, err := h.Captcha.RedeemChallenge(c.Request().Context(), req.Token, req.Solutions)
7980
if err != nil {
81+
sentry.CaptureException(err)
8082
return c.JSON(http.StatusInternalServerError, gocap.VerificationResult{
8183
Success: false,
8284
Message: err.Error(),

backend/middleware/share_auth.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package middleware
33
import (
44
"net/http"
55

6+
"github.com/getsentry/sentry-go"
67
"github.com/labstack/echo-contrib/session"
78
"github.com/labstack/echo/v4"
89

@@ -37,6 +38,7 @@ func (h *ShareAuthMiddleware) CheckForbidden(next echo.HandlerFunc) echo.Handler
3738
kb, err := h.kbUsecase.GetKnowledgeBase(c.Request().Context(), kbID)
3839
if err != nil {
3940
h.logger.Error("get knowledge base failed", log.String("kb_id", kbID), log.Error(err))
41+
sentry.CaptureException(err)
4042
return c.JSON(http.StatusInternalServerError, domain.PWResponse{
4143
Success: false,
4244
Message: "failed to get knowledge base detail",

backend/server/http/http.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import (
55
"log/slog"
66
"net/http"
77
"os"
8+
"time"
89

10+
"github.com/getsentry/sentry-go"
11+
sentryecho "github.com/getsentry/sentry-go/echo"
912
"github.com/go-playground/validator"
1013
"github.com/labstack/echo/v4"
1114
"github.com/labstack/echo/v4/middleware"
@@ -40,6 +43,20 @@ func NewEcho(
4043
sessionMiddleware *PWMiddleware.SessionMiddleware,
4144
) *echo.Echo {
4245

46+
// Initialize Sentry if enabled
47+
if config.Sentry.Enabled && config.Sentry.DSN != "" {
48+
err := sentry.Init(sentry.ClientOptions{
49+
Dsn: config.Sentry.DSN,
50+
})
51+
if err != nil {
52+
logger.Error("Failed to initialize Sentry", log.Error(err))
53+
} else {
54+
logger.Info("Sentry initialized successfully")
55+
// Flush buffered events on the default client before the program terminates.
56+
defer sentry.Flush(2 * time.Second)
57+
}
58+
}
59+
4360
e := echo.New()
4461
e.HideBanner = true
4562
e.HidePort = true
@@ -51,6 +68,15 @@ func NewEcho(
5168
// register validator
5269
e.Validator = &echoValidator{validator: validator.New()}
5370

71+
// Add Sentry middleware if enabled
72+
if config.Sentry.Enabled && config.Sentry.DSN != "" {
73+
e.Use(sentryecho.New(sentryecho.Options{
74+
Repanic: true,
75+
Timeout: 5 * time.Second,
76+
}))
77+
sentry.CaptureMessage("It works!")
78+
}
79+
5480
if config.GetBool("apm.enabled") {
5581
e.Use(middlewareOtel.Middleware(config.GetString("apm.service_name")))
5682
}

0 commit comments

Comments
 (0)