Skip to content

Commit 4141e24

Browse files
committed
test(integration): add Compose-based MySQL harness and collector test runner; docs
Add a self-contained integration test that spins up MySQL 8.4 with performance_schema, seeds a small workload, and validates collectors via a locally-built exporter image. What's included: - docker-compose.yml (fixed network name: mysql-test) - mysql/conf.d/perf-schema.cnf - mysql/initdb/01-users.sql (exporter/app users, grants) - seed/seed.sh (INSERT/SELECT/UPDATE/SLEEP loop) - test_compose_collectors.sh: * builds mysqld-exporter:local * ensures users (caching_sha2_password) * runs exporter per --collect.* flag (no host port binding) * curls /metrics from inside the network * per-flag log files under ./_testlogs/ * robust readiness + failure diagnostics * optional auto-discovery of collector flags from --help Docs (README.md): - Document new flags: --collect.sys.user_summary_by_statement_latency --collect.sys.user_summary_by_statement_type * list emitted metric families and units - Note clamping behavior in --collect.sys.user_summary - Add "Docker Compose integration test" section with usage Usage: ./test_compose_collectors.sh Artifacts: _testlogs/exporter_<flag>.log License: Added the mandatory license comments to the newly-committed source files. This commit touches only test infra and docs; no exporter runtime code changes.
1 parent e9a2699 commit 4141e24

11 files changed

+432
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
.idea
1010
*.iml
1111
/vendor
12+
_testlogs/

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,30 @@ docker run -d \
206206
prom/mysqld-exporter
207207
```
208208

209+
## Docker Compose integration test
210+
211+
A self-contained test harness is included to validate collectors against a local MySQL:
212+
213+
- Spins up **MySQL 8.4** with `performance_schema` on
214+
- Seeds basic workload so `sys` summaries have data
215+
- Builds and runs your **local** exporter image per collector flag
216+
- Captures exporter logs per test under `_testlogs/`
217+
- Verifies metrics via in-network HTTP (no host port binding)
218+
219+
**Prereqs:** Docker & Docker Compose v2.
220+
221+
**Files:**
222+
- `docker-compose.yml` (MySQL service + one-shot seeder)
223+
- `mysql/conf.d/perf-schema.cnf` (ensures P_S consumers on)
224+
- `mysql/initdb/01-users.sql` (creates `exporter` & `app`; grants)
225+
- `seed/seed.sh` (simple INSERT/SELECT/UPDATE/SLEEP loop)
226+
- `test_compose_collectors.sh` (runner)
227+
228+
**Run:**
229+
```bash
230+
./test_compose_collectors.sh
231+
```
232+
209233
## heartbeat
210234

211235
With `collect.heartbeat` enabled, mysqld_exporter will scrape replication delay

collector/sys_user_summary_by_statement_latency.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2022 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
114
package collector
215

316
import (

collector/sys_user_summary_by_statement_latency_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2022 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
114
package collector
215

316
import (

collector/sys_user_summary_by_statement_type.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2022 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
114
package collector
215

316
import (

collector/sys_user_summary_by_statement_type_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2022 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
114
package collector
215

316
import (

docker-compose.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
services:
2+
mysql:
3+
image: mysql:8.4
4+
environment:
5+
MYSQL_ROOT_PASSWORD: rootpass
6+
MYSQL_DATABASE: testdb
7+
MYSQL_USER: app
8+
MYSQL_PASSWORD: app
9+
command: ["--performance_schema=ON"]
10+
healthcheck:
11+
test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -prootpass || exit 1"]
12+
interval: 5s
13+
timeout: 3s
14+
retries: 30
15+
volumes:
16+
- ./test-files/mysql/conf.d:/etc/mysql/conf.d:ro
17+
- ./test-files/mysql/initdb:/docker-entrypoint-initdb.d:ro
18+
networks:
19+
mysql-test:
20+
aliases:
21+
- mysql # ensure 'mysql' DNS name exists on this network
22+
23+
seed:
24+
image: mysql:8.4
25+
depends_on:
26+
mysql:
27+
condition: service_healthy
28+
environment:
29+
MYSQL_HOST: mysql
30+
MYSQL_USER: app
31+
MYSQL_PASSWORD: app
32+
MYSQL_DATABASE: testdb
33+
volumes:
34+
- ./test-files/seed/seed.sh:/seed/seed.sh:ro
35+
entrypoint: ["/bin/bash", "/seed/seed.sh"]
36+
networks: [mysql-test]
37+
38+
networks:
39+
mysql-test:
40+
name: mysql-test
41+
driver: bridge
42+
external: true
43+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[mysqld]
2+
performance_schema=ON
3+
# The following consumers are typically on by default in 8.x; keeping explicit for clarity.
4+
performance_schema_consumer_events_statements_history=ON
5+
performance_schema_consumer_events_statements_history_long=ON
6+
performance_schema_consumer_events_statements_current=ON
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE USER IF NOT EXISTS 'exporter'@'%' IDENTIFIED BY 'exporter';
2+
-- Force the plugin in case the user existed from a previous run with a different plugin
3+
ALTER USER 'exporter'@'%' IDENTIFIED WITH caching_sha2_password BY 'exporter';
4+
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%';
5+
6+
-- App user for seeding
7+
CREATE USER IF NOT EXISTS 'app'@'%' IDENTIFIED BY 'app';
8+
CREATE DATABASE IF NOT EXISTS testdb;
9+
GRANT ALL PRIVILEGES ON testdb.* TO 'app'@'%';
10+
11+
FLUSH PRIVILEGES;

test-files/seed/seed.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
host="${MYSQL_HOST:-mysql}"
5+
user="${MYSQL_USER:-app}"
6+
pass="${MYSQL_PASSWORD:-app}"
7+
db="${MYSQL_DATABASE:-testdb}"
8+
9+
# Helper to run mysql client quietly
10+
mysqlq() {
11+
mysql -h "$host" -u "$user" "-p$pass" -D "$db" -ss -N -e "$1" >/dev/null 2>&1
12+
}
13+
14+
echo "Seed: waiting for DNS for host '${host}'…"
15+
for i in {1..60}; do
16+
if getent hosts "$host" > /dev/null 2>&1; then
17+
echo "Seed: DNS OK ($(getent hosts "$host" | awk '{print $1}' | head -n1))"
18+
break
19+
fi
20+
sleep 1
21+
[[ $i -eq 60 ]] && { echo "Seed: DNS for '$host' not found"; exit 1; }
22+
done
23+
24+
echo "Seed: waiting for MySQL TCP on ${host}:3306…"
25+
for i in {1..60}; do
26+
if mysqladmin ping -h "$host" -u"$user" -p"$pass" --silent 2>/dev/null; then
27+
echo "Seed: MySQL is up."
28+
break
29+
fi
30+
sleep 1
31+
[[ $i -eq 60 ]] && { echo "Seed: MySQL not reachable"; exit 1; }
32+
done
33+
34+
# Ensure table exists (idempotent)
35+
mysqlq "CREATE TABLE IF NOT EXISTS t1 (id INT PRIMARY KEY AUTO_INCREMENT, v INT, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB;"
36+
37+
# Generate some mixed statements under the 'app' user
38+
for i in $(seq 1 200); do
39+
mysqlq "INSERT INTO t1 (v) VALUES (FLOOR(RAND()*1000));"
40+
mysqlq "SELECT COUNT(*) FROM t1;"
41+
mysqlq "UPDATE t1 SET v = v + 1 WHERE id % 10 = 0;"
42+
mysqlq "SELECT SLEEP(0.01);"
43+
done
44+
45+
echo "Seed workload finished."

0 commit comments

Comments
 (0)