-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathwrap_doer.go
49 lines (42 loc) Β· 1.16 KB
/
wrap_doer.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
package xray
import (
"context"
"net/http"
goahttp "goa.design/goa/http"
"goa.design/goa/middleware"
"goa.design/goa/middleware/xray"
)
// xrayDoer is a goahttp.Doer middleware that will create xray subsegments for
// traced requests.
type xrayDoer struct {
wrapped goahttp.Doer
}
// WrapDoer wraps a goa HTTP Doer and creates xray subsegments for traced
// requests.
func WrapDoer(doer goahttp.Doer) goahttp.Doer {
return &xrayDoer{doer}
}
// Do calls through to the wrapped Doer, creating subsegments as appropriate.
func (r *xrayDoer) Do(req *http.Request) (*http.Response, error) {
ctx := req.Context()
seg := ctx.Value(xray.SegKey)
if seg == nil {
return r.wrapped.Do(req)
}
s := seg.(*xray.Segment)
sub := s.NewSubsegment(req.URL.Host)
hs := &HTTPSegment{Segment: sub}
hs.RecordRequest(req, "remote")
hs.SubmitInProgress()
defer hs.Close()
// update the context with the latest segment
ctx = middleware.WithSpan(ctx, hs.TraceID, hs.ID, hs.ParentID)
req = req.WithContext(context.WithValue(ctx, xray.SegKey, hs.Segment))
resp, err := r.wrapped.Do(req)
if err != nil {
hs.RecordError(err)
} else {
hs.RecordResponse(resp)
}
return resp, err
}