Skip to content
Closed
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
23 changes: 23 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,29 @@ func RegisterMetrics() {
prometheus.MustRegister(IndexLookRowsCounter)
prometheus.MustRegister(IndexLookUpExecutorRowNumber)
prometheus.MustRegister(IndexLookUpCopTaskCount)
<<<<<<< HEAD
=======

// StmtSummary
prometheus.MustRegister(StmtSummaryWindowRecordCount)
prometheus.MustRegister(StmtSummaryWindowEvictedCount)
prometheus.MustRegister(StmtSummaryEvictedLogCounter)

// Channelz
setupChannelzCollector()
}

// Register registers custom collectors.
func Register(cs ...prometheus.Collector) {
prometheus.MustRegister(cs...)
}

// Unregister unregisters custom collectors.
func Unregister(cs ...prometheus.Collector) {
for _, c := range cs {
prometheus.Unregister(c)
}
>>>>>>> 6c431044127 (util/stmtsummary: add tidb_stmt_summary_persist_evicted (#68513))
}

var mode struct {
Expand Down
153 changes: 153 additions & 0 deletions pkg/metrics/metrics_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,156 @@ func TestRetLabel(t *testing.T) {
require.Equal(t, opSucc, RetLabel(nil))
require.Equal(t, opFailed, RetLabel(errors.New("test error")))
}
<<<<<<< HEAD
=======

func readGaugeValue(t *testing.T, gauge prometheus.Gauge) float64 {
t.Helper()
m := &dto.Metric{}
require.NoError(t, gauge.Write(m))
return m.GetGauge().GetValue()
}

func readCounterValue(t *testing.T, counter prometheus.Counter) float64 {
t.Helper()
m := &dto.Metric{}
require.NoError(t, counter.Write(m))
return m.GetCounter().GetValue()
}

func countCollectedMetrics(collector prometheus.Collector) int {
ch := make(chan prometheus.Metric, 16)
collector.Collect(ch)
close(ch)

count := 0
for range ch {
count++
}
return count
}

func TestStmtSummaryMetricLabels(t *testing.T) {
InitStmtSummaryMetrics()
require.Equal(t, 0, countCollectedMetrics(StmtSummaryWindowRecordCount))
require.Equal(t, 0, countCollectedMetrics(StmtSummaryWindowEvictedCount))
require.Equal(t, 0, countCollectedMetrics(StmtSummaryEvictedLogCounter))

SetStmtSummaryWindowMetrics(StmtSummaryTypeV1, 3, 1)
require.Equal(t, 1, countCollectedMetrics(StmtSummaryWindowRecordCount))
require.Equal(t, 1, countCollectedMetrics(StmtSummaryWindowEvictedCount))
require.Equal(t, 3.0, readGaugeValue(t, StmtSummaryWindowRecordCount.WithLabelValues(StmtSummaryTypeV1)))
require.Equal(t, 1.0, readGaugeValue(t, StmtSummaryWindowEvictedCount.WithLabelValues(StmtSummaryTypeV1)))

SetStmtSummaryWindowMetrics(StmtSummaryTypeV2, 5, 2)
require.Equal(t, 2, countCollectedMetrics(StmtSummaryWindowRecordCount))
require.Equal(t, 2, countCollectedMetrics(StmtSummaryWindowEvictedCount))
require.Equal(t, 5.0, readGaugeValue(t, StmtSummaryWindowRecordCount.WithLabelValues(StmtSummaryTypeV2)))
require.Equal(t, 2.0, readGaugeValue(t, StmtSummaryWindowEvictedCount.WithLabelValues(StmtSummaryTypeV2)))

StmtSummaryEvictedLogCounter.WithLabelValues(StmtSummaryTypeV2, StmtSummaryEvictedLogResultPersisted).Add(3)
StmtSummaryEvictedLogCounter.WithLabelValues(StmtSummaryTypeV2, StmtSummaryEvictedLogResultDropped).Inc()
require.Equal(t, 2, countCollectedMetrics(StmtSummaryEvictedLogCounter))
require.Equal(t, 3.0, readCounterValue(t, StmtSummaryEvictedLogCounter.WithLabelValues(StmtSummaryTypeV2, StmtSummaryEvictedLogResultPersisted)))
require.Equal(t, 1.0, readCounterValue(t, StmtSummaryEvictedLogCounter.WithLabelValues(StmtSummaryTypeV2, StmtSummaryEvictedLogResultDropped)))
}

func TestGrpcChannelzCollectorSingleton(t *testing.T) {
cleanupGrpcChannelzCollectorForTest()
t.Cleanup(cleanupGrpcChannelzCollectorForTest)

func() {
grpcChannelzCollector.mu.Lock()
defer grpcChannelzCollector.mu.Unlock()

require.NoError(t, initGrpcChannelzCollectorLocked())
firstServer := grpcChannelzCollector.server
firstListener := grpcChannelzCollector.listener
firstConn := grpcChannelzCollector.conn
firstCollector := grpcChannelzCollector.collector

require.NoError(t, initGrpcChannelzCollectorLocked())
require.Same(t, firstServer, grpcChannelzCollector.server)
require.Same(t, firstListener, grpcChannelzCollector.listener)
require.Same(t, firstConn, grpcChannelzCollector.conn)
require.True(t, firstCollector == grpcChannelzCollector.collector)
}()

cleanupGrpcChannelzCollectorForTest()

func() {
grpcChannelzCollector.mu.Lock()
defer grpcChannelzCollector.mu.Unlock()

require.Nil(t, grpcChannelzCollector.server)
require.Nil(t, grpcChannelzCollector.listener)
require.Nil(t, grpcChannelzCollector.conn)
require.Nil(t, grpcChannelzCollector.collector)
require.False(t, grpcChannelzCollector.registered)
}()
}

func TestSetupChannelzCollectorSkippedInTest(t *testing.T) {
cleanupGrpcChannelzCollectorForTest()
t.Cleanup(cleanupGrpcChannelzCollectorForTest)
require.True(t, intest.InTest)

setupChannelzCollector()

func() {
grpcChannelzCollector.mu.Lock()
defer grpcChannelzCollector.mu.Unlock()

require.Nil(t, grpcChannelzCollector.collector)
require.False(t, grpcChannelzCollector.registered)
}()
}

func TestGrpcChannelzCollectorGather(t *testing.T) {
cleanupGrpcChannelzCollectorForTest()
t.Cleanup(cleanupGrpcChannelzCollectorForTest)

var collector prometheus.Collector
func() {
grpcChannelzCollector.mu.Lock()
defer grpcChannelzCollector.mu.Unlock()

require.NoError(t, initGrpcChannelzCollectorLocked())
collector = grpcChannelzCollector.collector
}()

registry := prometheus.NewRegistry()
require.NoError(t, registry.Register(collector))
families, err := registry.Gather()
require.NoError(t, err)

require.NotNil(t, findMetricFamily(families, "tidb_grpc_channelz_fetch_errors_total"))
for _, family := range families {
for _, metric := range family.GetMetric() {
require.False(t, metricHasLabelValue(metric, "target", "bufnet"))
require.False(t, metricHasLabelValue(metric, "target", "passthrough:///bufnet"))
if strings.HasPrefix(family.GetName(), "tidb_grpc_channelz_socket_") {
require.False(t, metricHasLabelValue(metric, "remote", ""))
}
}
}
}

func findMetricFamily(families []*dto.MetricFamily, name string) *dto.MetricFamily {
for _, family := range families {
if family.GetName() == name {
return family
}
}
return nil
}

func metricHasLabelValue(metric *dto.Metric, name string, value string) bool {
for _, label := range metric.GetLabel() {
if label.GetName() == name && label.GetValue() == value {
return true
}
}
return false
}
>>>>>>> 6c431044127 (util/stmtsummary: add tidb_stmt_summary_persist_evicted (#68513))
129 changes: 129 additions & 0 deletions pkg/metrics/stmtsummary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2026 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metrics

import (
"sync"

metricscommon "github.com/pingcap/tidb/pkg/metrics/common"
"github.com/prometheus/client_golang/prometheus"
)

// Statement summary metrics.
const (
// StmtSummaryTypeV1 marks metrics reported by the legacy statement summary implementation.
StmtSummaryTypeV1 = "v1"
// StmtSummaryTypeV2 marks metrics reported by the persistent statement summary implementation.
StmtSummaryTypeV2 = "v2"

// StmtSummaryEvictedLogResultPersisted marks evicted records submitted to the stmt log.
StmtSummaryEvictedLogResultPersisted = "persisted"
// StmtSummaryEvictedLogResultDropped marks evicted records dropped before reaching the stmt log.
StmtSummaryEvictedLogResultDropped = "dropped"
)

var (
// StmtSummaryWindowRecordCount is a gauge that tracks the number of statement
// summary records in the current statement summary window.
StmtSummaryWindowRecordCount *prometheus.GaugeVec

// StmtSummaryWindowEvictedCount is a gauge that tracks the number of LRU
// evictions that have occurred in the current statement summary window.
// This value resets to 0 when the window rotates.
StmtSummaryWindowEvictedCount *prometheus.GaugeVec

// StmtSummaryEvictedLogCounter counts v2 evicted-log persistence outcomes.
StmtSummaryEvictedLogCounter *prometheus.CounterVec

stmtSummaryWindowRecordCountV1 prometheus.Gauge
stmtSummaryWindowRecordCountV2 prometheus.Gauge
stmtSummaryWindowEvictedCountV1 prometheus.Gauge
stmtSummaryWindowEvictedCountV2 prometheus.Gauge

stmtSummaryWindowMetricsMu sync.Mutex
)

// InitStmtSummaryMetrics initializes statement summary metrics.
func InitStmtSummaryMetrics() {
StmtSummaryWindowRecordCount = metricscommon.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "tidb",
Subsystem: "stmt_summary",
Name: "window_record_count",
Help: "The number of statement summary records currently tracked by statement summary.",
}, []string{LblType})

StmtSummaryWindowEvictedCount = metricscommon.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "tidb",
Subsystem: "stmt_summary",
Name: "window_evicted_count",
Help: "The number of LRU evictions in the current statement summary window.",
}, []string{LblType})

StmtSummaryEvictedLogCounter = metricscommon.NewCounterVec(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "stmt_summary",
Name: "evicted_log_total",
Help: "The number of v2 statement summary evicted-log records by result.",
}, []string{LblType, LblResult})

stmtSummaryWindowMetricsMu.Lock()
stmtSummaryWindowRecordCountV1 = nil
stmtSummaryWindowRecordCountV2 = nil
stmtSummaryWindowEvictedCountV1 = nil
stmtSummaryWindowEvictedCountV2 = nil
stmtSummaryWindowMetricsMu.Unlock()
}

// SetStmtSummaryWindowMetrics reports statement summary window metrics for a given implementation type.
func SetStmtSummaryWindowMetrics(typ string, recordCount, evictedCount float64) {
switch typ {
case StmtSummaryTypeV1:
recordGauge, evictedGauge := getStmtSummaryWindowMetricsLocked(typ)
recordGauge.Set(recordCount)
evictedGauge.Set(evictedCount)
case StmtSummaryTypeV2:
recordGauge, evictedGauge := getStmtSummaryWindowMetricsLocked(typ)
recordGauge.Set(recordCount)
evictedGauge.Set(evictedCount)
default:
StmtSummaryWindowRecordCount.WithLabelValues(typ).Set(recordCount)
StmtSummaryWindowEvictedCount.WithLabelValues(typ).Set(evictedCount)
}
}

func getStmtSummaryWindowMetricsLocked(typ string) (prometheus.Gauge, prometheus.Gauge) {
stmtSummaryWindowMetricsMu.Lock()
defer stmtSummaryWindowMetricsMu.Unlock()

switch typ {
case StmtSummaryTypeV1:
if stmtSummaryWindowRecordCountV1 == nil {
stmtSummaryWindowRecordCountV1 = StmtSummaryWindowRecordCount.WithLabelValues(typ)
stmtSummaryWindowEvictedCountV1 = StmtSummaryWindowEvictedCount.WithLabelValues(typ)
}
return stmtSummaryWindowRecordCountV1, stmtSummaryWindowEvictedCountV1
case StmtSummaryTypeV2:
if stmtSummaryWindowRecordCountV2 == nil {
stmtSummaryWindowRecordCountV2 = StmtSummaryWindowRecordCount.WithLabelValues(typ)
stmtSummaryWindowEvictedCountV2 = StmtSummaryWindowEvictedCount.WithLabelValues(typ)
}
return stmtSummaryWindowRecordCountV2, stmtSummaryWindowEvictedCountV2
default:
return nil, nil
}
}
Loading