EnableExporterInPostgreSQL runs SQL setup commands in `database` to enable the exporter to retrieve metrics. pgMonitor objects are created and expected extensions are installed. We also ensure that the monitoring user has the current password and can login.
(ctx context.Context, exec postgres.Executor, monitoringSecret *corev1.Secret, database, setup string)
| 74 | // extensions are installed. We also ensure that the monitoring user has the |
| 75 | // current password and can login. |
| 76 | func EnableExporterInPostgreSQL(ctx context.Context, exec postgres.Executor, |
| 77 | monitoringSecret *corev1.Secret, database, setup string) error { |
| 78 | log := logging.FromContext(ctx) |
| 79 | |
| 80 | stdout, stderr, err := exec.ExecInAllDatabases(ctx, |
| 81 | strings.Join([]string{ |
| 82 | // Quiet NOTICE messages from IF EXISTS statements. |
| 83 | // - https://www.postgresql.org/docs/current/runtime-config-client.html |
| 84 | `SET client_min_messages = WARNING;`, |
| 85 | |
| 86 | // Do not wait for changes to be replicated. [Since PostgreSQL v9.1] |
| 87 | // - https://www.postgresql.org/docs/current/runtime-config-wal.html |
| 88 | `SET synchronous_commit = LOCAL;`, |
| 89 | |
| 90 | // Exporter expects that extension(s) to be installed in all databases |
| 91 | // pg_stat_statements: https://access.crunchydata.com/documentation/pgmonitor/latest/exporter/ |
| 92 | "CREATE EXTENSION IF NOT EXISTS pg_stat_statements;", |
| 93 | |
| 94 | // Run idempotent update |
| 95 | "ALTER EXTENSION pg_stat_statements UPDATE;", |
| 96 | }, "\n"), |
| 97 | map[string]string{ |
| 98 | "ON_ERROR_STOP": "on", // Abort when any one statement fails. |
| 99 | "QUIET": "on", // Do not print successful commands to stdout. |
| 100 | }, |
| 101 | ) |
| 102 | |
| 103 | log.V(1).Info("applied pgMonitor objects", "database", "current and future databases", "stdout", stdout, "stderr", stderr) |
| 104 | |
| 105 | // NOTE: Setup is run last to ensure that the setup sql is used in the hash |
| 106 | if err == nil { |
| 107 | stdout, stderr, err = exec.ExecInDatabasesFromQuery(ctx, |
| 108 | `SELECT :'database'`, |
| 109 | strings.Join([]string{ |
| 110 | // Quiet NOTICE messages from IF EXISTS statements. |
| 111 | // - https://www.postgresql.org/docs/current/runtime-config-client.html |
| 112 | `SET client_min_messages = WARNING;`, |
| 113 | |
| 114 | // Do not wait for changes to be replicated. [Since PostgreSQL v9.1] |
| 115 | // - https://www.postgresql.org/docs/current/runtime-config-wal.html |
| 116 | `SET synchronous_commit = LOCAL;`, |
| 117 | |
| 118 | // Setup.sql file from the exporter image. sql is specific |
| 119 | // to the PostgreSQL version |
| 120 | setup, |
| 121 | |
| 122 | // pgnodemx: https://github.com/CrunchyData/pgnodemx |
| 123 | // The `monitor` schema is hard-coded in the setup SQL files |
| 124 | // from pgMonitor configuration |
| 125 | // https://github.com/CrunchyData/pgmonitor/blob/master/postgres_exporter/common/queries_nodemx.yml |
| 126 | "CREATE EXTENSION IF NOT EXISTS pgnodemx WITH SCHEMA monitor;", |
| 127 | |
| 128 | // Run idempotent update |
| 129 | "ALTER EXTENSION pgnodemx UPDATE;", |
| 130 | |
| 131 | // ccp_monitoring user is created in Setup.sql without a |
| 132 | // password; update the password and ensure that the ROLE |
| 133 | // can login to the database |
no test coverage detected