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

feat(gin): Add transactions to Gin integration #644

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions gin/sentrygin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sentrygin

import (
"context"
"fmt"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -46,15 +47,33 @@ func New(options Options) gin.HandlerFunc {
}).handle
}

func (h *handler) handle(ctx *gin.Context) {
hub := sentry.GetHubFromContext(ctx.Request.Context())
func (h *handler) handle(c *gin.Context) {
ctx := c.Request.Context()
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
hub = sentry.CurrentHub().Clone()
ctx = sentry.SetHubOnContext(ctx, hub)
}
hub.Scope().SetRequest(ctx.Request)
ctx.Set(valuesKey, hub)
defer h.recoverWithSentry(hub, ctx.Request)
ctx.Next()
options := []sentry.SpanOption{
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(c.Request),
sentry.WithTransactionSource(sentry.SourceURL),
}

transaction := sentry.StartTransaction(ctx,
fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path),
options...,
)
defer func() {
transaction.Status = sentry.HTTPtoSpanStatus(c.Writer.Status())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added Status and tests for that as well

transaction.Finish()
}()

c.Request = c.Request.WithContext(transaction.Context())
hub.Scope().SetRequest(c.Request)
c.Set(valuesKey, hub)
defer h.recoverWithSentry(hub, c.Request)
c.Next()
}

func (h *handler) recoverWithSentry(hub *sentry.Hub, r *http.Request) {
Expand Down
Loading