// Copyright 2026 PingCAP, Inc. Licensed under Apache-2.0. package metrics import ( "strconv" "time" "github.com/prometheus/client_golang/prometheus" ) var snapshotLabels = []string{GetKeyspaceLabel(), "changefeed", "stage"} var ( snapshotRetries = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "retries_total", Help: "CSE command retries started after a failed attempt.", }, snapshotLabels) snapshotBytes = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "bytes_total", Help: "Logical KV bytes published by export or acknowledged by the sink, including replayed writes.", }, snapshotLabels) snapshotRows = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "rows_total", Help: "KV rows published by export or acknowledged by the sink, including replayed writes.", }, snapshotLabels) snapshotStarted = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "stage_started_timestamp_seconds", Help: "Start of the latest execution attempt's stage. Export and apply overlap.", }, snapshotLabels) snapshotFinished = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "stage_finished_timestamp_seconds", Help: "Successful finish of the latest execution attempt's stage; zero while unfinished.", }, snapshotLabels) snapshotWorkers = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "active_workers", Help: "Active CSE commands or range apply workers.", }, snapshotLabels) snapshotDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "operation_duration_seconds", Help: "CSE command, worker write-budget acquisition, and batch enqueue-to-ACK duration.", Buckets: prometheus.ExponentialBuckets(0.001, 4, 11), }, snapshotLabels) snapshotErrors = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "errors_total", Help: "Failed CSE commands or snapshot executions. Canceled executions are included.", }, snapshotLabels) snapshotCPU = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "cse_cpu_seconds_total", Help: "Completed CSE commands' user and system CPU seconds.", }, []string{GetKeyspaceLabel(), "changefeed"}) snapshotValues = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "resource", Help: "Snapshot resource/progress values; kind identifies bytes, configured limits, or range counts.", }, []string{GetKeyspaceLabel(), "changefeed", "kind"}) ) var snapshotWorkerInflight = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "worker_inflight_bytes", Help: "Mounted data awaiting ACKs in each apply worker; excludes chunk and client overhead.", }, []string{GetKeyspaceLabel(), "changefeed", "worker"}) var snapshotWorkerBytes = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "worker_acknowledged_bytes_total", Help: "Logical KV bytes acknowledged by each apply worker, including replayed writes.", }, []string{GetKeyspaceLabel(), "changefeed", "worker"}) func (s *SnapshotStatistics) ResetWorker(worker int) { if s != nil { snapshotWorkerInflight.WithLabelValues(s.keyspace, s.changefeed, strconv.Itoa(worker)).Set(0) } } func (s *SnapshotStatistics) WorkerInflight(worker int, delta float64) { if s != nil { snapshotWorkerInflight.WithLabelValues(s.keyspace, s.changefeed, strconv.Itoa(worker)).Add(delta) } } func (s *SnapshotStatistics) WorkerOutput(worker int, bytes uint64) { if s != nil { snapshotWorkerBytes.WithLabelValues(s.keyspace, s.changefeed, strconv.Itoa(worker)).Add(float64(bytes)) } } func initSnapshotMetrics(r *prometheus.Registry) { r.MustRegister(snapshotWorkerInflight, snapshotWorkerBytes, snapshotBytes, snapshotRows, snapshotStarted, snapshotFinished, snapshotWorkers, snapshotDuration, snapshotErrors, snapshotRetries, snapshotCPU, snapshotValues, ) } // SnapshotStatistics is shared by one executor's exporter, controller and sink. // Nil statistics allow package-local users to run without instrumentation. type SnapshotStatistics struct{ keyspace, changefeed string } func NewSnapshotStatistics(keyspace, changefeed string) *SnapshotStatistics { return &SnapshotStatistics{keyspace: keyspace, changefeed: changefeed} } func (s *SnapshotStatistics) Start(stage string) { if s == nil { return } snapshotStarted.WithLabelValues(s.keyspace, s.changefeed, stage).SetToCurrentTime() snapshotFinished.WithLabelValues(s.keyspace, s.changefeed, stage).Set(0) snapshotBytes.WithLabelValues(s.keyspace, s.changefeed, stage).Add(0) snapshotRows.WithLabelValues(s.keyspace, s.changefeed, stage).Add(0) } func (s *SnapshotStatistics) Finish(stage string) { if s != nil { snapshotFinished.WithLabelValues(s.keyspace, s.changefeed, stage).SetToCurrentTime() } } func (s *SnapshotStatistics) Output(stage string, bytes, rows uint64) { if s == nil { return } snapshotBytes.WithLabelValues(s.keyspace, s.changefeed, stage).Add(float64(bytes)) snapshotRows.WithLabelValues(s.keyspace, s.changefeed, stage).Add(float64(rows)) } func (s *SnapshotStatistics) Worker(stage string, delta float64) { if s != nil { snapshotWorkers.WithLabelValues(s.keyspace, s.changefeed, stage).Add(delta) } } func (s *SnapshotStatistics) Observe(operation string, elapsed time.Duration) { if s != nil { snapshotDuration.WithLabelValues(s.keyspace, s.changefeed, operation).Observe(elapsed.Seconds()) } } func (s *SnapshotStatistics) Error(stage string) { if s != nil { snapshotErrors.WithLabelValues(s.keyspace, s.changefeed, stage).Inc() } } func (s *SnapshotStatistics) Set(kind string, value float64) { if s != nil { snapshotValues.WithLabelValues(s.keyspace, s.changefeed, kind).Set(value) } } func (s *SnapshotStatistics) Add(kind string, delta float64) { if s != nil { snapshotValues.WithLabelValues(s.keyspace, s.changefeed, kind).Add(delta) } } func (s *SnapshotStatistics) Process(cpu float64, accountingPeakRSS int64) { if s == nil { return } snapshotCPU.WithLabelValues(s.keyspace, s.changefeed).Add(cpu) // This is child resource accounting from wait4. On Linux, its RSS high-water // mark may include parent memory inherited before exec, rather than CSE runtime RSS. s.Set("last_child_accounting_peak_rss_bytes", float64(accountingPeakRSS)) } func (s *SnapshotStatistics) Retry(operation string) { if s != nil { snapshotRetries.WithLabelValues(s.keyspace, s.changefeed, operation).Inc() } }