Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlstats: set session meta fields directly on sqlstats.RecordedTxnStats #143027

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4356,8 +4356,9 @@ func (ex *connExecutor) recordTransactionFinish(
// TODO(107318): add qos
// TODO(107318): add asoftime or ishistorical
// TODO(107318): add readonly
SessionData: ex.sessionData(),
TxnErr: txnErr,
TxnErr: txnErr,
Application: ex.applicationName.Load().(string),
UserNormalized: ex.sessionData().User().Normalized(),
}

if ex.server.cfg.TestingKnobs.OnRecordTxnFinish != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/exec_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@ func (p *planner) logTransaction(

*sampledTxn = eventpb.SampledTransaction{
SkippedTransactions: int64(skippedTransactions),
User: txnStats.SessionData.SessionUser().Normalized(),
ApplicationName: txnStats.SessionData.ApplicationName,
User: txnStats.UserNormalized,
ApplicationName: txnStats.Application,
TxnCounter: uint32(txnCounter),
SessionID: txnStats.SessionID.String(),
TransactionID: txnStats.TransactionID.String(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/instrumentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestSampledStatsCollectionOnNewFingerprint(t *testing.T) {
OnRecordTxnFinish: func(isInternal bool, _ *sessionphase.Times, stmt string, txnStats sqlstats.RecordedTxnStats) {
// We won't run into a race here because we'll only observe
// txns from a single connection.
if txnStats.SessionData.ApplicationName == testApp {
if txnStats.Application == testApp {
if strings.Contains(stmt, `SET application_name`) {
return
}
Expand Down
1 change: 0 additions & 1 deletion pkg/sql/sqlstats/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ go_library(
"//pkg/sql/clusterunique",
"//pkg/sql/execstats",
"//pkg/sql/sem/tree",
"//pkg/sql/sessiondata",
"//pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil",
"//pkg/util/log",
"//pkg/util/metric",
Expand Down
10 changes: 2 additions & 8 deletions pkg/sql/sqlstats/insights/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ func MakeTxnInsight(value sqlstats.RecordedTxnStats) *Transaction {
status = Transaction_Completed
}

var user, appName string
if value.SessionData != nil {
user = value.SessionData.User().Normalized()
appName = value.SessionData.ApplicationName
}

insight := &Transaction{
ID: value.TransactionID,
FingerprintID: value.FingerprintID,
Expand All @@ -50,8 +44,8 @@ func MakeTxnInsight(value sqlstats.RecordedTxnStats) *Transaction {
Contention: &value.ExecStats.ContentionTime,
StartTime: value.StartTime,
EndTime: value.EndTime,
User: user,
ApplicationName: appName,
User: value.UserNormalized,
ApplicationName: value.Application,
RowsRead: value.RowsRead,
RowsWritten: value.RowsWritten,
RetryCount: value.RetryCount,
Expand Down
1 change: 0 additions & 1 deletion pkg/sql/sqlstats/sslocal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ go_test(
"//pkg/sql/sem/catconstants",
"//pkg/sql/sem/tree",
"//pkg/sql/sessiondata",
"//pkg/sql/sessiondatapb",
"//pkg/sql/sessionphase",
"//pkg/sql/sqlstats",
"//pkg/sql/sqlstats/insights",
Expand Down
28 changes: 8 additions & 20 deletions pkg/sql/sqlstats/sslocal/sql_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/cockroach/pkg/sql/sessionphase"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insights"
Expand Down Expand Up @@ -474,17 +473,11 @@ func TestExplicitTxnFingerprintAccounting(t *testing.T) {
defer func() {
statsCollector.EndTransaction(ctx, txnFingerprintID)
require.NoError(t,
statsCollector.
RecordTransaction(ctx, sqlstats.RecordedTxnStats{
FingerprintID: txnFingerprintID,
SessionData: &sessiondata.SessionData{
SessionData: sessiondatapb.SessionData{
UserProto: username.RootUserName().EncodeProto(),
Database: "defaultdb",
ApplicationName: "appname_findme",
},
},
}))
statsCollector.RecordTransaction(ctx, sqlstats.RecordedTxnStats{
FingerprintID: txnFingerprintID,
UserNormalized: username.RootUser,
Application: "appname_findme",
}))
}()
for _, fingerprint := range testCase.fingerprints {
stmtFingerprintID := appstatspb.ConstructStatementFingerprintID(fingerprint, testCase.implicit, "defaultdb")
Expand Down Expand Up @@ -609,14 +602,9 @@ func TestAssociatingStmtStatsWithTxnFingerprint(t *testing.T) {
transactionFingerprintID := appstatspb.TransactionFingerprintID(txnFingerprintIDHash.Sum())
statsCollector.EndTransaction(ctx, transactionFingerprintID)
err := statsCollector.RecordTransaction(ctx, sqlstats.RecordedTxnStats{
FingerprintID: transactionFingerprintID,
SessionData: &sessiondata.SessionData{
SessionData: sessiondatapb.SessionData{
UserProto: username.RootUserName().EncodeProto(),
Database: "defaultdb",
ApplicationName: "appname_findme",
},
},
FingerprintID: transactionFingerprintID,
UserNormalized: username.RootUser,
Application: "appname_findme",
})
require.NoError(t, err)

Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/sqlstats/ssprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/clusterunique"
"github.com/cockroachdb/cockroach/pkg/sql/execstats"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)

Expand Down Expand Up @@ -119,8 +118,10 @@ type RecordedTxnStats struct {
RowsWritten int64
BytesRead int64
Priority roachpb.UserPriority
SessionData *sessiondata.SessionData
TxnErr error
Application string
// Normalized user name.
UserNormalized string
}

// SSDrainer is the interface for draining or resetting sql stats.
Expand Down
Loading