forked from st3v/go-plugins
/
awsxray.go
110 lines (89 loc) · 2.46 KB
/
awsxray.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Package awsxray is a wrapper for AWS X-Ray distributed tracing
package awsxray
import (
"context"
"github.com/micro/go-awsxray"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
)
type xrayWrapper struct {
opts Options
x *awsxray.AWSXRay
client.Client
}
func (x *xrayWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
var err error
s := getSegment(x.opts.Name, ctx)
defer func() {
setCallStatus(s, req.Service(), req.Method(), err)
go record(x.x, s)
}()
ctx = newContext(ctx, s)
err = x.Client.Call(ctx, req, rsp, opts...)
return err
}
// NewCallWrapper accepts Options and returns a Trace Call Wrapper for individual node calls made by the client
func NewCallWrapper(opts ...Option) client.CallWrapper {
options := Options{
Name: "go.micro.client.CallFunc",
Daemon: "localhost:2000",
}
for _, o := range opts {
o(&options)
}
x := newXRay(options)
return func(cf client.CallFunc) client.CallFunc {
return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
var err error
s := getSegment(options.Name, ctx)
defer func() {
setCallStatus(s, addr, req.Method(), err)
go record(x, s)
}()
ctx = newContext(ctx, s)
err = cf(ctx, addr, req, rsp, opts)
return err
}
}
}
// NewClientWrapper accepts Options and returns a Trace Client Wrapper which tracks high level service calls
func NewClientWrapper(opts ...Option) client.Wrapper {
options := Options{
Name: "go.micro.client.Call",
Daemon: "localhost:2000",
}
for _, o := range opts {
o(&options)
}
return func(c client.Client) client.Client {
return &xrayWrapper{options, newXRay(options), c}
}
}
// NewHandlerWrapper accepts Options and returns a Trace Handler Wrapper
func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
options := Options{
Daemon: "localhost:2000",
}
for _, o := range opts {
o(&options)
}
x := newXRay(options)
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
name := options.Name
if len(name) == 0 {
// default name
name = req.Service() + "." + req.Method()
}
var err error
s := getSegment(name, ctx)
defer func() {
setCallStatus(s, req.Service(), req.Method(), err)
go record(x, s)
}()
ctx = newContext(ctx, s)
err = h(ctx, req, rsp)
return err
}
}
}