// Copyright 2026 PingCAP, Inc. Licensed under Apache-2.0. package kafka import ( "context" "strconv" "time" "github.com/pingcap/ticdc/pkg/common" "github.com/pingcap/ticdc/pkg/metrics" "github.com/prometheus/client_golang/prometheus" gometrics "github.com/rcrowley/go-metrics" ) var snapshotProducerLabels = []string{metrics.GetKeyspaceLabel(), "changefeed", "worker", "broker"} var ( snapshotOutgoingBytes = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "kafka_producer_outgoing_bytes_total", Help: "Franz worker client bytes written to Kafka.", }, snapshotProducerLabels) snapshotRequests = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "kafka_producer_requests_total", Help: "Franz worker client request writes.", }, snapshotProducerLabels) snapshotRequestsInFlight = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "kafka_producer_in_flight_requests", Help: "Requests awaiting Kafka responses for each worker.", }, snapshotProducerLabels) snapshotRequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "kafka_producer_request_duration_seconds", Help: "Franz worker client successful request duration.", }, snapshotProducerLabels) snapshotThrottleTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "kafka_producer_throttle_time_seconds", Help: "Franz worker client broker throttling duration.", }, snapshotProducerLabels) snapshotRecordsPerBatch = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "kafka_producer_records_per_batch", Help: "Franz worker client records in successful batches.", }, snapshotProducerLabels[:3]) snapshotCompressionRatio = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "kafka_producer_batch_compression_ratio", Help: "Franz worker client batch compression ratio times 100.", }, snapshotProducerLabels[:3]) snapshotOutgoingRate = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "kafka_producer_outgoing_byte_rate", Help: "Sarama worker client outgoing byte rate.", }, snapshotProducerLabels) snapshotRequestRate = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "kafka_producer_request_rate", Help: "Sarama worker client request rate.", }, snapshotProducerLabels) snapshotRequestLatency = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: "ticdc", Subsystem: "snapshot", Name: "kafka_producer_request_latency_seconds", Help: "Sarama worker client mean request latency.", }, snapshotProducerLabels) ) func registerSnapshotProducerMetrics(r *prometheus.Registry) { r.MustRegister( snapshotOutgoingBytes, snapshotRequests, snapshotRequestsInFlight, snapshotRequestDuration, snapshotThrottleTime, snapshotRecordsPerBatch, snapshotCompressionRatio, snapshotOutgoingRate, snapshotRequestRate, snapshotRequestLatency, ) } func cleanSnapshotProducerMetrics(id common.ChangeFeedID, worker string) { labels := prometheus.Labels{metrics.GetKeyspaceLabel(): id.Keyspace(), "changefeed": id.Name(), "worker": worker} snapshotOutgoingBytes.DeletePartialMatch(labels) snapshotRequests.DeletePartialMatch(labels) snapshotRequestsInFlight.DeletePartialMatch(labels) snapshotRequestDuration.DeletePartialMatch(labels) snapshotThrottleTime.DeletePartialMatch(labels) snapshotRecordsPerBatch.DeletePartialMatch(labels) snapshotCompressionRatio.DeletePartialMatch(labels) snapshotOutgoingRate.DeletePartialMatch(labels) snapshotRequestRate.DeletePartialMatch(labels) snapshotRequestLatency.DeletePartialMatch(labels) } // Snapshot producers never write or delete another worker's metric series. type snapshotSaramaCollector struct { factory *saramaFactory admin AdminClient } func (m *snapshotSaramaCollector) Run(ctx context.Context) { defer cleanSnapshotProducerMetrics(m.factory.changefeedID, m.factory.option.snapshotWorker) brokers := m.admin.GetAllBrokers(ctx) ticker := time.NewTicker(refreshMetricsInterval) defer ticker.Stop() for { for _, broker := range brokers { m.collect(strconv.Itoa(int(broker.ID))) } select { case <-ctx.Done(): return case <-ticker.C: } } } func (m *snapshotSaramaCollector) collect(broker string) { f := m.factory labels := []string{f.changefeedID.Keyspace(), f.changefeedID.Name(), f.option.snapshotWorker, broker} if v, ok := f.metricRegistry.Get(getBrokerMetricName(outgoingByteRateMetricNamePrefix, broker)).(gometrics.Meter); ok { snapshotOutgoingRate.WithLabelValues(labels...).Set(v.Snapshot().Rate1()) } if v, ok := f.metricRegistry.Get(getBrokerMetricName(requestRateMetricNamePrefix, broker)).(gometrics.Meter); ok { snapshotRequestRate.WithLabelValues(labels...).Set(v.Snapshot().Rate1()) } if v, ok := f.metricRegistry.Get(getBrokerMetricName(requestLatencyInMsMetricNamePrefix, broker)).(gometrics.Histogram); ok { snapshotRequestLatency.WithLabelValues(labels...).Set(v.Snapshot().Mean() / 1000) } if v, ok := f.metricRegistry.Get(getBrokerMetricName(requestsInFlightMetricNamePrefix, broker)).(gometrics.Counter); ok { snapshotRequestsInFlight.WithLabelValues(labels...).Set(float64(v.Snapshot().Count())) } }