-
Notifications
You must be signed in to change notification settings - Fork 593
feat(database_observability): Add scaffolding for db-o11y integration tests #5575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6954e57
add scaffolding for db-o11y integration tests
matthewnolf a6596b8
Merge branch 'main' into mn/db-o11y-mysql-integration
matthewnolf fee6ffa
Merge branch 'main' into mn/db-o11y-mysql-integration
matthewnolf f76d954
remove mysql-init container in favour of docker-entrypoint-initdb.d
matthewnolf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
integration-tests/docker/tests/database-observability-mysql/config.alloy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // WARNING: This configuration is for integration testing only. | ||
| // It is not intended to represent a recommended production configuration. | ||
|
|
||
| prometheus.exporter.mysql "mysql" { | ||
|
cristiangreco marked this conversation as resolved.
|
||
| data_source_name = "root:rootpassword@tcp(mysql:3306)/testdb" | ||
| } | ||
|
|
||
| database_observability.mysql "test" { | ||
| data_source_name = "root:rootpassword@tcp(mysql:3306)/testdb" | ||
| forward_to = [loki.write.default.receiver] | ||
| targets = prometheus.exporter.mysql.mysql.targets | ||
|
|
||
| query_samples { | ||
| collect_interval = "1s" | ||
| } | ||
|
|
||
| query_details { | ||
| collect_interval = "1s" | ||
| } | ||
|
|
||
| schema_details { | ||
| collect_interval = "1s" | ||
| } | ||
|
|
||
| explain_plans { | ||
| collect_interval = "1s" | ||
| } | ||
|
|
||
| health_check { | ||
| collect_interval = "1s" | ||
| } | ||
| } | ||
|
|
||
| prometheus.scrape "mysql" { | ||
| targets = database_observability.mysql.test.targets | ||
| forward_to = [prometheus.remote_write.default.receiver] | ||
| } | ||
|
|
||
| prometheus.remote_write "default" { | ||
| endpoint { | ||
| url = "http://mimir:9009/api/v1/push" | ||
| } | ||
| external_labels = { | ||
| test_name = "database_observability_mysql", | ||
| } | ||
| } | ||
|
|
||
| loki.write "default" { | ||
| endpoint { | ||
| url = "http://loki:3100/loki/api/v1/push" | ||
| } | ||
| external_labels = { | ||
| test_name = "database_observability_mysql", | ||
| } | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
integration-tests/docker/tests/database-observability-mysql/init_mysql.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| echo "Creating test tables..." | ||
|
|
||
| mysql -u root -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" <<EOF | ||
| -- Create products table | ||
| CREATE TABLE IF NOT EXISTS products ( | ||
| id INT PRIMARY KEY AUTO_INCREMENT, | ||
| name VARCHAR(255) NOT NULL, | ||
| price DECIMAL(10, 2) NOT NULL, | ||
| quantity INT DEFAULT 0 | ||
| ); | ||
|
|
||
| -- Create orders table | ||
| CREATE TABLE IF NOT EXISTS orders ( | ||
| id INT PRIMARY KEY AUTO_INCREMENT, | ||
| product_id INT, | ||
| quantity INT NOT NULL, | ||
| total DECIMAL(10, 2) NOT NULL, | ||
| status VARCHAR(50) DEFAULT 'pending', | ||
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP | ||
| ); | ||
|
|
||
| -- Insert test data into products | ||
| INSERT INTO products (name, price, quantity) VALUES | ||
| ('Widget', 19.99, 100), | ||
| ('Gadget', 29.99, 50), | ||
| ('Gizmo', 39.99, 25); | ||
|
|
||
| -- Insert test data into orders | ||
| INSERT INTO orders (product_id, quantity, total, status) VALUES | ||
| (1, 2, 39.98, 'completed'), | ||
| (2, 1, 29.99, 'pending'), | ||
| (3, 3, 119.97, 'shipped'); | ||
|
|
||
| -- Run some queries to populate performance_schema | ||
| SELECT * FROM products WHERE id = 1; | ||
| SELECT * FROM products WHERE price > 20; | ||
| SELECT * FROM orders WHERE status = 'pending'; | ||
| SELECT p.name, o.total FROM products p JOIN orders o ON p.id = o.product_id; | ||
| EOF | ||
|
|
||
| echo "Test tables and data created successfully!" |
52 changes: 52 additions & 0 deletions
52
integration-tests/docker/tests/database-observability-mysql/mysql_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| //go:build alloyintegrationtests | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/grafana/alloy/integration-tests/docker/common" | ||
| ) | ||
|
|
||
| const testName = "database_observability_mysql" | ||
|
|
||
| func TestDatabaseObservabilityMySQLMetrics(t *testing.T) { | ||
| var metrics = []string{ | ||
| "database_observability_connection_info", | ||
| } | ||
| common.MimirMetricsTest(t, metrics, []string{}, testName) | ||
| } | ||
|
|
||
| func TestDatabaseObservabilityMySQLLogs(t *testing.T) { | ||
| common.AssertStatefulTestEnv(t) | ||
|
|
||
| expectedOps := []string{ | ||
| "health_status", | ||
| "query_association", | ||
| "query_parsed_table_name", | ||
| "schema_detection", | ||
| "table_detection", | ||
| "create_statement", | ||
| } | ||
|
|
||
| var logResponse common.LogResponse | ||
|
|
||
| require.EventuallyWithT(t, func(c *assert.CollectT) { | ||
| _, err := common.FetchDataFromURL(common.LogQuery(testName, 100), &logResponse) | ||
| assert.NoError(c, err) | ||
|
|
||
| ops := make(map[string]bool) | ||
| for _, result := range logResponse.Data.Result { | ||
| if op, ok := result.Stream["op"]; ok { | ||
| ops[op] = true | ||
| } | ||
| } | ||
|
|
||
| for _, op := range expectedOps { | ||
| assert.True(c, ops[op], "expected %s logs", op) | ||
| } | ||
| }, common.TestTimeoutEnv(t), common.DefaultRetryInterval) | ||
| } |
55 changes: 55 additions & 0 deletions
55
integration-tests/docker/tests/database-observability-postgres/config.alloy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // WARNING: This configuration is for integration testing only. | ||
| // It is not intended to represent a recommended production configuration. | ||
|
|
||
| prometheus.exporter.postgres "postgres" { | ||
| data_source_names = ["postgresql://root:rootpassword@postgres:5432/testdb?sslmode=disable"] | ||
| } | ||
|
|
||
| database_observability.postgres "test" { | ||
| data_source_name = "postgresql://root:rootpassword@postgres:5432/testdb?sslmode=disable" | ||
| forward_to = [loki.write.default.receiver] | ||
| targets = prometheus.exporter.postgres.postgres.targets | ||
|
|
||
| query_samples { | ||
| collect_interval = "1s" | ||
| } | ||
|
|
||
| query_details { | ||
| collect_interval = "1s" | ||
| } | ||
|
|
||
| schema_details { | ||
| collect_interval = "1s" | ||
| } | ||
|
|
||
| explain_plans { | ||
| collect_interval = "1s" | ||
| } | ||
|
|
||
| health_check { | ||
| collect_interval = "1s" | ||
| } | ||
| } | ||
|
|
||
| prometheus.scrape "postgres" { | ||
| targets = database_observability.postgres.test.targets | ||
| forward_to = [prometheus.remote_write.default.receiver] | ||
| } | ||
|
|
||
| prometheus.remote_write "default" { | ||
| endpoint { | ||
| url = "http://mimir:9009/api/v1/push" | ||
| } | ||
| external_labels = { | ||
| test_name = "database_observability_postgres", | ||
| } | ||
| } | ||
|
|
||
| loki.write "default" { | ||
| endpoint { | ||
| url = "http://loki:3100/loki/api/v1/push" | ||
| } | ||
| external_labels = { | ||
| test_name = "database_observability_postgres", | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
integration-tests/docker/tests/database-observability-postgres/init_postgres.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| # Wait for PostgreSQL to be ready | ||
| echo "Waiting for PostgreSQL to be ready..." | ||
| until PGPASSWORD=rootpassword psql -h postgres -U root -d testdb -c "SELECT 1" > /dev/null 2>&1; do | ||
| sleep 1 | ||
| done | ||
|
|
||
| echo "PostgreSQL is ready. Enabling pg_stat_statements and creating test tables..." | ||
|
|
||
| PGPASSWORD=rootpassword psql -h postgres -U root -d testdb <<EOF | ||
| -- Enable pg_stat_statements extension | ||
| CREATE EXTENSION IF NOT EXISTS pg_stat_statements; | ||
|
|
||
| -- Create products table | ||
| CREATE TABLE IF NOT EXISTS products ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL, | ||
| price DECIMAL(10, 2) NOT NULL, | ||
| quantity INT DEFAULT 0 | ||
| ); | ||
|
|
||
| -- Create orders table | ||
| CREATE TABLE IF NOT EXISTS orders ( | ||
| id SERIAL PRIMARY KEY, | ||
| product_id INT, | ||
| quantity INT NOT NULL, | ||
| total DECIMAL(10, 2) NOT NULL, | ||
| status VARCHAR(50) DEFAULT 'pending', | ||
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP | ||
| ); | ||
|
|
||
| -- Insert test data into products | ||
| INSERT INTO products (name, price, quantity) VALUES | ||
| ('Widget', 19.99, 100), | ||
| ('Gadget', 29.99, 50), | ||
| ('Gizmo', 39.99, 25); | ||
|
|
||
| -- Insert test data into orders | ||
| INSERT INTO orders (product_id, quantity, total, status) VALUES | ||
| (1, 2, 39.98, 'completed'), | ||
| (2, 1, 29.99, 'pending'), | ||
| (3, 3, 119.97, 'shipped'); | ||
|
|
||
| -- Run some queries to populate pg_stat_statements | ||
| SELECT * FROM products WHERE id = 1; | ||
| SELECT * FROM products WHERE price > 20; | ||
| SELECT * FROM orders WHERE status = 'pending'; | ||
| SELECT p.name, o.total FROM products p JOIN orders o ON p.id = o.product_id; | ||
| EOF | ||
|
|
||
| echo "Test tables and data created successfully!" |
51 changes: 51 additions & 0 deletions
51
integration-tests/docker/tests/database-observability-postgres/postgres_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| //go:build alloyintegrationtests | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/grafana/alloy/integration-tests/docker/common" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const testName = "database_observability_postgres" | ||
|
|
||
| func TestDatabaseObservabilityPostgresMetrics(t *testing.T) { | ||
| var metrics = []string{ | ||
| "database_observability_connection_info", | ||
| } | ||
| common.MimirMetricsTest(t, metrics, []string{}, testName) | ||
| } | ||
|
|
||
| func TestDatabaseObservabilityPostgresLogs(t *testing.T) { | ||
| common.AssertStatefulTestEnv(t) | ||
|
|
||
| expectedOps := []string{ | ||
| "health_status", | ||
| "query_association", | ||
| "query_parsed_table_name", | ||
| "schema_detection", | ||
| "table_detection", | ||
| "create_statement", | ||
| } | ||
|
|
||
| var logResponse common.LogResponse | ||
|
|
||
| require.EventuallyWithT(t, func(c *assert.CollectT) { | ||
| _, err := common.FetchDataFromURL(common.LogQuery(testName, 100), &logResponse) | ||
| assert.NoError(c, err) | ||
|
|
||
| ops := make(map[string]bool) | ||
| for _, result := range logResponse.Data.Result { | ||
| if op, ok := result.Stream["op"]; ok { | ||
| ops[op] = true | ||
| } | ||
| } | ||
|
|
||
| for _, op := range expectedOps { | ||
| assert.True(c, ops[op], "expected %s logs", op) | ||
| } | ||
| }, common.TestTimeoutEnv(t), common.DefaultRetryInterval) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.