Skip to content

Commit

Permalink
add transactions to gin
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaab committed May 29, 2023
1 parent 02e712a commit 1d4b344
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 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,30 @@ 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 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

0 comments on commit 1d4b344

Please sign in to comment.