Skip to content

Commit 80fad8c

Browse files
authored
trace event only when category is enabled (#1814)
close #1813 Signed-off-by: ekexium <eke@fastmail.com>
1 parent f9460d4 commit 80fad8c

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

txnkv/transaction/commit.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ func (action actionCommit) handleSingleBatch(c *twoPhaseCommitter, bo *retry.Bac
103103
c.resourceGroupTagger(req)
104104
}
105105

106-
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.start",
107-
zap.Uint64("startTS", c.startTS),
108-
zap.Uint64("commitTS", c.commitTS),
109-
zap.Uint64("regionID", batch.region.GetID()),
110-
zap.Bool("isPrimary", batch.isPrimary),
111-
zap.Int("keyCount", len(keys)))
106+
if trace.IsCategoryEnabled(trace.CategoryTxn2PC) {
107+
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.start",
108+
zap.Uint64("startTS", c.startTS),
109+
zap.Uint64("commitTS", c.commitTS),
110+
zap.Uint64("regionID", batch.region.GetID()),
111+
zap.Bool("isPrimary", batch.isPrimary),
112+
zap.Int("keyCount", len(keys)))
113+
}
112114

113115
tBegin := time.Now()
114116
attempts := 0
@@ -134,9 +136,11 @@ func (action actionCommit) handleSingleBatch(c *twoPhaseCommitter, bo *retry.Bac
134136

135137
// Unexpected error occurs, return it.
136138
if err != nil {
137-
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.result",
138-
zap.Uint64("regionID", batch.region.GetID()),
139-
zap.Bool("success", false))
139+
if trace.IsCategoryEnabled(trace.CategoryTxn2PC) {
140+
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.result",
141+
zap.Uint64("regionID", batch.region.GetID()),
142+
zap.Bool("success", false))
143+
}
140144
return err
141145
}
142146

@@ -256,9 +260,11 @@ func (action actionCommit) handleSingleBatch(c *twoPhaseCommitter, bo *retry.Bac
256260
// Group that contains primary key is always the first.
257261
// We mark transaction's status committed when we receive the first success response.
258262
c.mu.committed = true
259-
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.result",
260-
zap.Uint64("regionID", batch.region.GetID()),
261-
zap.Bool("success", true))
263+
if trace.IsCategoryEnabled(trace.CategoryTxn2PC) {
264+
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.result",
265+
zap.Uint64("regionID", batch.region.GetID()),
266+
zap.Bool("success", true))
267+
}
262268
return nil
263269
}
264270

txnkv/transaction/prewrite.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,21 +234,25 @@ func (action actionPrewrite) handleSingleBatch(
234234

235235
handler := action.newSingleBatchPrewriteReqHandler(c, batch, bo)
236236

237-
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "prewrite.batch.start",
238-
zap.Uint64("startTS", c.startTS),
239-
zap.Uint64("regionID", batch.region.GetID()),
240-
zap.Bool("isPrimary", batch.isPrimary),
241-
zap.Int("keyCount", batch.mutations.Len()))
237+
if trace.IsCategoryEnabled(trace.CategoryTxn2PC) {
238+
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "prewrite.batch.start",
239+
zap.Uint64("startTS", c.startTS),
240+
zap.Uint64("regionID", batch.region.GetID()),
241+
zap.Bool("isPrimary", batch.isPrimary),
242+
zap.Int("keyCount", batch.mutations.Len()))
243+
}
242244

243245
var retryable bool
244246
for {
245247
// It will return false if the request is success or meet an unretryable error.
246248
// otherwise if the error is retryable, it will return true.
247249
retryable, err = handler.sendReqAndCheck()
248250
if !retryable {
249-
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "prewrite.batch.result",
250-
zap.Uint64("regionID", batch.region.GetID()),
251-
zap.Bool("success", err == nil))
251+
if trace.IsCategoryEnabled(trace.CategoryTxn2PC) {
252+
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "prewrite.batch.result",
253+
zap.Uint64("regionID", batch.region.GetID()),
254+
zap.Bool("success", err == nil))
255+
}
252256
handler.drop(err)
253257
return err
254258
}

txnkv/txnlock/lock_resolver.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,13 @@ func (lr *LockResolver) resolveLocks(bo *retry.Backoffer, opts ResolveLocksOptio
498498
TTL: msBeforeTxnExpired.value(),
499499
}, nil
500500
}
501-
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxnLockResolve, "resolve_locks.start",
502-
zap.Uint64("callerStartTS", callerStartTS),
503-
zap.Int("lockCount", len(locks)),
504-
zap.Bool("forRead", forRead),
505-
zap.Bool("lite", lite))
501+
if trace.IsCategoryEnabled(trace.CategoryTxnLockResolve) {
502+
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxnLockResolve, "resolve_locks.start",
503+
zap.Uint64("callerStartTS", callerStartTS),
504+
zap.Int("lockCount", len(locks)),
505+
zap.Bool("forRead", forRead),
506+
zap.Bool("lite", lite))
507+
}
506508

507509
// trace the results
508510
defer func() {

0 commit comments

Comments
 (0)