Skip to content

Commit 66de092

Browse files
committed
sqlstats: set session meta fields directly on sqlstats.RecordedTxnStats
This commit replaces the SessionData pointer field on sqlstas.RecordedTxnStats with direct Application and UserNormalized fields for simpler access to session metadata. Epic: none Release note: None
1 parent 40c5937 commit 66de092

8 files changed

+15
-26
lines changed

Diff for: pkg/sql/conn_executor_exec.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4355,8 +4355,9 @@ func (ex *connExecutor) recordTransactionFinish(
43554355
// TODO(107318): add qos
43564356
// TODO(107318): add asoftime or ishistorical
43574357
// TODO(107318): add readonly
4358-
SessionData: ex.sessionData(),
4359-
TxnErr: txnErr,
4358+
TxnErr: txnErr,
4359+
Application: ex.applicationName,
4360+
UserNormalized: ex.sessionData().User().Normalized(),
43604361
}
43614362

43624363
if ex.server.cfg.TestingKnobs.OnRecordTxnFinish != nil {

Diff for: pkg/sql/exec_log.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@ func (p *planner) logTransaction(
471471

472472
*sampledTxn = eventpb.SampledTransaction{
473473
SkippedTransactions: int64(skippedTransactions),
474-
User: txnStats.SessionData.SessionUser().Normalized(),
475-
ApplicationName: txnStats.SessionData.ApplicationName,
474+
User: txnStats.UserNormalized,
475+
ApplicationName: txnStats.Application,
476476
TxnCounter: uint32(txnCounter),
477477
SessionID: txnStats.SessionID.String(),
478478
TransactionID: txnStats.TransactionID.String(),

Diff for: pkg/sql/instrumentation_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func TestSampledStatsCollectionOnNewFingerprint(t *testing.T) {
215215
OnRecordTxnFinish: func(isInternal bool, _ *sessionphase.Times, stmt string, txnStats sqlstats.RecordedTxnStats) {
216216
// We won't run into a race here because we'll only observe
217217
// txns from a single connection.
218-
if txnStats.SessionData.ApplicationName == testApp {
218+
if txnStats.Application == testApp {
219219
if strings.Contains(stmt, `SET application_name`) {
220220
return
221221
}

Diff for: pkg/sql/sqlstats/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ go_library(
2020
"//pkg/sql/clusterunique",
2121
"//pkg/sql/execstats",
2222
"//pkg/sql/sem/tree",
23-
"//pkg/sql/sessiondata",
2423
"//pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil",
2524
"//pkg/util/log",
2625
"//pkg/util/metric",

Diff for: pkg/sql/sqlstats/sslocal/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ go_test(
6565
"//pkg/sql/sem/catconstants",
6666
"//pkg/sql/sem/tree",
6767
"//pkg/sql/sessiondata",
68-
"//pkg/sql/sessiondatapb",
6968
"//pkg/sql/sessionphase",
7069
"//pkg/sql/sqlstats",
7170
"//pkg/sql/sqlstats/insights",

Diff for: pkg/sql/sqlstats/sslocal/sql_stats_test.go

+4-15
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
3131
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
3232
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
33-
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
3433
"github.com/cockroachdb/cockroach/pkg/sql/sessionphase"
3534
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
3635
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insights"
@@ -476,13 +475,8 @@ func TestExplicitTxnFingerprintAccounting(t *testing.T) {
476475
require.NoError(t,
477476
statsCollector.
478477
RecordTransaction(ctx, txnFingerprintID, sqlstats.RecordedTxnStats{
479-
SessionData: &sessiondata.SessionData{
480-
SessionData: sessiondatapb.SessionData{
481-
UserProto: username.RootUserName().EncodeProto(),
482-
Database: "defaultdb",
483-
ApplicationName: "appname_findme",
484-
},
485-
},
478+
UserNormalized: username.RootUser,
479+
Application: "appname_findme",
486480
}))
487481
}()
488482
for _, fingerprint := range testCase.fingerprints {
@@ -608,13 +602,8 @@ func TestAssociatingStmtStatsWithTxnFingerprint(t *testing.T) {
608602
transactionFingerprintID := appstatspb.TransactionFingerprintID(txnFingerprintIDHash.Sum())
609603
statsCollector.EndTransaction(ctx, transactionFingerprintID)
610604
err := statsCollector.RecordTransaction(ctx, transactionFingerprintID, sqlstats.RecordedTxnStats{
611-
SessionData: &sessiondata.SessionData{
612-
SessionData: sessiondatapb.SessionData{
613-
UserProto: username.RootUserName().EncodeProto(),
614-
Database: "defaultdb",
615-
ApplicationName: "appname_findme",
616-
},
617-
},
605+
UserNormalized: username.RootUser,
606+
Application: "appname_findme",
618607
})
619608
require.NoError(t, err)
620609

Diff for: pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ func (s *StatsCollector) ObserveTransaction(
324324
Contention: &value.ExecStats.ContentionTime,
325325
StartTime: value.StartTime,
326326
EndTime: value.EndTime,
327-
User: value.SessionData.User().Normalized(),
328-
ApplicationName: value.SessionData.ApplicationName,
327+
User: value.UserNormalized,
328+
ApplicationName: value.Application,
329329
RowsRead: value.RowsRead,
330330
RowsWritten: value.RowsWritten,
331331
RetryCount: value.RetryCount,

Diff for: pkg/sql/sqlstats/ssprovider.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/cockroachdb/cockroach/pkg/sql/clusterunique"
1818
"github.com/cockroachdb/cockroach/pkg/sql/execstats"
1919
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
20-
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
2120
"github.com/cockroachdb/cockroach/pkg/util/uuid"
2221
)
2322

@@ -118,8 +117,10 @@ type RecordedTxnStats struct {
118117
RowsWritten int64
119118
BytesRead int64
120119
Priority roachpb.UserPriority
121-
SessionData *sessiondata.SessionData
122120
TxnErr error
121+
Application string
122+
// Normalized user name.
123+
UserNormalized string
123124
}
124125

125126
// SSDrainer is the interface for draining or resetting sql stats.

0 commit comments

Comments
 (0)