Skip to content

mysql: move MySQL-only metrics into the MySQL sink package - #5866

Merged
ti-chi-bot[bot] merged 4 commits into
pingcap:masterfrom
3AceShowHand:mysql-affected-rows
Aug 4, 2026
Merged

mysql: move MySQL-only metrics into the MySQL sink package#5866
ti-chi-bot[bot] merged 4 commits into
pingcap:masterfrom
3AceShowHand:mysql-affected-rows

Conversation

@3AceShowHand

@3AceShowHand 3AceShowHand commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

What problem does this PR solve?

Issue Number: close #5867

Metrics that only apply to the MySQL sink were defined and registered in pkg/metrics. Affected-row recording and cleanup were also part of the generic metrics.Statistics type, although the values come from sql.Result.RowsAffected() in the MySQL writer. This split ownership across the generic metrics package and the MySQL sink.

What is changed and how it works?

  • Move the affected-row, transaction worker, conflict detection, and active-active conflict collectors to pkg/sink/mysql/metrics.go.
  • Record affected-row metrics in mysql.Writer and cache each labeled counter handle so WithLabelValues is called only on the first use.
  • Remove MySQL-specific affected-row state and methods from metrics.Statistics.
  • Register MySQL sink collectors through mysql.InitMetrics.
  • Reuse metrics.GetKeyspaceLabel for legacy and next-generation label names.
  • Delete affected-row series for a changefeed when its MySQL sink closes.
  • Fix encoder group metrics collection and cleanup: sample all input and output channel sizes with one ticker, populate the previously unused output-channel gauge, and delete the labeled series when the group exits.

The Prometheus metric names, labels, help text, and histogram buckets are unchanged. The encoder group output-channel gauge is now populated as intended.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test
  • No code

The following checks passed:

  • go test ./pkg/sink/mysql -run "^TestDMLEventRowsAffectedMetrics$" -count=1
  • go test --tags=intest ./pkg/sink/mysql -run "^TestDMLEventRowsAffectedMetrics$" -count=1
  • go test ./pkg/metrics -count=1
  • go test ./downstreamadapter/sink/mysql/causality ./downstreamadapter/sink/mysql ./server -run "^$" -count=1

Questions

Will it cause performance regression or break compatibility?

No performance regression is expected. The MySQL writer caches labeled affected-row counters instead of calling WithLabelValues for every DML event.

The Prometheus contract is unchanged. The Go collector variables move from pkg/metrics to pkg/sink/mysql, and all in-repository callers are updated.

Do you need to update user documentation, design documentation or monitoring documentation?

No. Existing dashboards and Prometheus queries continue to use the same metric names and labels.

Release note

None

Summary by CodeRabbit

  • New Features

    • Added detailed MySQL sink metrics for DML row counts, conflict handling, and worker performance.
    • Added encoder queue-depth monitoring across input and output channels.
    • Improved cleanup of per-changefeed and per-input metrics.
  • Bug Fixes

    • Corrected metric registration and labeling to improve monitoring consistency.
    • Prevented stale metrics from remaining after changefeeds or encoder inputs are removed.
  • Refactor

    • Consolidated metric ownership under the relevant MySQL sink and encoder components.
    • Removed obsolete transaction and row-tracking metrics.

@ti-chi-bot ti-chi-bot Bot added do-not-merge/needs-linked-issue release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Aug 3, 2026
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change exports the keyspace label helper, moves MySQL-specific Prometheus metrics into pkg/sink/mysql, updates metric recording and cleanup, removes generic row-affected tracking, and centralizes encoder queue metric collection.

Changes

Metrics Ownership Migration

Layer / File(s) Summary
Export shared keyspace label helper
pkg/metrics/*.go
GetKeyspaceLabel() replaces the private helper across shared metric definitions.
Add MySQL metric package
pkg/sink/mysql/metrics.go, pkg/sink/mysql/metrics_test.go, server/metrics.go
The MySQL sink defines and registers DML, conflict, and worker metrics. Tests cover affected-row recording and per-changefeed deletion.
Wire MySQL metric recording
pkg/sink/mysql/mysql_writer.go, pkg/sink/mysql/mysql_writer_dml_exec.go, pkg/sink/mysql/mysql_writer_for_active_active_sync_stats.go, downstreamadapter/sink/mysql/causality/conflict_detector.go, downstreamadapter/sink/mysql/sink.go
MySQL writers, conflict detection, active-active synchronization, and sink shutdown use the MySQL metrics package.
Remove legacy metric ownership
pkg/metrics/sink.go, pkg/metrics/statistics.go
Removed MySQL-specific metric declarations, registrations, row-affected storage, recording methods, and cleanup logic.
Centralize encoder queue metrics
pkg/sink/codec/encoder_group.go, pkg/sink/codec/metrics.go
A dedicated goroutine collects input and output queue gauges. Cleanup removes all labeled queue series.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

Possibly related PRs

  • pingcap/ticdc#5801 — Moves related statistics and metrics ownership into the MySQL sink package.

Suggested labels: lgtm, approved

Suggested reviewers: hongyunyan, wk989898

Poem

A rabbit counts each row with care,
MySQL metrics gather there.
Shared labels take a public name,
Queue gauges join the counting game.
Old counters clear at shutdown’s call.
Hop, hop—the metrics cover all.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The encoder-group metric collection and cleanup changes in pkg/sink/codec are unrelated to issue #5867. Remove the unrelated pkg/sink/codec changes or link an issue that requires the encoder-group metric updates.
Docstring Coverage ⚠️ Warning Docstring coverage is 43.75% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes satisfy issue #5867 by relocating MySQL metrics, moving affected-row recording to the writer, preserving Prometheus contracts, and managing series cleanup.
Title check ✅ Passed The title clearly and concisely describes the main change: moving MySQL-only metrics into the MySQL sink package.
Description check ✅ Passed The description follows the required template and explains the problem, implementation, tests, compatibility, documentation, and release note.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@3AceShowHand 3AceShowHand changed the title mysql: move mysql only metrics to own package to simplify the statistics mysql: move MySQL-only metrics into the MySQL sink package Aug 3, 2026
@ti-chi-bot ti-chi-bot Bot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/needs-linked-issue release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Aug 3, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/sink/mysql/metrics_test.go`:
- Around line 49-50: Update the assertion after
DeleteDMLEventRowsAffectedMetrics(changefeedID) to expect a collected series
count of 1, reflecting that only the otherChangefeedID actual/insert series
remains.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a41bec11-9da2-4f38-aa6e-1f5803cbd320

📥 Commits

Reviewing files that changed from the base of the PR and between af33cc1 and c9b87ad.

📒 Files selected for processing (19)
  • downstreamadapter/sink/mysql/causality/conflict_detector.go
  • downstreamadapter/sink/mysql/sink.go
  • pkg/metrics/changefeed.go
  • pkg/metrics/ddl.go
  • pkg/metrics/dispatcher.go
  • pkg/metrics/dynamic_stream.go
  • pkg/metrics/init.go
  • pkg/metrics/log_coordinator.go
  • pkg/metrics/maintainer.go
  • pkg/metrics/redo.go
  • pkg/metrics/scheduler.go
  • pkg/metrics/sink.go
  • pkg/metrics/statistics.go
  • pkg/sink/mysql/metrics.go
  • pkg/sink/mysql/metrics_test.go
  • pkg/sink/mysql/mysql_writer.go
  • pkg/sink/mysql/mysql_writer_dml_exec.go
  • pkg/sink/mysql/mysql_writer_for_active_active_sync_stats.go
  • server/metrics.go

Comment thread pkg/sink/mysql/metrics_test.go
@3AceShowHand

Copy link
Copy Markdown
Collaborator Author

/test all

@ti-chi-bot ti-chi-bot Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Aug 4, 2026
@3AceShowHand

Copy link
Copy Markdown
Collaborator Author

/test all

@ti-chi-bot ti-chi-bot Bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Aug 4, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hongyunyan, wk989898

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [hongyunyan,wk989898]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Aug 4, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-08-04 06:57:44.286996265 +0000 UTC m=+2511250.323091321: ☑️ agreed by hongyunyan.
  • 2026-08-04 07:34:17.481755608 +0000 UTC m=+2513443.517850664: ☑️ agreed by wk989898.

@ti-chi-bot
ti-chi-bot Bot merged commit c3c98e7 into pingcap:master Aug 4, 2026
40 checks passed
@ti-chi-bot

ti-chi-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

@3AceShowHand: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-cdc-mysql-integration-light-next-gen-legacy-safepoint 9803a56 link unknown /test pull-cdc-mysql-integration-light-next-gen-legacy-safepoint

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mysql: move MySQL-only metrics out of the generic metrics package

3 participants