Skip to content

Commit 55247e7

Browse files
committed
sql: add bytes_read metric
Add new `sql.statements.bytes_read.count` metric that counts the number of bytes scanned by SQL statements. This is the same value that's collected by SQL stats for each statement and transaction, except in aggregated metric form. Epic: AOP-30 Release note (sql change): Added sql.statements.bytes_read.count metric that counts the number of bytes scanned by SQL statements.
1 parent de81162 commit 55247e7

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

docs/generated/metrics/metrics.yaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8747,17 +8747,33 @@ layers:
87478747
unit: COUNT
87488748
aggregation: AVG
87498749
derivative: NON_NEGATIVE_DERIVATIVE
8750+
- name: sql.statements.bytes_read.count
8751+
exported_name: sql_statements_bytes_read_count
8752+
description: Number of bytes read by SQL statements
8753+
y_axis_label: SQL Statements
8754+
type: COUNTER
8755+
unit: BYTES
8756+
aggregation: AVG
8757+
derivative: NON_NEGATIVE_DERIVATIVE
8758+
- name: sql.statements.bytes_read.count.internal
8759+
exported_name: sql_statements_bytes_read_count_internal
8760+
description: Number of bytes read by SQL statements (internal queries)
8761+
y_axis_label: SQL Internal Statements
8762+
type: COUNTER
8763+
unit: BYTES
8764+
aggregation: AVG
8765+
derivative: NON_NEGATIVE_DERIVATIVE
87508766
- name: sql.statements.rows_read.count
87518767
exported_name: sql_statements_rows_read_count
8752-
description: Number of rows read by SQL statements from primary and secondary indexes
8768+
description: Number of rows read by SQL statements
87538769
y_axis_label: SQL Statements
87548770
type: COUNTER
87558771
unit: COUNT
87568772
aggregation: AVG
87578773
derivative: NON_NEGATIVE_DERIVATIVE
87588774
- name: sql.statements.rows_read.count.internal
87598775
exported_name: sql_statements_rows_read_count_internal
8760-
description: Number of rows read by SQL statements from primary and secondary indexes (internal queries)
8776+
description: Number of rows read by SQL statements (internal queries)
87618777
y_axis_label: SQL Internal Statements
87628778
type: COUNTER
87638779
unit: COUNT

pkg/sql/conn_executor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ func makeMetrics(internal bool, sv *settings.Values) Metrics {
626626
TxnRetryCount: metric.NewCounter(getMetricMeta(MetaTxnRetry, internal)),
627627
StatementRetryCount: metric.NewCounter(getMetricMeta(MetaStatementRetry, internal)),
628628
StatementRowsRead: metric.NewCounter(getMetricMeta(MetaStatementRowsRead, internal)),
629+
StatementBytesRead: metric.NewCounter(getMetricMeta(MetaStatementBytesRead, internal)),
629630
},
630631
StartedStatementCounters: makeStartedStatementCounters(internal),
631632
ExecutedStatementCounters: makeExecutedStatementCounters(internal),

pkg/sql/exec_util.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,10 +1516,16 @@ var (
15161516
}
15171517
MetaStatementRowsRead = metric.Metadata{
15181518
Name: "sql.statements.rows_read.count",
1519-
Help: "Number of rows read by SQL statements from primary and secondary indexes",
1519+
Help: "Number of rows read by SQL statements",
15201520
Measurement: "SQL Statements",
15211521
Unit: metric.Unit_COUNT,
15221522
}
1523+
MetaStatementBytesRead = metric.Metadata{
1524+
Name: "sql.statements.bytes_read.count",
1525+
Help: "Number of bytes read by SQL statements",
1526+
Measurement: "SQL Statements",
1527+
Unit: metric.Unit_BYTES,
1528+
}
15231529
)
15241530

15251531
func getMetricMeta(meta metric.Metadata, internal bool) metric.Metadata {

pkg/sql/executor_statement_metrics.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ type EngineMetrics struct {
8888
// primary and secondary indexes. Note that some secondary indexes can have
8989
// multiple index rows per primary index row (e.g. inverted and vector).
9090
StatementRowsRead *metric.Counter
91+
92+
// StatementBytesRead counts the number of bytes scanned by SQL statements
93+
// from primary and secondary indexes.
94+
StatementBytesRead *metric.Counter
9195
}
9296

9397
// EngineMetrics implements the metric.Struct interface.
@@ -204,6 +208,7 @@ func (ex *connExecutor) recordStatementSummary(
204208

205209
// Update SQL statement metrics.
206210
ex.metrics.EngineMetrics.StatementRowsRead.Inc(stats.rowsRead)
211+
ex.metrics.EngineMetrics.StatementBytesRead.Inc(stats.bytesRead)
207212

208213
if ex.statsCollector.EnabledForTransaction() {
209214
recordedStmtStats := &sqlstats.RecordedStmtStats{

pkg/sql/metric_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ func TestStatementMetrics(t *testing.T) {
246246
{query: `SELECT * FROM db.t ORDER BY v <-> '[2, 2]' LIMIT 1`, rowsRead: 3},
247247
}
248248

249-
lastRowsRead := s.MustGetSQLCounter(sql.MetaStatementRowsRead.Name)
250-
251249
for _, tc := range testCases {
252250
t.Run(tc.query, func(t *testing.T) {
253251
for _, vectorized := range []string{"off", "on"} {
@@ -257,11 +255,23 @@ func TestStatementMetrics(t *testing.T) {
257255
return
258256
}
259257

258+
// Get initial values of the counters.
259+
lastRowsRead := s.MustGetSQLCounter(sql.MetaStatementRowsRead.Name)
260+
lastBytesRead := s.MustGetSQLCounter(sql.MetaStatementBytesRead.Name)
261+
260262
runner.Exec(t, tc.query)
263+
264+
// Test the rows read metric.
261265
actual, err := checkCounterDelta(
262266
s, sql.MetaStatementRowsRead, lastRowsRead, tc.rowsRead)
263267
require.NoError(t, err)
264268
lastRowsRead = actual
269+
270+
// If there were rows read, then expect bytes read to have
271+
// increased. Don't check for a specific value, since there are
272+
// too many factors that can change this.
273+
currBytesRead := s.MustGetSQLCounter(sql.MetaStatementBytesRead.Name)
274+
require.Greater(t, currBytesRead, lastBytesRead)
265275
})
266276
}
267277
})

0 commit comments

Comments
 (0)