From bc2c7a75837e4d86a0a8e2b22ade78535b991afd Mon Sep 17 00:00:00 2001 From: Ruidy Date: Mon, 11 Mar 2024 22:11:08 +0100 Subject: [PATCH] basic tracing middleware --- internal/server/handle_bookings.go | 22 ------------------- internal/server/server.go | 1 + internal/server/tracing.go | 35 ++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 internal/server/tracing.go diff --git a/internal/server/handle_bookings.go b/internal/server/handle_bookings.go index 7209a73..b246025 100644 --- a/internal/server/handle_bookings.go +++ b/internal/server/handle_bookings.go @@ -7,7 +7,6 @@ import ( "time" "github.com/a-h/templ" - "github.com/getsentry/sentry-go" "github.com/labstack/echo/v4" "github.com/labstack/gommon/log" u "github.com/rjNemo/underscore" @@ -21,27 +20,6 @@ import ( func handleListBookingPage(bs *booking.Service, hc *config.Host) echo.HandlerFunc { return func(c echo.Context) error { - ctx := c.Request().Context() - hub := sentry.GetHubFromContext(ctx) - if hub == nil { - // Check the concurrency guide for more details: https://docs.sentry.io/platforms/go/concurrency/ - hub = sentry.CurrentHub().Clone() - ctx = sentry.SetHubOnContext(ctx, hub) - } - - options := []sentry.SpanOption{ - // Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/ - 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() - bookings := bs.All() bvm := u.Map(bookings, func(b *booking.Line) *view.ListBookingsViewModel { diff --git a/internal/server/server.go b/internal/server/server.go index 1ba4a2d..faf82a8 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -125,6 +125,7 @@ func NewRouter(fs embed.FS) *echo.Echo { e.Use(middleware.Secure()) e.Use(middleware.Gzip()) e.Use(sentryecho.New(sentryecho.Options{})) + e.Use(SentryTracingMiddleware) // static assets e.StaticFS("/static", echo.MustSubFS(fs, "assets")) diff --git a/internal/server/tracing.go b/internal/server/tracing.go new file mode 100644 index 0000000..9ef5b86 --- /dev/null +++ b/internal/server/tracing.go @@ -0,0 +1,35 @@ +package server + +import ( + "fmt" + + "github.com/getsentry/sentry-go" + "github.com/labstack/echo/v4" +) + +func SentryTracingMiddleware(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + ctx := c.Request().Context() + hub := sentry.GetHubFromContext(ctx) + if hub == nil { + // Check the concurrency guide for more details: https://docs.sentry.io/platforms/go/concurrency/ + hub = sentry.CurrentHub().Clone() + ctx = sentry.SetHubOnContext(ctx, hub) + } + + options := []sentry.SpanOption{ + // Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/ + 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() + return next(c) + } +}