forked from yandex-cloud/ydb-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.go
249 lines (236 loc) · 5.17 KB
/
trace.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package ydb
import (
"context"
"strings"
"github.com/yandex-cloud/ydb-go-sdk/internal/api/protos/Ydb_Operations"
)
type DriverTrace struct {
DialStart func(DialStartInfo)
DialDone func(DialDoneInfo)
DiscoveryStart func(DiscoveryStartInfo)
DiscoveryDone func(DiscoveryDoneInfo)
OperationStart func(OperationStartInfo)
OperationWait func(OperationWaitInfo)
OperationDone func(OperationDoneInfo)
}
func (d DriverTrace) dialStart(ctx context.Context, addr string) {
x := DialStartInfo{
Context: ctx,
Address: addr,
}
if f := d.DialStart; f != nil {
f(x)
}
if f := ContextDriverTrace(ctx).DialStart; f != nil {
f(x)
}
}
func (d DriverTrace) dialDone(ctx context.Context, addr string, err error) {
x := DialDoneInfo{
Context: ctx,
Address: addr,
Error: err,
}
if f := d.DialDone; f != nil {
f(x)
}
if f := ContextDriverTrace(ctx).DialDone; f != nil {
f(x)
}
}
func (d DriverTrace) discoveryStart(ctx context.Context) {
x := DiscoveryStartInfo{
Context: ctx,
}
if f := d.DiscoveryStart; f != nil {
f(x)
}
if f := ContextDriverTrace(ctx).DiscoveryStart; f != nil {
f(x)
}
}
func (d DriverTrace) discoveryDone(ctx context.Context, es []Endpoint, err error) {
x := DiscoveryDoneInfo{
Context: ctx,
Endpoints: es,
Error: err,
}
if f := d.DiscoveryDone; f != nil {
f(x)
}
if f := ContextDriverTrace(ctx).DiscoveryDone; f != nil {
f(x)
}
}
func (d DriverTrace) operationStart(ctx context.Context, conn *conn, method string) {
x := OperationStartInfo{
Context: ctx,
Address: conn.addr.String(),
Method: Method(method),
}
if f := d.OperationStart; f != nil {
f(x)
}
if f := ContextDriverTrace(ctx).OperationStart; f != nil {
f(x)
}
}
func (d DriverTrace) operationDone(ctx context.Context, conn *conn, method string, resp Ydb_Operations.GetOperationResponse, err error) {
x := OperationDoneInfo{
Context: ctx,
Address: conn.addr.String(),
Method: Method(method),
Error: err,
}
if op := resp.Operation; op != nil {
x.OpID = op.Id
x.Issues = IssueIterator(op.Issues)
}
if f := d.OperationDone; f != nil {
f(x)
}
if f := ContextDriverTrace(ctx).OperationDone; f != nil {
f(x)
}
}
// Method represents rpc method.
type Method string
// Name returns the rpc method name.
func (m Method) Name() string {
i := strings.LastIndex(string(m), "/")
if i == -1 {
return string(m)
}
return string(m[i+1:])
}
type (
DialStartInfo struct {
Context context.Context
Address string
}
DialDoneInfo struct {
Context context.Context
Address string
Error error
}
DiscoveryStartInfo struct {
Context context.Context
}
DiscoveryDoneInfo struct {
Context context.Context
Endpoints []Endpoint
Error error
}
OperationStartInfo struct {
Context context.Context
Address string
Method Method
}
OperationWaitInfo struct {
Context context.Context
Address string
Method Method
OpID string
}
OperationDoneInfo struct {
Context context.Context
Address string
Method Method
OpID string
Issues IssueIterator
Error error
}
)
func composeDriverTrace(a, b DriverTrace) (c DriverTrace) {
switch {
case a.DialStart == nil:
c.DialStart = b.DialStart
case b.DialStart == nil:
c.DialStart = a.DialStart
default:
c.DialStart = func(info DialStartInfo) {
a.DialStart(info)
b.DialStart(info)
}
}
switch {
case a.DialDone == nil:
c.DialDone = b.DialDone
case b.DialDone == nil:
c.DialDone = a.DialDone
default:
c.DialDone = func(info DialDoneInfo) {
a.DialDone(info)
b.DialDone(info)
}
}
switch {
case a.DiscoveryStart == nil:
c.DiscoveryStart = b.DiscoveryStart
case b.DiscoveryStart == nil:
c.DiscoveryStart = a.DiscoveryStart
default:
c.DiscoveryStart = func(info DiscoveryStartInfo) {
a.DiscoveryStart(info)
b.DiscoveryStart(info)
}
}
switch {
case a.DiscoveryDone == nil:
c.DiscoveryDone = b.DiscoveryDone
case b.DiscoveryDone == nil:
c.DiscoveryDone = a.DiscoveryDone
default:
c.DiscoveryDone = func(info DiscoveryDoneInfo) {
a.DiscoveryDone(info)
b.DiscoveryDone(info)
}
}
switch {
case a.OperationStart == nil:
c.OperationStart = b.OperationStart
case b.OperationStart == nil:
c.OperationStart = a.OperationStart
default:
c.OperationStart = func(info OperationStartInfo) {
a.OperationStart(info)
b.OperationStart(info)
}
}
switch {
case a.OperationWait == nil:
c.OperationWait = b.OperationWait
case b.OperationWait == nil:
c.OperationWait = a.OperationWait
default:
c.OperationWait = func(info OperationWaitInfo) {
a.OperationWait(info)
b.OperationWait(info)
}
}
switch {
case a.OperationDone == nil:
c.OperationDone = b.OperationDone
case b.OperationDone == nil:
c.OperationDone = a.OperationDone
default:
c.OperationDone = func(info OperationDoneInfo) {
a.OperationDone(info)
b.OperationDone(info)
}
}
return
}
type driverTraceContextKey struct{}
func WithDriverTrace(ctx context.Context, trace DriverTrace) context.Context {
return context.WithValue(ctx,
driverTraceContextKey{},
composeDriverTrace(
ContextDriverTrace(ctx), trace,
),
)
}
func ContextDriverTrace(ctx context.Context) DriverTrace {
trace, _ := ctx.Value(driverTraceContextKey{}).(DriverTrace)
return trace
}