Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: Make route path as transaction name in gin performance #675

Merged
merged 11 commits into from Sep 4, 2023
4 changes: 2 additions & 2 deletions gin/sentrygin.go
Expand Up @@ -57,11 +57,11 @@ 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,
fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path),
fmt.Sprintf("%s %s", c.Request.Method, c.FullPath()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For not found routes returns an empty string.

This causes issues for 404 routes, as the transaction name is now always something like GET .
We should check for the return of c.FullPath(). If it's empty, use c.Request.URL.Path as a fallback and set sentry.WithTransactionSource(sentry.SourceURL).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's cool that you found it! Done
3b50c09

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antonsacred I think what @cleptric suggests is more like, do the check and fallback here in handle directly (not in defer), and set TransactionSource depending on the value of c.FullPath().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tonyo thanks for the take. I like your proposal. I have changed the code.

options...,
)
defer func() {
Expand Down
25 changes: 14 additions & 11 deletions gin/sentrygin_test.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to test different paths, like /panic, /panic/:id etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c1edc11
This way I tried to show where request and router paths are in used

Expand Up @@ -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) {
Expand All @@ -40,22 +40,22 @@ func TestIntegration(t *testing.T) {
WantTransaction: &sentry.Event{
Level: sentry.LevelInfo,
Type: "transaction",
Transaction: "GET /panic",
Transaction: "GET /panic/:id",
Request: &sentry.Request{
URL: "/panic",
URL: "/panic/:id",
Method: "GET",
Headers: map[string]string{
"Accept-Encoding": "gzip",
"User-Agent": "Go-http-client/1.1",
},
},
TransactionInfo: &sentry.TransactionInfo{Source: "url"},
TransactionInfo: &sentry.TransactionInfo{Source: "route"},
},
WantEvent: &sentry.Event{
Level: sentry.LevelFatal,
Message: "test",
Request: &sentry.Request{
URL: "/panic",
URL: "/panic/:id",
Method: "GET",
Headers: map[string]string{
"Accept-Encoding": "gzip",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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 {
Expand Down