Skip to content

Commit

Permalink
feat(gin): Add transactions to Gin integration (#644)
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Ovchinnikov <anton@tonyo.info>
  • Loading branch information
zetaab and tonyo authored Jun 7, 2023
1 parent 1b0b536 commit 2aacdfb
Show file tree
Hide file tree
Showing 3 changed files with 434 additions and 6 deletions.
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())
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

0 comments on commit 2aacdfb

Please # to comment.