-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
68 lines (58 loc) · 1.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
"log"
"net/http"
"time"
)
func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "http://[email protected]:9000/2",
// 打印过程:如 [Sentry] 2021/11/12 22:17:12 Sending info event [9c24a1511ce74ab98177b34255db0439] to tencent.danny.games project: 2
Debug: true,
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
if hint.Context != nil {
if req, ok := hint.Context.Value(sentry.RequestContextKey).(*http.Request); ok {
// You have access to the original Request here
fmt.Printf("请求内容%+v\n", req)
}
}
return event
},
AttachStacktrace: true,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
return
}
// Flush buffered events before the program terminates.
defer sentry.Flush(2 * time.Second)
app := gin.Default()
app.Use(sentrygin.New(sentrygin.Options{
Repanic: true,
}))
app.Use(func(ctx *gin.Context) {
if hub := sentrygin.GetHubFromContext(ctx); hub != nil {
hub.Scope().SetTag("tag_test", "maybeYouNeedIt")
}
ctx.Next()
})
app.GET("/", func(ctx *gin.Context) {
if hub := sentrygin.GetHubFromContext(ctx); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetExtra("unwantedQuery", "someQueryDataMaybe")
hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
})
}
ctx.Status(http.StatusOK)
})
app.GET("/foo", func(ctx *gin.Context) {
// sentrygin handler will catch it just fine. Also, because we attached "someRandomTag"
// in the middleware before, it will be sent through as well
panic("y tho")
})
app.Run(":3000")
}