-
Notifications
You must be signed in to change notification settings - Fork 10
/
wrap_doer.go
49 lines (38 loc) · 1.13 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"
"github.com/shogo82148/goa-v1/client"
"github.com/shogo82148/goa-v1/middleware"
)
// wrapDoer is a client.Doer middleware that will create xray subsegments for traced requests.
type wrapDoer struct {
wrapped client.Doer
}
var _ client.Doer = (*wrapDoer)(nil)
// WrapDoer wraps a goa client Doer, and creates xray subsegments for traced requests.
func WrapDoer(wrapped client.Doer) client.Doer {
return &wrapDoer{wrapped}
}
// Do calls through to the wrapped Doer, creating subsegments as appropriate.
func (r *wrapDoer) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
s := ContextSegment(ctx)
if s == nil {
// this request isn't traced
return r.wrapped.Do(ctx, req)
}
sub := s.NewSubsegment(req.URL.Host)
sub.RecordRequest(req, "remote")
sub.SubmitInProgress()
defer sub.Close()
// update the context with the latest segment
ctx = middleware.WithTrace(ctx, sub.TraceID, sub.ID, sub.ParentID)
ctx = WithSegment(ctx, sub)
resp, err := r.wrapped.Do(ctx, req)
if err != nil {
sub.RecordError(err)
} else {
sub.RecordResponse(resp)
}
return resp, err
}