@@ -7,17 +7,14 @@ package sslocal
77
88import (
99 "context"
10- "time"
1110
1211 "github.com/cockroachdb/cockroach/pkg/settings/cluster"
1312 "github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
1413 "github.com/cockroachdb/cockroach/pkg/sql/clusterunique"
15- "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
1614 "github.com/cockroachdb/cockroach/pkg/sql/sessionphase"
1715 "github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
1816 "github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insights"
1917 "github.com/cockroachdb/cockroach/pkg/sql/sqlstats/ssmemstorage"
20- "github.com/cockroachdb/redact"
2118)
2219
2320type bufferedStmtStats []sqlstats.RecordedStmtStats
@@ -39,8 +36,7 @@ type bufferedStmtStats []sqlstats.RecordedStmtStats
3936//
4037// 2. The insights subsystem (insightsWriter) which is used to
4138// persist statement and transaction insights to an in-memory cache.
42- // Events are sent to the insights subsystem for async processing in
43- // observeStatement() and observeTransaction() respectively.
39+ // Events are sent to the insights subsystem for async processing.
4440type StatsCollector struct {
4541
4642 // stmtBuf contains the current transaction's statement
@@ -216,130 +212,18 @@ func (s *StatsCollector) SetStatementSampled(
216212 s .flushTarget .TrySetStatementSampled (fingerprint , implicitTxn , database )
217213}
218214
219- func getInsightStatus (statementError error ) insights.Statement_Status {
220- if statementError == nil {
221- return insights .Statement_Completed
222- }
223-
224- return insights .Statement_Failed
225- }
226-
227215func (s * StatsCollector ) shouldObserveInsights () bool {
228216 return sqlstats .StmtStatsEnable .Get (& s .st .SV ) && sqlstats .TxnStatsEnable .Get (& s .st .SV )
229217}
230218
231- // observeStatement sends the recorded statement stats to the insights system
232- // for further processing.
233- func (s * StatsCollector ) observeStatement (value sqlstats.RecordedStmtStats ) {
234- if ! s .sendInsights {
235- return
236- }
237-
238- var autoRetryReason string
239- if value .AutoRetryReason != nil {
240- autoRetryReason = value .AutoRetryReason .Error ()
241- }
242-
243- var contention * time.Duration
244- var cpuSQLNanos int64
245- if value .ExecStats != nil {
246- contention = & value .ExecStats .ContentionTime
247- cpuSQLNanos = value .ExecStats .CPUTime .Nanoseconds ()
248- }
249-
250- var errorCode string
251- var errorMsg redact.RedactableString
252- if value .StatementError != nil {
253- errorCode = pgerror .GetPGCode (value .StatementError ).String ()
254- errorMsg = redact .Sprint (value .StatementError )
255- }
256-
257- insight := insights.Statement {
258- ID : value .StatementID ,
259- FingerprintID : value .FingerprintID ,
260- LatencyInSeconds : value .ServiceLatencySec ,
261- Query : value .Query ,
262- Status : getInsightStatus (value .StatementError ),
263- StartTime : value .StartTime ,
264- EndTime : value .EndTime ,
265- FullScan : value .FullScan ,
266- PlanGist : value .PlanGist ,
267- Retries : int64 (value .AutoRetryCount ),
268- AutoRetryReason : autoRetryReason ,
269- RowsRead : value .RowsRead ,
270- RowsWritten : value .RowsWritten ,
271- Nodes : value .Nodes ,
272- KVNodeIDs : value .KVNodeIDs ,
273- Contention : contention ,
274- IndexRecommendations : value .IndexRecommendations ,
275- Database : value .Database ,
276- CPUSQLNanos : cpuSQLNanos ,
277- ErrorCode : errorCode ,
278- ErrorMsg : errorMsg ,
279- }
280- if s .insightsWriter != nil {
281- s .insightsWriter .ObserveStatement (value .SessionID , & insight )
282- }
283- }
284-
285- // observeTransaction sends the recorded transaction stats to the insights system
286- // for further processing.
287- func (s * StatsCollector ) observeTransaction (value sqlstats.RecordedTxnStats ) {
288- if ! s .sendInsights {
289- return
290- }
291-
292- var retryReason string
293- if value .AutoRetryReason != nil {
294- retryReason = value .AutoRetryReason .Error ()
295- }
296-
297- var cpuSQLNanos int64
298- if value .ExecStats .CPUTime .Nanoseconds () >= 0 {
299- cpuSQLNanos = value .ExecStats .CPUTime .Nanoseconds ()
300- }
301-
302- var errorCode string
303- var errorMsg redact.RedactableString
304- if value .TxnErr != nil {
305- errorCode = pgerror .GetPGCode (value .TxnErr ).String ()
306- errorMsg = redact .Sprint (value .TxnErr )
307- }
308-
309- status := insights .Transaction_Failed
310- if value .Committed {
311- status = insights .Transaction_Completed
312- }
313-
314- insight := insights.Transaction {
315- ID : value .TransactionID ,
316- FingerprintID : value .FingerprintID ,
317- UserPriority : value .Priority .String (),
318- ImplicitTxn : value .ImplicitTxn ,
319- Contention : & value .ExecStats .ContentionTime ,
320- StartTime : value .StartTime ,
321- EndTime : value .EndTime ,
322- User : value .SessionData .User ().Normalized (),
323- ApplicationName : value .SessionData .ApplicationName ,
324- RowsRead : value .RowsRead ,
325- RowsWritten : value .RowsWritten ,
326- RetryCount : value .RetryCount ,
327- AutoRetryReason : retryReason ,
328- CPUSQLNanos : cpuSQLNanos ,
329- LastErrorCode : errorCode ,
330- LastErrorMsg : errorMsg ,
331- Status : status ,
332- }
333- if s .insightsWriter != nil {
334- s .insightsWriter .ObserveTransaction (value .SessionID , & insight )
335- }
336- }
337-
338219// RecordStatement records the statistics of a statement.
339220func (s * StatsCollector ) RecordStatement (
340221 ctx context.Context , value sqlstats.RecordedStmtStats ,
341222) error {
342- s .observeStatement (value )
223+ if s .sendInsights && s .insightsWriter != nil {
224+ insight := insights .MakeStmtInsight (value )
225+ s .insightsWriter .ObserveStatement (value .SessionID , insight )
226+ }
343227
344228 // TODO(xinhaoz): This isn't the best place to set this, but we'll clean this up
345229 // when we refactor the stats collection code to send the stats to an ingester.
@@ -359,7 +243,10 @@ func (s *StatsCollector) RecordStatement(
359243func (s * StatsCollector ) RecordTransaction (
360244 ctx context.Context , value sqlstats.RecordedTxnStats ,
361245) error {
362- s .observeTransaction (value )
246+ if s .sendInsights && s .insightsWriter != nil {
247+ insight := insights .MakeTxnInsight (value )
248+ s .insightsWriter .ObserveTransaction (value .SessionID , insight )
249+ }
363250
364251 // TODO(117690): Unify StmtStatsEnable and TxnStatsEnable into a single cluster setting.
365252 if ! sqlstats .TxnStatsEnable .Get (& s .st .SV ) {
0 commit comments