-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho.go
57 lines (48 loc) · 1.5 KB
/
echo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package apm
import (
"fmt"
"github.com/getsentry/sentry-go"
"github.com/labstack/echo/v4"
"github.com/ziflex/lecho/v3"
)
// EchoLogger is a wrapper around the zerolog logger (without sentry) that implements the echo.Logger interface.
func EchoLogger() echo.Logger {
return lecho.From(*NewLoggerPlain())
}
// WithSentry is a middleware that creates a new transaction for each request.
func WithSentry() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := NewContext(c.Request().Context())
c.SetRequest(c.Request().WithContext(ctx))
if hub := sentry.GetHubFromContext(ctx); hub != nil {
hub.Scope().SetRequest(c.Request())
}
defer func() { Recover(recover(), true, ctx) }()
if c.Request().URL.Path == "/_health" {
return next(c)
}
options := []sentry.SpanOption{
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(c.Request()),
sentry.WithTransactionSource(sentry.SourceURL),
}
path := c.Path()
if path == "" || path == "/" {
path = c.Request().URL.Path
}
transaction := sentry.StartTransaction(c.Request().Context(),
fmt.Sprintf("%s %s", c.Request().Method, path),
options...,
)
defer transaction.Finish()
c.SetRequest(c.Request().WithContext(transaction.Context()))
if err := next(c); err != nil {
transaction.Status = sentry.HTTPtoSpanStatus(c.Response().Status)
return err
}
transaction.Status = sentry.SpanStatusOK
return nil
}
}
}