@@ -186,6 +186,7 @@ func NewController(
186186func (c * Controller ) collectMetrics (ctx context.Context ) error {
187187 ticker := time .NewTicker (5 * time .Second )
188188 defer ticker .Stop ()
189+ defer metrics .ResetOwnerChangefeedMetrics ()
189190 for {
190191 select {
191192 case <- ctx .Done ():
@@ -203,21 +204,41 @@ func (c *Controller) collectMetrics(ctx context.Context) error {
203204 name := info .ChangefeedID .Name ()
204205 metrics .ChangefeedStatusGauge .WithLabelValues (keyspace , name ).Set (float64 (info .State .ToInt ()))
205206
206- // don't update checkpoint ts and checkpoint ts lag for stopped changefeed
207- if info .State == config .StateStopped {
207+ if ! updateChangefeedCheckpointMetrics (
208+ keyspace ,
209+ name ,
210+ info .State ,
211+ cf .GetLastSavedCheckPointTs (),
212+ c .pdClock .CurrentTime (),
213+ ) {
208214 return
209215 }
210-
211- pdPhysicalTime := oracle .GetPhysical (c .pdClock .CurrentTime ())
212- phyCkpTs := oracle .ExtractPhysical (cf .GetLastSavedCheckPointTs ())
213- lag := float64 (pdPhysicalTime - phyCkpTs ) / 1e3
214- metrics .ChangefeedCheckpointTsGauge .WithLabelValues (keyspace , name ).Set (float64 (phyCkpTs ))
215- metrics .ChangefeedCheckpointTsLagGauge .WithLabelValues (keyspace , name ).Set (lag )
216216 })
217217 }
218218 }
219219}
220220
221+ func updateChangefeedCheckpointMetrics (
222+ keyspace string ,
223+ name string ,
224+ state config.FeedState ,
225+ checkpointTs uint64 ,
226+ pdTime time.Time ,
227+ ) bool {
228+ switch state {
229+ case config .StateStopped , config .StateFinished , config .StateRemoved :
230+ metrics .DeleteChangefeedCheckpointMetrics (keyspace , name )
231+ return false
232+ }
233+
234+ pdPhysicalTime := oracle .GetPhysical (pdTime )
235+ phyCkpTs := oracle .ExtractPhysical (checkpointTs )
236+ lag := float64 (pdPhysicalTime - phyCkpTs ) / 1e3
237+ metrics .ChangefeedCheckpointTsGauge .WithLabelValues (keyspace , name ).Set (float64 (phyCkpTs ))
238+ metrics .ChangefeedCheckpointTsLagGauge .WithLabelValues (keyspace , name ).Set (lag )
239+ return true
240+ }
241+
221242// HandleEvent implements the event-driven process mode
222243func (c * Controller ) HandleEvent (ctx context.Context , event * Event ) {
223244 if event == nil {
0 commit comments