Trace events (#1776)

ref pingcap/tidb#64008

Signed-off-by: ekexium <eke@fastmail.com>
This commit is contained in:
ekexium 2025-10-23 22:33:37 +08:00 committed by GitHub
parent d632a4c0b2
commit dcb62bb121
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 266 additions and 31 deletions

View File

@ -11,7 +11,6 @@ require (
github.com/cloudfoundry/gosigar v1.3.6 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
@ -23,14 +22,14 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect
github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect
github.com/twmb/murmur3 v1.1.3 // indirect
@ -49,6 +48,7 @@ require (
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
)
replace github.com/tikv/client-go/v2 => ../../

View File

@ -11,7 +11,6 @@ require (
github.com/cloudfoundry/gosigar v1.3.6 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
@ -23,14 +22,14 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect
github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect
github.com/twmb/murmur3 v1.1.3 // indirect
@ -49,6 +48,7 @@ require (
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
)
replace github.com/tikv/client-go/v2 => ../../

View File

@ -11,7 +11,6 @@ require (
github.com/cloudfoundry/gosigar v1.3.6 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
@ -23,14 +22,14 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect
github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect
github.com/twmb/murmur3 v1.1.3 // indirect
@ -49,6 +48,7 @@ require (
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
)
replace github.com/tikv/client-go/v2 => ../../../

View File

@ -11,7 +11,6 @@ require (
github.com/cloudfoundry/gosigar v1.3.6 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
@ -23,14 +22,14 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect
github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect
github.com/twmb/murmur3 v1.1.3 // indirect
@ -49,6 +48,7 @@ require (
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
)
replace github.com/tikv/client-go/v2 => ../../../

View File

@ -11,7 +11,6 @@ require (
github.com/cloudfoundry/gosigar v1.3.6 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
@ -23,14 +22,14 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect
github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect
github.com/twmb/murmur3 v1.1.3 // indirect
@ -49,6 +48,7 @@ require (
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
)
replace github.com/tikv/client-go/v2 => ../../../

View File

@ -11,7 +11,6 @@ require (
github.com/cloudfoundry/gosigar v1.3.6 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
@ -23,14 +22,14 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect
github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect
github.com/twmb/murmur3 v1.1.3 // indirect
@ -49,6 +48,7 @@ require (
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
)
replace github.com/tikv/client-go/v2 => ../../

View File

@ -11,7 +11,6 @@ require (
github.com/cloudfoundry/gosigar v1.3.6 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
@ -23,14 +22,14 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect
github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect
github.com/twmb/murmur3 v1.1.3 // indirect
@ -49,6 +48,7 @@ require (
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
)
replace github.com/tikv/client-go/v2 => ../../../

View File

@ -11,7 +11,6 @@ require (
github.com/cloudfoundry/gosigar v1.3.6 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
@ -23,14 +22,14 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122 // indirect
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 // indirect
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 // indirect
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect
github.com/tikv/pd/client v0.0.0-20250625073039-fb496b371ff3 // indirect
github.com/twmb/murmur3 v1.1.3 // indirect
@ -49,6 +48,7 @@ require (
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
)
replace github.com/tikv/client-go/v2 => ../../../

2
go.mod
View File

@ -15,7 +15,7 @@ require (
github.com/pingcap/errors v0.11.5-0.20241219054535-6b8c588c3122
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.20.5

4
go.sum
View File

@ -77,8 +77,8 @@ github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 h1:tdMsjOqUR7YXH
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86/go.mod h1:exzhVYca3WRtd6gclGNErRWb1qEgff3LYta0LvRmON4=
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 h1:surzm05a8C9dN8dIUmo4Be2+pMRb6f55i+UIYrluu2E=
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw=
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 h1:Heh92YLNrK2KTXiLj+cXbCTKG1TmZVSNagyvsGtoqHk=
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8=
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 h1:wAL6q8MTf0kL6EGFribwUhMnJW3YovdDUslFgcQ1iWU=
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8=
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 h1:HR/ylkkLmGdSSDaD8IDP+SZrdhV1Kibl9KrHxJ9eciw=
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

View File

@ -7,7 +7,7 @@ require (
github.com/ninedraft/israce v0.0.3
github.com/pingcap/errors v0.11.5-0.20250523034308-74f78ae071ee
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23
github.com/pingcap/tidb v1.1.0-beta.0.20250609033843-a165d9fd7c01
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.10.0

View File

@ -1351,8 +1351,8 @@ github.com/pingcap/fn v1.0.0/go.mod h1:u9WZ1ZiOD1RpNhcI42RucFh/lBuzTu6rw88a+oF2Z
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 h1:surzm05a8C9dN8dIUmo4Be2+pMRb6f55i+UIYrluu2E=
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw=
github.com/pingcap/kvproto v0.0.0-20241113043844-e1fa7ea8c302/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8=
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4 h1:Heh92YLNrK2KTXiLj+cXbCTKG1TmZVSNagyvsGtoqHk=
github.com/pingcap/kvproto v0.0.0-20250728031536-f08901d17bf4/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8=
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23 h1:wAL6q8MTf0kL6EGFribwUhMnJW3YovdDUslFgcQ1iWU=
github.com/pingcap/kvproto v0.0.0-20251023055424-e9d10f5dcd23/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8=
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM=
github.com/pingcap/log v1.1.0/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
github.com/pingcap/log v1.1.1-0.20250514022801-14f3b4ca066e h1:8AZZRv1Ox9FVGATVZBBgr6y1MrNBQFABEdovNJt1QIc=

View File

@ -64,8 +64,10 @@ import (
"github.com/tikv/client-go/v2/metrics"
"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/tikvrpc"
"github.com/tikv/client-go/v2/trace"
"github.com/tikv/client-go/v2/util"
"github.com/tikv/client-go/v2/util/async"
"github.com/tikv/client-go/v2/util/redact"
"github.com/tikv/pd/client/errs"
pderr "github.com/tikv/pd/client/errs"
)
@ -993,6 +995,10 @@ func (s *sendReqState) next() (done bool) {
logutil.Eventf(bo.GetCtx(), "send %s request to region %d at %s", req.Type, s.args.regionID.id, s.vars.rpcCtx.Addr)
s.storeAddr = s.vars.rpcCtx.Addr
// Extract trace ID from context and propagate to TiKV
if traceID := trace.TraceIDFromContext(bo.GetCtx()); len(traceID) > 0 {
req.Context.TraceId = traceID
}
req.Context.ClusterId = s.vars.rpcCtx.ClusterID
if req.InputRequestSource != "" && s.replicaSelector != nil {
patchRequestSource(req, s.replicaSelector.replicaType())
@ -1122,9 +1128,73 @@ func (s *sendReqState) send() (canceled bool) {
}
if !injectFailOnSend {
// Emit kv.request.send trace event before sending
if trace.IsCategoryEnabled(trace.CategoryKVRequest) {
var storeID uint64
var storeAddr string
if rpcCtx.Store != nil {
storeID = rpcCtx.Store.StoreID()
storeAddr = rpcCtx.Store.GetAddr()
}
fields := []zap.Field{
zap.Stringer("cmd", req.Type),
zap.Uint64("region_id", rpcCtx.Region.id),
zap.Uint64("region_ver", rpcCtx.Region.ver),
zap.Uint64("region_confVer", rpcCtx.Region.confVer),
zap.Uint64("store_id", storeID),
zap.String("store_addr", storeAddr),
zap.Duration("timeout", s.args.timeout),
}
if rpcCtx.Meta != nil {
fields = append(fields,
zap.String("region_start_key", redact.Key(rpcCtx.Meta.GetStartKey())),
zap.String("region_end_key", redact.Key(rpcCtx.Meta.GetEndKey())),
)
}
trace.TraceEvent(ctx, trace.CategoryKVRequest, "kv.request.send", fields...)
}
start := time.Now()
s.vars.resp, s.vars.err = s.client.SendRequest(ctx, sendToAddr, req, s.args.timeout)
rpcDuration := time.Since(start)
// Emit kv.request.result trace event after receiving response
if trace.IsCategoryEnabled(trace.CategoryKVRequest) {
fields := []zap.Field{
zap.Stringer("cmd", req.Type),
zap.Duration("latency", rpcDuration),
zap.Bool("success", s.vars.err == nil && s.vars.resp != nil),
}
if s.vars.err != nil {
fields = append(fields, zap.Error(s.vars.err))
}
if s.vars.resp != nil && s.vars.resp.Resp != nil {
if regionErr, _ := s.vars.resp.GetRegionError(); regionErr != nil {
fields = append(fields, zap.String("region_error", regionErr.String()))
}
}
trace.TraceEvent(ctx, trace.CategoryKVRequest, "kv.request.result", fields...)
}
if trace.IsCategoryEnabled(trace.CategoryKVRequest) && s.vars.resp != nil && s.vars.resp.Resp != nil {
if copResp, ok := s.vars.resp.Resp.(*coprocessor.Response); ok && copResp.OtherError != "" {
var storeID uint64
var storeAddr string
if rpcCtx.Store != nil {
storeID = rpcCtx.Store.StoreID()
storeAddr = rpcCtx.Store.GetAddr()
}
trace.TraceEvent(ctx, trace.CategoryKVRequest, "cop.other_error",
zap.String("other_error", copResp.OtherError),
zap.Uint64("region_id", rpcCtx.Region.id),
zap.Uint64("region_ver", rpcCtx.Region.ver),
zap.Uint64("region_confVer", rpcCtx.Region.confVer),
zap.Uint64("store_id", storeID),
zap.String("store_addr", storeAddr),
)
}
}
if s.replicaSelector != nil {
recordAttemptedTime(s.replicaSelector, rpcDuration)
}
@ -1246,6 +1316,10 @@ func (s *sendReqState) initForAsyncRequest() (ok bool) {
// set access location based on source and target "zone" label.
s.setReqAccessLocation(req)
// Extract trace ID from context and propagate to TiKV
if traceID := trace.TraceIDFromContext(bo.GetCtx()); len(traceID) > 0 {
req.Context.TraceId = traceID
}
req.Context.ClusterId = s.vars.rpcCtx.ClusterID
if req.InputRequestSource != "" && s.replicaSelector != nil {
patchRequestSource(req, s.replicaSelector.replicaType())

113
trace/trace.go Normal file
View File

@ -0,0 +1,113 @@
// Copyright 2025 TiKV Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package trace
import (
"context"
"sync/atomic"
"go.uber.org/zap"
)
// Category identifies a trace event family emitted from client-go.
type Category uint32
const (
// CategoryTxn2PC traces two-phase commit prewrite and commit phases.
CategoryTxn2PC Category = iota
// CategoryTxnLockResolve traces lock resolution and conflict handling.
CategoryTxnLockResolve
// CategoryKVRequest traces individual KV request send and result events.
CategoryKVRequest
)
// TraceEventFunc is the function signature for recording trace events.
type TraceEventFunc func(ctx context.Context, category Category, name string, fields ...zap.Field)
// IsCategoryEnabledFunc is the function signature for checking if a category is enabled.
type IsCategoryEnabledFunc func(category Category) bool
// Default no-op implementations
func noopTraceEvent(context.Context, Category, string, ...zap.Field) {}
func noopIsCategoryEnabled(Category) bool { return false }
// Global function pointers stored independently
var (
globalTraceEventFunc atomic.Pointer[TraceEventFunc]
globalIsCategoryEnabledFunc atomic.Pointer[IsCategoryEnabledFunc]
)
func init() {
// Set default no-op implementations
defaultTraceEvent := TraceEventFunc(noopTraceEvent)
defaultIsCategoryEnabled := IsCategoryEnabledFunc(noopIsCategoryEnabled)
globalTraceEventFunc.Store(&defaultTraceEvent)
globalIsCategoryEnabledFunc.Store(&defaultIsCategoryEnabled)
}
// SetTraceEventFunc registers the trace event handler function.
// This is typically called once during application initialization (e.g., by TiDB).
// Passing nil will use a no-op implementation.
func SetTraceEventFunc(fn TraceEventFunc) {
if fn == nil {
fn = noopTraceEvent
}
globalTraceEventFunc.Store(&fn)
}
// SetIsCategoryEnabledFunc registers the category enablement check function.
// This can be updated independently of the trace event function.
// Passing nil will use a no-op implementation that returns false.
func SetIsCategoryEnabledFunc(fn IsCategoryEnabledFunc) {
if fn == nil {
fn = noopIsCategoryEnabled
}
globalIsCategoryEnabledFunc.Store(&fn)
}
// TraceEvent records a trace event using the registered function.
func TraceEvent(ctx context.Context, category Category, name string, fields ...zap.Field) {
fn := globalTraceEventFunc.Load()
(*fn)(ctx, category, name, fields...)
}
// IsCategoryEnabled checks if a category is enabled for tracing using the registered function.
func IsCategoryEnabled(category Category) bool {
fn := globalIsCategoryEnabledFunc.Load()
return (*fn)(category)
}
// Trace ID context management
// traceIDKey is the context key for storing trace IDs.
// This key is shared between TiDB and client-go for trace ID propagation.
type traceIDKey struct{}
// ContextWithTraceID returns a new context with the given trace ID attached.
func ContextWithTraceID(ctx context.Context, traceID []byte) context.Context {
return context.WithValue(ctx, traceIDKey{}, traceID)
}
// TraceIDFromContext extracts the trace ID from the context.
// Returns nil if no trace ID is present.
func TraceIDFromContext(ctx context.Context) []byte {
if ctx == nil {
return nil
}
if traceID, ok := ctx.Value(traceIDKey{}).([]byte); ok {
return traceID
}
return nil
}

View File

@ -49,6 +49,7 @@ import (
"github.com/tikv/client-go/v2/internal/logutil"
"github.com/tikv/client-go/v2/metrics"
"github.com/tikv/client-go/v2/tikvrpc"
"github.com/tikv/client-go/v2/trace"
"github.com/tikv/client-go/v2/util/redact"
"go.uber.org/zap"
)
@ -102,6 +103,13 @@ func (action actionCommit) handleSingleBatch(c *twoPhaseCommitter, bo *retry.Bac
c.resourceGroupTagger(req)
}
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.start",
zap.Uint64("startTS", c.startTS),
zap.Uint64("commitTS", c.commitTS),
zap.Uint64("regionID", batch.region.GetID()),
zap.Bool("isPrimary", batch.isPrimary),
zap.Int("keyCount", len(keys)))
tBegin := time.Now()
attempts := 0
@ -126,6 +134,9 @@ func (action actionCommit) handleSingleBatch(c *twoPhaseCommitter, bo *retry.Bac
// Unexpected error occurs, return it.
if err != nil {
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.result",
zap.Uint64("regionID", batch.region.GetID()),
zap.Bool("success", false))
return err
}
@ -245,6 +256,9 @@ func (action actionCommit) handleSingleBatch(c *twoPhaseCommitter, bo *retry.Bac
// Group that contains primary key is always the first.
// We mark transaction's status committed when we receive the first success response.
c.mu.committed = true
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "commit.batch.result",
zap.Uint64("regionID", batch.region.GetID()),
zap.Bool("success", true))
return nil
}

View File

@ -54,6 +54,7 @@ import (
"github.com/tikv/client-go/v2/metrics"
"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/tikvrpc"
"github.com/tikv/client-go/v2/trace"
"github.com/tikv/client-go/v2/txnkv/txnlock"
"github.com/tikv/client-go/v2/util"
"github.com/tikv/client-go/v2/util/redact"
@ -238,12 +239,21 @@ func (action actionPrewrite) handleSingleBatch(
handler := action.newSingleBatchPrewriteReqHandler(c, batch, bo)
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "prewrite.batch.start",
zap.Uint64("startTS", c.startTS),
zap.Uint64("regionID", batch.region.GetID()),
zap.Bool("isPrimary", batch.isPrimary),
zap.Int("keyCount", batch.mutations.Len()))
var retryable bool
for {
// It will return false if the request is success or meet an unretryable error.
// otherwise if the error is retryable, it will return true.
retryable, err = handler.sendReqAndCheck()
if !retryable {
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxn2PC, "prewrite.batch.result",
zap.Uint64("regionID", batch.region.GetID()),
zap.Bool("success", err == nil))
handler.drop(err)
return err
}

View File

@ -35,6 +35,7 @@ import (
"github.com/tikv/client-go/v2/metrics"
"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/tikvrpc"
"github.com/tikv/client-go/v2/trace"
"github.com/tikv/client-go/v2/util"
"github.com/tikv/client-go/v2/util/redact"
"go.uber.org/zap"
@ -485,7 +486,7 @@ func (lr *LockResolver) ResolveLocksDone(callerStartTS uint64, token int) {
lr.mu.Unlock()
}
func (lr *LockResolver) resolveLocks(bo *retry.Backoffer, opts ResolveLocksOptions) (ResolveLockResult, error) {
func (lr *LockResolver) resolveLocks(bo *retry.Backoffer, opts ResolveLocksOptions) (result ResolveLockResult, err error) {
callerStartTS, locks, forRead, lite, detail, pessimisticRegionResolve := opts.CallerStartTS, opts.Locks, opts.ForRead, opts.Lite, opts.Detail, opts.PessimisticRegionResolve
util.EvalFailpoint("tryResolveLock")
if lr.testingKnobs.meetLock != nil {
@ -497,6 +498,29 @@ func (lr *LockResolver) resolveLocks(bo *retry.Backoffer, opts ResolveLocksOptio
TTL: msBeforeTxnExpired.value(),
}, nil
}
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxnLockResolve, "resolve_locks.start",
zap.Uint64("callerStartTS", callerStartTS),
zap.Int("lockCount", len(locks)),
zap.Bool("forRead", forRead),
zap.Bool("lite", lite))
// trace the results
defer func() {
if trace.IsCategoryEnabled(trace.CategoryTxnLockResolve) {
fields := []zap.Field{
zap.Uint64("callerStartTS", callerStartTS),
zap.Int("lockCount", len(locks)),
zap.Int64("ttl", result.TTL),
zap.Int("ignoredLocks", len(result.IgnoreLocks)),
zap.Int("accessibleLocks", len(result.AccessLocks)),
}
if err != nil {
fields = append(fields, zap.Error(err))
}
trace.TraceEvent(bo.GetCtx(), trace.CategoryTxnLockResolve, "resolve_locks.finish", fields...)
}
}()
metrics.LockResolverCountWithResolve.Inc()
// This is the origin resolve lock time.
// TODO(you06): record the more details and calculate the total time by calculating the sum of details.