feat(collector): add sys per-user latency/type collectors; harden user_summary; docs + compose integration test #972
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What & why
This PR adds two opt-in collectors that expose per-user statement activity from MySQL’s
sys
schema, and fixes a failure mode in the existing--collect.sys.user_summary
on MySQL 8.x. It also includes unit tests, docs, and a Docker Compose harness to exercise collectors locally.New collectors (opt-in)
--collect.sys.user_summary_by_statement_latency
Scrapes
sys.x$user_summary_by_statement_latency
to expose per-user totals and latency aggregates.--collect.sys.user_summary_by_statement_type
Scrapes
sys.x$user_summary_by_statement_type
to expose per-user by statement type aggregates.Requirements: MySQL ≥ 5.7 with
performance_schema
enabled and thesys
schema available.Harden existing collector
--collect.sys.user_summary
Some MySQL 8.x installations occasionally surface negative values for
current_memory
/total_memory_allocated
in thesys
view, which previously caused a scan intouint64
to fail and the scrape to error. We now clamp these to ≥ 0 (via SQL), avoiding exporter errors while preserving metric semantics.Flags & metrics
--collect.sys.user_summary_by_statement_latency
Labels:
user
Emits:
mysql_sys_user_summary_by_statement_latency_total
mysql_sys_user_summary_by_statement_latency
(seconds total)mysql_sys_user_summary_by_statement_max_latency
(seconds)mysql_sys_user_summary_by_statement_lock_latency
(seconds total)mysql_sys_user_summary_by_statement_cpu_latency
(seconds total)mysql_sys_user_summary_by_statement_rows_sent_total
mysql_sys_user_summary_by_statement_rows_examined_total
mysql_sys_user_summary_by_statement_rows_affected_total
mysql_sys_user_summary_by_statement_full_scans_total
--collect.sys.user_summary_by_statement_type
Labels:
user
,statement
Emits:
mysql_sys_user_summary_by_statement_type_total
mysql_sys_user_summary_by_statement_type_latency
(seconds total)mysql_sys_user_summary_by_statement_type_max_latency
(seconds)mysql_sys_user_summary_by_statement_type_lock_latency
(seconds total)mysql_sys_user_summary_by_statement_type_cpu_latency
(seconds total)mysql_sys_user_summary_by_statement_type_rows_sent_total
mysql_sys_user_summary_by_statement_type_rows_examined_total
mysql_sys_user_summary_by_statement_type_rows_affected_total
mysql_sys_user_summary_by_statement_type_full_scans_total
Implementation notes
sys_user_summary.go
style (namespacemysql
, subsystemsys
, metric stemuser_summary_by_*
).picoSeconds
constant.Behavior change in
--collect.sys.user_summary
GREATEST(current_memory, 0)
andGREATEST(total_memory_allocated, 0)
to avoid negative values in 8.x. No metric name/type changes.Testing
Unit tests
sys_user_summary_by_statement_latency_test.go
sys_user_summary_by_statement_type_test.go
sys_user_summary_test.go
(robust SQL matcher, guard channel reads, fix column name, align with clamping)Each test uses
sqlmock
and the repo’sMetricResult
/readMetric
helpers to verify:sys.x$…
view,Run:
Docker Compose integration harness (developer convenience)
A self-contained script to validate collectors against a local MySQL:
docker-compose.yml
(fixed networkmysql-test
)mysql/conf.d/perf-schema.cnf
(ensures Performance Schema/consumers on)mysql/initdb/01-users.sql
(creates/grantsexporter
andapp
;caching_sha2_password
)seed/seed.sh
(simple INSERT/SELECT/UPDATE/SLEEP to populate sys views)test_compose_collectors.sh
--collect.*
flag (inside the Docker network; no host port binding)/metrics
from inside the network_testlogs/
TESTS
set (plus optional auto-discovery of flags from--help
)Run:
Backwards compatibility & perf
user_summary
clamp only affects rare negative values; otherwise behavior and types are unchanged.sys.x$…
views; expected to be inexpensive with Performance Schema enabled.How to review
Key files:
collector/sys_user_summary_by_statement_latency.go
collector/sys_user_summary_by_statement_type.go
collector/sys_user_summary.go
(clamp)collector/*_test.go
(new + updated tests)README.md
(docs for new flags + integration test)docker-compose.yml
,mysql/
,seed/
,test_compose_collectors.sh
(integration harness)Happy to split the test harness/docs into a separate PR if you prefer, but they’re isolated from runtime code.