@@ -33,16 +33,10 @@ import (
33
33
)
34
34
35
35
func Put (ctx context.Context , lg * zap.Logger , lessor lease.Lessor , kv mvcc.KV , p * pb.PutRequest ) (resp * pb.PutResponse , trace * traceutil.Trace , err error ) {
36
- trace = traceutil .Get (ctx )
37
- // create put tracing if the trace in context is empty
38
- if trace .IsEmpty () {
39
- trace = traceutil .New ("put" ,
40
- lg ,
41
- traceutil.Field {Key : "key" , Value : string (p .Key )},
42
- traceutil.Field {Key : "req_size" , Value : p .Size ()},
43
- )
44
- ctx = context .WithValue (ctx , traceutil.TraceKey {}, trace )
45
- }
36
+ ctx , trace = ensureTrace (ctx , lg , "put" ,
37
+ traceutil.Field {Key : "key" , Value : string (p .Key )},
38
+ traceutil.Field {Key : "req_size" , Value : p .Size ()},
39
+ )
46
40
leaseID := lease .LeaseID (p .Lease )
47
41
if leaseID != lease .NoLease {
48
42
if l := lessor .Lookup (leaseID ); l == nil {
@@ -95,16 +89,10 @@ func put(ctx context.Context, txnWrite mvcc.TxnWrite, p *pb.PutRequest) (resp *p
95
89
}
96
90
97
91
func DeleteRange (ctx context.Context , lg * zap.Logger , kv mvcc.KV , dr * pb.DeleteRangeRequest ) (resp * pb.DeleteRangeResponse , trace * traceutil.Trace , err error ) {
98
- trace = traceutil .Get (ctx )
99
- // create delete tracing if the trace in context is empty
100
- if trace .IsEmpty () {
101
- trace = traceutil .New ("delete_range" ,
102
- lg ,
103
- traceutil.Field {Key : "key" , Value : string (dr .Key )},
104
- traceutil.Field {Key : "range_end" , Value : string (dr .RangeEnd )},
105
- )
106
- ctx = context .WithValue (ctx , traceutil.TraceKey {}, trace )
107
- }
92
+ ctx , trace = ensureTrace (ctx , lg , "delete_range" ,
93
+ traceutil.Field {Key : "key" , Value : string (dr .Key )},
94
+ traceutil.Field {Key : "range_end" , Value : string (dr .RangeEnd )},
95
+ )
108
96
txnWrite := kv .Write (trace )
109
97
defer txnWrite .End ()
110
98
resp , err = deleteRange (ctx , txnWrite , dr )
@@ -134,11 +122,7 @@ func deleteRange(ctx context.Context, txnWrite mvcc.TxnWrite, dr *pb.DeleteRange
134
122
}
135
123
136
124
func Range (ctx context.Context , lg * zap.Logger , kv mvcc.KV , r * pb.RangeRequest ) (resp * pb.RangeResponse , trace * traceutil.Trace , err error ) {
137
- trace = traceutil .Get (ctx )
138
- if trace .IsEmpty () {
139
- trace = traceutil .New ("range" , lg )
140
- ctx = context .WithValue (ctx , traceutil.TraceKey {}, trace )
141
- }
125
+ ctx , trace = ensureTrace (ctx , lg , "range" )
142
126
defer func (start time.Time ) {
143
127
success := err == nil
144
128
RangeSecObserve (success , time .Since (start ))
@@ -249,12 +233,8 @@ func executeRange(ctx context.Context, lg *zap.Logger, txnRead mvcc.TxnRead, r *
249
233
return resp , nil
250
234
}
251
235
252
- func Txn (ctx context.Context , lg * zap.Logger , rt * pb.TxnRequest , txnModeWriteWithSharedBuffer bool , kv mvcc.KV , lessor lease.Lessor ) (* pb.TxnResponse , * traceutil.Trace , error ) {
253
- trace := traceutil .Get (ctx )
254
- if trace .IsEmpty () {
255
- trace = traceutil .New ("transaction" , lg )
256
- ctx = context .WithValue (ctx , traceutil.TraceKey {}, trace )
257
- }
236
+ func Txn (ctx context.Context , lg * zap.Logger , rt * pb.TxnRequest , txnModeWriteWithSharedBuffer bool , kv mvcc.KV , lessor lease.Lessor ) (txnResp * pb.TxnResponse , trace * traceutil.Trace , err error ) {
237
+ ctx , trace = ensureTrace (ctx , lg , "transaction" )
258
238
isWrite := ! IsTxnReadonly (rt )
259
239
// When the transaction contains write operations, we use ReadTx instead of
260
240
// ConcurrentReadTx to avoid extra overhead of copying buffer.
@@ -275,7 +255,7 @@ func Txn(ctx context.Context, lg *zap.Logger, rt *pb.TxnRequest, txnModeWriteWit
275
255
if isWrite {
276
256
trace .AddField (traceutil.Field {Key : "read_only" , Value : false })
277
257
}
278
- _ , err : = checkTxn (txnRead , rt , lessor , txnPath )
258
+ _ , err = checkTxn (txnRead , rt , lessor , txnPath )
279
259
if err != nil {
280
260
txnRead .End ()
281
261
return nil , nil , err
@@ -292,7 +272,7 @@ func Txn(ctx context.Context, lg *zap.Logger, rt *pb.TxnRequest, txnModeWriteWit
292
272
} else {
293
273
txnWrite = mvcc .NewReadOnlyTxnWrite (txnRead )
294
274
}
295
- txnResp , err : = txn (ctx , lg , txnWrite , rt , isWrite , txnPath )
275
+ txnResp , err = txn (ctx , lg , txnWrite , rt , isWrite , txnPath )
296
276
txnWrite .End ()
297
277
298
278
trace .AddField (
@@ -721,3 +701,15 @@ func checkTxnReqsPermission(as auth.AuthStore, ai *auth.AuthInfo, reqs []*pb.Req
721
701
722
702
return nil
723
703
}
704
+
705
+ func ensureTrace (ctx context.Context , lg * zap.Logger , operation string , fields ... traceutil.Field ) (context.Context , * traceutil.Trace ) {
706
+ trace := traceutil .Get (ctx )
707
+ if trace .IsEmpty () {
708
+ trace = traceutil .New (operation ,
709
+ lg ,
710
+ fields ... ,
711
+ )
712
+ ctx = context .WithValue (ctx , traceutil.TraceKey {}, trace )
713
+ }
714
+ return ctx , trace
715
+ }
0 commit comments