Skip to content

Commit

Permalink
metrics: add per-query-statement RPC count and scan processed key cou…
Browse files Browse the repository at this point in the history
…nt metrics (#54890)

close #54887
  • Loading branch information
HaoW30 authored Aug 6, 2024
1 parent 5aad7df commit a1606cf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
2 changes: 2 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func RegisterMetrics() {
prometheus.MustRegister(PseudoEstimation)
prometheus.MustRegister(PacketIOCounter)
prometheus.MustRegister(QueryDurationHistogram)
prometheus.MustRegister(QueryRPCHistogram)
prometheus.MustRegister(QueryProcessedKeyHistogram)
prometheus.MustRegister(QueryTotalCounter)
prometheus.MustRegister(AffectedRowsCounter)
prometheus.MustRegister(SchemaLeaseErrorCounter)
Expand Down
38 changes: 29 additions & 9 deletions pkg/metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ var (

// Metrics
var (
PacketIOCounter *prometheus.CounterVec
QueryDurationHistogram *prometheus.HistogramVec
QueryTotalCounter *prometheus.CounterVec
AffectedRowsCounter *prometheus.CounterVec
ConnGauge *prometheus.GaugeVec
DisconnectionCounter *prometheus.CounterVec
PreparedStmtGauge prometheus.Gauge
ExecuteErrorCounter *prometheus.CounterVec
CriticalErrorCounter prometheus.Counter
PacketIOCounter *prometheus.CounterVec
QueryDurationHistogram *prometheus.HistogramVec
QueryRPCHistogram *prometheus.HistogramVec
QueryProcessedKeyHistogram *prometheus.HistogramVec
QueryTotalCounter *prometheus.CounterVec
AffectedRowsCounter *prometheus.CounterVec
ConnGauge *prometheus.GaugeVec
DisconnectionCounter *prometheus.CounterVec
PreparedStmtGauge prometheus.Gauge
ExecuteErrorCounter *prometheus.CounterVec
CriticalErrorCounter prometheus.Counter

ServerStart = "server-start"
ServerStop = "server-stop"
Expand Down Expand Up @@ -92,6 +94,24 @@ func InitServerMetrics() {
Buckets: prometheus.ExponentialBuckets(0.0005, 2, 29), // 0.5ms ~ 1.5days
}, []string{LblSQLType, LblDb, LblResourceGroup})

QueryRPCHistogram = NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "tidb",
Subsystem: "server",
Name: "query_statement_rpc_count",
Help: "Bucketed histogram of execution rpc count of handled query statements.",
Buckets: prometheus.ExponentialBuckets(1, 1.5, 23), // 1 ~ 8388608
}, []string{LblSQLType, LblDb})

QueryProcessedKeyHistogram = NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "tidb",
Subsystem: "server",
Name: "query_statement_processed_keys",
Help: "Bucketed histogram of processed key count during the scan of handled query statements.",
Buckets: prometheus.ExponentialBuckets(1, 2, 32),
}, []string{LblSQLType, LblDb})

QueryTotalCounter = NewCounterVec(
prometheus.CounterOpts{
Namespace: "tidb",
Expand Down
4 changes: 4 additions & 0 deletions pkg/server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,10 @@ func (cc *clientConn) addMetrics(cmd byte, startTime time.Time, err error) {

for _, dbName := range session.GetDBNames(vars) {
metrics.QueryDurationHistogram.WithLabelValues(sqlType, dbName, vars.StmtCtx.ResourceGroupName).Observe(cost.Seconds())
metrics.QueryRPCHistogram.WithLabelValues(sqlType, dbName).Observe(float64(vars.StmtCtx.GetExecDetails().RequestCount))
if vars.StmtCtx.GetExecDetails().ScanDetail != nil {
metrics.QueryProcessedKeyHistogram.WithLabelValues(sqlType, dbName).Observe(float64(vars.StmtCtx.GetExecDetails().ScanDetail.ProcessedKeys))
}
}
}

Expand Down

0 comments on commit a1606cf

Please sign in to comment.