-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
38 lines (32 loc) · 954 Bytes
/
middleware.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
package zapray
import (
"net/http"
"github.com/aws/aws-xray-sdk-go/xray"
)
// NewMiddlware creates X-Ray middleware that creates a subsegment
// for each HTTP request.
func NewMiddleware(appName string, next http.Handler) http.Handler {
return Middleware{
Next: next,
name: appName,
}
}
// Middlware applies X-Ray segements to the wrapped handler.
type Middleware struct {
Next http.Handler
name string
}
func (h Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, s := xray.BeginSubsegment(r.Context(), "xrayMiddleware")
defer s.Close(nil)
xray.HandlerWithContext(ctx, xray.NewFixedSegmentNamer(h.name+"Handler"), setParent(s, h.Next)).ServeHTTP(w, r.WithContext(ctx))
}
func setParent(s *xray.Segment, h http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
seg := xray.GetSegment(r.Context())
if seg != nil {
seg.ParentID = s.ID
}
h.ServeHTTP(rw, r)
})
}