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
sysschema, and fixes a failure mode in the existing--collect.sys.user_summaryon 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_latencyScrapes
sys.x$user_summary_by_statement_latencyto expose per-user totals and latency aggregates.--collect.sys.user_summary_by_statement_typeScrapes
sys.x$user_summary_by_statement_typeto expose per-user by statement type aggregates.Requirements: MySQL ≥ 5.7 with
performance_schemaenabled and thesysschema available.Harden existing collector
--collect.sys.user_summarySome MySQL 8.x installations occasionally surface negative values for
current_memory/total_memory_allocatedin thesysview, which previously caused a scan intouint64to 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_latencyLabels:
userEmits:
mysql_sys_user_summary_by_statement_latency_totalmysql_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_totalmysql_sys_user_summary_by_statement_rows_examined_totalmysql_sys_user_summary_by_statement_rows_affected_totalmysql_sys_user_summary_by_statement_full_scans_total--collect.sys.user_summary_by_statement_typeLabels:
user,statementEmits:
mysql_sys_user_summary_by_statement_type_totalmysql_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_totalmysql_sys_user_summary_by_statement_type_rows_examined_totalmysql_sys_user_summary_by_statement_type_rows_affected_totalmysql_sys_user_summary_by_statement_type_full_scans_totalImplementation notes
sys_user_summary.gostyle (namespacemysql, subsystemsys, metric stemuser_summary_by_*).picoSecondsconstant.Behavior change in
--collect.sys.user_summaryGREATEST(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.gosys_user_summary_by_statement_type_test.gosys_user_summary_test.go(robust SQL matcher, guard channel reads, fix column name, align with clamping)Each test uses
sqlmockand the repo’sMetricResult/readMetrichelpers 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/grantsexporterandapp;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)/metricsfrom inside the network_testlogs/TESTSset (plus optional auto-discovery of flags from--help)Run:
Backwards compatibility & perf
user_summaryclamp 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.gocollector/sys_user_summary_by_statement_type.gocollector/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.