basic tracing middleware

This commit is contained in:
Ruidy 2024-03-11 22:11:08 +01:00
parent 08739d10ba
commit bc2c7a7583
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
3 changed files with 36 additions and 22 deletions

View file

@ -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 {

View file

@ -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"))

View file

@ -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)
}
}