From 668305034a5aab42f270b744757a0ecca4c308b1 Mon Sep 17 00:00:00 2001 From: Tony Afanasev Date: Thu, 27 Jul 2023 14:39:57 +0700 Subject: [PATCH 1/8] Remove http method from transaction name --- gin/sentrygin.go | 3 +-- gin/sentrygin_test.go | 18 +++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/gin/sentrygin.go b/gin/sentrygin.go index 98803cf7c..984db2c94 100644 --- a/gin/sentrygin.go +++ b/gin/sentrygin.go @@ -2,7 +2,6 @@ package sentrygin import ( "context" - "fmt" "net" "net/http" "os" @@ -61,7 +60,7 @@ func (h *handler) handle(c *gin.Context) { } transaction := sentry.StartTransaction(ctx, - fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path), + c.Request.URL.Path, options..., ) defer func() { diff --git a/gin/sentrygin_test.go b/gin/sentrygin_test.go index 97d733214..ca34ed10d 100644 --- a/gin/sentrygin_test.go +++ b/gin/sentrygin_test.go @@ -31,7 +31,7 @@ func TestIntegration(t *testing.T) { WantTransaction *sentry.Event }{ { - Path: "/panic", + Path: "/panic/:id", Method: "GET", WantStatus: 200, Handler: func(c *gin.Context) { @@ -40,9 +40,9 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "GET /panic", + Transaction: "/panic/:id", Request: &sentry.Request{ - URL: "/panic", + URL: "/panic/:id", Method: "GET", Headers: map[string]string{ "Accept-Encoding": "gzip", @@ -55,7 +55,7 @@ func TestIntegration(t *testing.T) { Level: sentry.LevelFatal, Message: "test", Request: &sentry.Request{ - URL: "/panic", + URL: "/panic/:id", Method: "GET", Headers: map[string]string{ "Accept-Encoding": "gzip", @@ -81,7 +81,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "POST /post", + Transaction: "/post", Request: &sentry.Request{ URL: "/post", Method: "POST", @@ -121,7 +121,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "GET /get", + Transaction: "/get", Request: &sentry.Request{ URL: "/get", Method: "GET", @@ -161,7 +161,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "POST /post/large", + Transaction: "/post/large", Request: &sentry.Request{ URL: "/post/large", Method: "POST", @@ -201,7 +201,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "POST /post/body-ignored", + Transaction: "/post/body-ignored", Request: &sentry.Request{ URL: "/post/body-ignored", Method: "POST", @@ -241,7 +241,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "GET /badreq", + Transaction: "/badreq", Request: &sentry.Request{ URL: "/badreq", Method: "GET", From 2a286b08a57f5ca35b03de9e3ba25b4a46bc6d69 Mon Sep 17 00:00:00 2001 From: Tony Afanasev Date: Thu, 27 Jul 2023 14:40:50 +0700 Subject: [PATCH 2/8] Use gin full path as transaction name --- gin/sentrygin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gin/sentrygin.go b/gin/sentrygin.go index 984db2c94..45ae805b9 100644 --- a/gin/sentrygin.go +++ b/gin/sentrygin.go @@ -60,7 +60,7 @@ func (h *handler) handle(c *gin.Context) { } transaction := sentry.StartTransaction(ctx, - c.Request.URL.Path, + c.FullPath(), options..., ) defer func() { From 359dd8e15c00abab342019740906c4195a5d54d8 Mon Sep 17 00:00:00 2001 From: Tony Afanasev Date: Thu, 27 Jul 2023 16:47:48 +0700 Subject: [PATCH 3/8] Change transaction source to route --- gin/sentrygin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gin/sentrygin.go b/gin/sentrygin.go index 45ae805b9..4d9976765 100644 --- a/gin/sentrygin.go +++ b/gin/sentrygin.go @@ -56,7 +56,7 @@ func (h *handler) handle(c *gin.Context) { options := []sentry.SpanOption{ sentry.WithOpName("http.server"), sentry.ContinueFromRequest(c.Request), - sentry.WithTransactionSource(sentry.SourceURL), + sentry.WithTransactionSource(sentry.SourceRoute), } transaction := sentry.StartTransaction(ctx, From de4e6e934c54bc93b22efcce11e2f2d9de966bef Mon Sep 17 00:00:00 2001 From: Tony Afanasev Date: Thu, 27 Jul 2023 22:04:59 +0700 Subject: [PATCH 4/8] Include HTTP Method into transaction name for gin --- gin/sentrygin.go | 3 ++- gin/sentrygin_test.go | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/gin/sentrygin.go b/gin/sentrygin.go index 4d9976765..6332c4fc7 100644 --- a/gin/sentrygin.go +++ b/gin/sentrygin.go @@ -2,6 +2,7 @@ package sentrygin import ( "context" + "fmt" "net" "net/http" "os" @@ -60,7 +61,7 @@ func (h *handler) handle(c *gin.Context) { } transaction := sentry.StartTransaction(ctx, - c.FullPath(), + fmt.Sprintf("%s %s", c.Request.Method, c.FullPath()), options..., ) defer func() { diff --git a/gin/sentrygin_test.go b/gin/sentrygin_test.go index ca34ed10d..5bf916f23 100644 --- a/gin/sentrygin_test.go +++ b/gin/sentrygin_test.go @@ -40,7 +40,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "/panic/:id", + Transaction: "GET /panic/:id", Request: &sentry.Request{ URL: "/panic/:id", Method: "GET", @@ -49,7 +49,7 @@ func TestIntegration(t *testing.T) { "User-Agent": "Go-http-client/1.1", }, }, - TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + TransactionInfo: &sentry.TransactionInfo{Source: "route"}, }, WantEvent: &sentry.Event{ Level: sentry.LevelFatal, @@ -81,7 +81,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "/post", + Transaction: "POST /post", Request: &sentry.Request{ URL: "/post", Method: "POST", @@ -92,7 +92,7 @@ func TestIntegration(t *testing.T) { "User-Agent": "Go-http-client/1.1", }, }, - TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + TransactionInfo: &sentry.TransactionInfo{Source: "route"}, }, WantEvent: &sentry.Event{ Level: sentry.LevelInfo, @@ -121,7 +121,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "/get", + Transaction: "GET /get", Request: &sentry.Request{ URL: "/get", Method: "GET", @@ -130,7 +130,7 @@ func TestIntegration(t *testing.T) { "User-Agent": "Go-http-client/1.1", }, }, - TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + TransactionInfo: &sentry.TransactionInfo{Source: "route"}, }, WantEvent: &sentry.Event{ Level: sentry.LevelInfo, @@ -161,7 +161,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "/post/large", + Transaction: "POST /post/large", Request: &sentry.Request{ URL: "/post/large", Method: "POST", @@ -171,7 +171,7 @@ func TestIntegration(t *testing.T) { "User-Agent": "Go-http-client/1.1", }, }, - TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + TransactionInfo: &sentry.TransactionInfo{Source: "route"}, }, WantEvent: &sentry.Event{ Level: sentry.LevelInfo, @@ -201,7 +201,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "/post/body-ignored", + Transaction: "POST /post/body-ignored", Request: &sentry.Request{ URL: "/post/body-ignored", Method: "POST", @@ -213,7 +213,7 @@ func TestIntegration(t *testing.T) { "User-Agent": "Go-http-client/1.1", }, }, - TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + TransactionInfo: &sentry.TransactionInfo{Source: "route"}, }, WantEvent: &sentry.Event{ Level: sentry.LevelInfo, @@ -241,7 +241,7 @@ func TestIntegration(t *testing.T) { WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", - Transaction: "/badreq", + Transaction: "GET /badreq", Request: &sentry.Request{ URL: "/badreq", Method: "GET", @@ -250,7 +250,7 @@ func TestIntegration(t *testing.T) { "User-Agent": "Go-http-client/1.1", }, }, - TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + TransactionInfo: &sentry.TransactionInfo{Source: "route"}, }, WantEvent: nil, }, From 5a94ba1449003af8453f9f9f224cc345d1e391ee Mon Sep 17 00:00:00 2001 From: Tony Afanasev Date: Thu, 27 Jul 2023 22:06:14 +0700 Subject: [PATCH 5/8] Handle error in sentry gin test --- gin/sentrygin_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gin/sentrygin_test.go b/gin/sentrygin_test.go index 5bf916f23..af961bb34 100644 --- a/gin/sentrygin_test.go +++ b/gin/sentrygin_test.go @@ -314,7 +314,10 @@ func TestIntegration(t *testing.T) { if res.StatusCode != tt.WantStatus { t.Errorf("Status code = %d expected: %d", res.StatusCode, tt.WantStatus) } - res.Body.Close() + err = res.Body.Close() + if err != nil { + t.Fatal(err) + } } if ok := sentry.Flush(time.Second); !ok { From 3b50c09995a5a8f149545f5e9ea4f52d9e77d061 Mon Sep 17 00:00:00 2001 From: Tony Afanasev Date: Mon, 14 Aug 2023 19:42:14 +0700 Subject: [PATCH 6/8] Set real path for gin transaction on 404 --- gin/sentrygin.go | 4 ++++ gin/sentrygin_test.go | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/gin/sentrygin.go b/gin/sentrygin.go index 6332c4fc7..2565ce0cb 100644 --- a/gin/sentrygin.go +++ b/gin/sentrygin.go @@ -65,6 +65,10 @@ func (h *handler) handle(c *gin.Context) { options..., ) defer func() { + if c.Writer.Status() == 404 { + transaction.Name = fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path) + transaction.Source = sentry.SourceURL + } transaction.Status = sentry.HTTPtoSpanStatus(c.Writer.Status()) transaction.Finish() }() diff --git a/gin/sentrygin_test.go b/gin/sentrygin_test.go index af961bb34..2036c6ba2 100644 --- a/gin/sentrygin_test.go +++ b/gin/sentrygin_test.go @@ -64,6 +64,28 @@ func TestIntegration(t *testing.T) { }, }, }, + { + Path: "/404/1", + Method: "GET", + WantStatus: 404, + Handler: func(c *gin.Context) { + c.AbortWithStatus(404) + }, + WantTransaction: &sentry.Event{ + Level: sentry.LevelInfo, + Type: "transaction", + Transaction: "GET /404/1", + Request: &sentry.Request{ + URL: "/404/1", + Method: "GET", + Headers: map[string]string{ + "Accept-Encoding": "gzip", + "User-Agent": "Go-http-client/1.1", + }, + }, + TransactionInfo: &sentry.TransactionInfo{Source: "url"}, + }, + }, { Path: "/post", Method: "POST", From c1edc111039f7b7866b7b795aa5e4c4fe212d569 Mon Sep 17 00:00:00 2001 From: Tony Afanasev Date: Mon, 14 Aug 2023 20:00:35 +0700 Subject: [PATCH 7/8] Improve gin transaction tests with different paths for router and request --- gin/sentrygin_test.go | 79 +++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/gin/sentrygin_test.go b/gin/sentrygin_test.go index 2036c6ba2..636c05bdc 100644 --- a/gin/sentrygin_test.go +++ b/gin/sentrygin_test.go @@ -21,19 +21,21 @@ func TestIntegration(t *testing.T) { largePayload := strings.Repeat("Large", 3*1024) // 15 KB tests := []struct { - Path string - Method string - WantStatus int - Body string - Handler gin.HandlerFunc + RequestPath string + RoutePath string + Method string + WantStatus int + Body string + Handler gin.HandlerFunc WantEvent *sentry.Event WantTransaction *sentry.Event }{ { - Path: "/panic/:id", - Method: "GET", - WantStatus: 200, + RequestPath: "/panic/1", + RoutePath: "/panic/:id", + Method: "GET", + WantStatus: 200, Handler: func(c *gin.Context) { panic("test") }, @@ -42,7 +44,7 @@ func TestIntegration(t *testing.T) { Type: "transaction", Transaction: "GET /panic/:id", Request: &sentry.Request{ - URL: "/panic/:id", + URL: "/panic/1", Method: "GET", Headers: map[string]string{ "Accept-Encoding": "gzip", @@ -55,7 +57,7 @@ func TestIntegration(t *testing.T) { Level: sentry.LevelFatal, Message: "test", Request: &sentry.Request{ - URL: "/panic/:id", + URL: "/panic/1", Method: "GET", Headers: map[string]string{ "Accept-Encoding": "gzip", @@ -65,12 +67,11 @@ func TestIntegration(t *testing.T) { }, }, { - Path: "/404/1", - Method: "GET", - WantStatus: 404, - Handler: func(c *gin.Context) { - c.AbortWithStatus(404) - }, + RequestPath: "/404/1", + RoutePath: "/some-other-route", + Method: "GET", + WantStatus: 404, + Handler: nil, WantTransaction: &sentry.Event{ Level: sentry.LevelInfo, Type: "transaction", @@ -85,12 +86,14 @@ func TestIntegration(t *testing.T) { }, TransactionInfo: &sentry.TransactionInfo{Source: "url"}, }, + WantEvent: nil, }, { - Path: "/post", - Method: "POST", - WantStatus: 200, - Body: "payload", + RequestPath: "/post", + RoutePath: "/post", + Method: "POST", + WantStatus: 200, + Body: "payload", Handler: func(c *gin.Context) { hub := sentry.GetHubFromContext(c.Request.Context()) body, err := io.ReadAll(c.Request.Body) @@ -132,9 +135,10 @@ func TestIntegration(t *testing.T) { }, }, { - Path: "/get", - Method: "GET", - WantStatus: 200, + RequestPath: "/get", + RoutePath: "/get", + Method: "GET", + WantStatus: 200, Handler: func(c *gin.Context) { hub := sentry.GetHubFromContext(c.Request.Context()) hub.CaptureMessage("get") @@ -168,10 +172,11 @@ func TestIntegration(t *testing.T) { }, }, { - Path: "/post/large", - Method: "POST", - WantStatus: 200, - Body: largePayload, + RequestPath: "/post/large", + RoutePath: "/post/large", + Method: "POST", + WantStatus: 200, + Body: largePayload, Handler: func(c *gin.Context) { hub := sentry.GetHubFromContext(c.Request.Context()) body, err := io.ReadAll(c.Request.Body) @@ -212,10 +217,11 @@ func TestIntegration(t *testing.T) { }, }, { - Path: "/post/body-ignored", - Method: "POST", - WantStatus: 200, - Body: "client sends, server ignores, SDK doesn't read", + RequestPath: "/post/body-ignored", + RoutePath: "/post/body-ignored", + Method: "POST", + WantStatus: 200, + Body: "client sends, server ignores, SDK doesn't read", Handler: func(c *gin.Context) { hub := sentry.GetHubFromContext(c.Request.Context()) hub.CaptureMessage("body ignored") @@ -254,9 +260,10 @@ func TestIntegration(t *testing.T) { }, }, { - Path: "/badreq", - Method: "GET", - WantStatus: 400, + RequestPath: "/badreq", + RoutePath: "/badreq", + Method: "GET", + WantStatus: 400, Handler: func(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"status": "bad_request"}) }, @@ -300,7 +307,7 @@ func TestIntegration(t *testing.T) { router.Use(sentrygin.New(sentrygin.Options{})) for _, tt := range tests { - router.Handle(tt.Method, tt.Path, tt.Handler) + router.Handle(tt.Method, tt.RoutePath, tt.Handler) } srv := httptest.NewServer(router) @@ -325,7 +332,7 @@ func TestIntegration(t *testing.T) { wanttrans = append(wanttrans, tt.WantTransaction) wantCodes = append(wantCodes, sentry.HTTPtoSpanStatus(tt.WantStatus)) - req, err := http.NewRequest(tt.Method, srv.URL+tt.Path, strings.NewReader(tt.Body)) + req, err := http.NewRequest(tt.Method, srv.URL+tt.RequestPath, strings.NewReader(tt.Body)) if err != nil { t.Fatal(err) } From 67593bda92f31a5d55153342316f4964c8b6dd28 Mon Sep 17 00:00:00 2001 From: Tony Afanasev Date: Fri, 1 Sep 2023 22:06:58 +0700 Subject: [PATCH 8/8] Choose transaction source and name depending on found route --- gin/sentrygin.go | 20 ++++++++++++++------ gin/sentrygin_test.go | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/gin/sentrygin.go b/gin/sentrygin.go index 2565ce0cb..50f9abc58 100644 --- a/gin/sentrygin.go +++ b/gin/sentrygin.go @@ -54,21 +54,29 @@ func (h *handler) handle(c *gin.Context) { hub = sentry.CurrentHub().Clone() ctx = sentry.SetHubOnContext(ctx, hub) } + + var transactionName string + var transactionSource sentry.TransactionSource + + if c.FullPath() != "" { + transactionName = c.FullPath() + transactionSource = sentry.SourceRoute + } else { + transactionName = c.Request.URL.Path + transactionSource = sentry.SourceURL + } + options := []sentry.SpanOption{ sentry.WithOpName("http.server"), sentry.ContinueFromRequest(c.Request), - sentry.WithTransactionSource(sentry.SourceRoute), + sentry.WithTransactionSource(transactionSource), } transaction := sentry.StartTransaction(ctx, - fmt.Sprintf("%s %s", c.Request.Method, c.FullPath()), + fmt.Sprintf("%s %s", c.Request.Method, transactionName), options..., ) defer func() { - if c.Writer.Status() == 404 { - transaction.Name = fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path) - transaction.Source = sentry.SourceURL - } transaction.Status = sentry.HTTPtoSpanStatus(c.Writer.Status()) transaction.Finish() }() diff --git a/gin/sentrygin_test.go b/gin/sentrygin_test.go index 4e11cfcae..b75e0f1f0 100644 --- a/gin/sentrygin_test.go +++ b/gin/sentrygin_test.go @@ -69,7 +69,7 @@ func TestIntegration(t *testing.T) { }, { RequestPath: "/404/1", - RoutePath: "/some-other-route", + RoutePath: "", Method: "GET", WantStatus: 404, Handler: nil,