diff --git a/README.md b/README.md index 329e719..96eab2b 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,11 @@ Container share memory can be configured with: postgres_share_memory: '1g' ``` +Metrics exporter for query performance can be activated with: +```yaml +postgres_ha_perf_metrics_exporter_disabled: false +``` + # Management The service is managed using [Docker Compose](https://docs.docker.com/compose/): @@ -75,4 +80,5 @@ admin@node-01.example.org:/docker/postgres-ha % docker-compose ps Name Command State Ports -------------------------------------------------------------------------------------------- postgres-ha docker-entrypoint.sh -p 5433 Up (healthy) 5432/tcp, 0.0.0.0:5433->5433/tcp +pg-metrics-exporter /bin/postgres_exporter Up 0.0.0.0:9187->9187/tcp ``` diff --git a/defaults/main.yml b/defaults/main.yml index 5c8df96..901a136 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -51,6 +51,18 @@ postgres_ha_users: [] # - user: 'dbuser' # pass: 'dbpass' +# Metrics Exporter +postgres_ha_metrics_exporter_cont_name: 'pg-metrics-exporter' +postgres_ha_metrics_exporter_cont_version: 'v0.15.0' +postgres_ha_metrics_exporter_cont_image: 'quay.io/prometheuscommunity/postgres-exporter:{{ postgres_ha_metrics_exporter_cont_version}}' +postgres_ha_metrics_exporter_cont_port: 9187 + +# Performence Metrics Exporter +postgres_ha_perf_metrics_exporter_disabled: true +postgres_ha_perf_metrics_exporter_cont_name: 'pg-perf-metrics-exporter' +postgres_ha_perf_metrics_exporter_cont_version: '1.2.6' +postgres_ha_perf_metrics_exporter_cont_image: 'ghcr.io/coroot/coroot-pg-agent:{{ postgres_ha_perf_metrics_exporter_cont_version }}' +postgres_ha_perf_metrics_exporter_cont_port: 9188 # Backups postgres_ha_backup: true postgres_ha_backup_enabled: true diff --git a/tasks/consul.yml b/tasks/consul.yml index 6d93798..d013851 100644 --- a/tasks/consul.yml +++ b/tasks/consul.yml @@ -24,3 +24,25 @@ name: 'PostgreSQL {{ postgres_ha_replica_enabled | ternary("Replica", "Tables") }} Healthcheck' type: 'script' script: '{{ postgres_ha_service_path }}/health.sh' + - name: 'postgres-metrics' + id: '{{ postgres_ha_metrics_exporter_cont_name }}' + tags: [ 'postgres-metrics', 'perf'] + port: '{{ postgres_ha_metrics_exporter_cont_port }}' + address: '{{ ansible_local.wireguard.address }}' + checks: + - id: '{{ postgres_ha_metrics_exporter_cont_name }}-status' + name: 'PostgreSQL Metrics exporter' + type: 'tcp' + tcp: 'localhost:{{ postgres_ha_metrics_exporter_cont_port }}' + - name: 'postgres-perf-metrics' + id: '{{ postgres_ha_perf_metrics_exporter_cont_name }}' + tags: [ 'postgres-metrics', 'perf'] + port: '{{ postgres_ha_perf_metrics_exporter_cont_port }}' + address: '{{ ansible_local.wireguard.address }}' + disabled: '{{ postgres_ha_perf_metrics_exporter_disabled | to_json }}' + checks: + - id: '{{ postgres_ha_perf_metrics_exporter_cont_name }}-status' + name: 'PostgreSQL Metrics exporter' + type: 'tcp' + tcp: 'localhost:{{ postgres_ha_perf_metrics_exporter_cont_port }}' + diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2 index b7de038..a8edd37 100644 --- a/templates/docker-compose.yml.j2 +++ b/templates/docker-compose.yml.j2 @@ -40,6 +40,27 @@ services: '-U', '{{ postgres_ha_admin_user }}' ] + metrics-exporter: + image: '{{ postgres_ha_metrics_exporter_cont_image }}' + container_name: '{{ postgres_ha_metrics_exporter_cont_name }}' + environment: + DATA_SOURCE_URI: '{{ postgres_ha_cont_name }}:{{ postgres_ha_cont_port }}/{{ postgres_ha_db_name }}?sslmode=disable' + DATA_SOURCE_USER: '{{ postgres_ha_admin_user }}' + DATA_SOURCE_PASS: '{{ postgres_ha_admin_pass }}' + ports: + - '{{ postgres_ha_metrics_exporter_cont_port }}:{{ postgres_ha_metrics_exporter_cont_port }}' + +{% if not postgres_ha_perf_metrics_exporter_disabled %} + perf-metrics-exporter: + image: '{{ postgres_ha_perf_metrics_exporter_cont_image }}' + container_name: '{{ postgres_ha_perf_metrics_exporter_cont_name }}' + environment: + DSN:"postgresql://{{ postgres_ha_cont_name }}:{{ postgres_ha_cont_port }}@{{ postgres_ha_cont_name }}:{{postgres_ha_cont_port }}/{{ postgres_ha_db_name }}?connect_timeout=1&statement_timeout=30000&sslmode=disable" + LISTEN:"0.0.0.0:{{ postgres_ha_perf_metrics_exporter_cont_port }}" + ports: + - '{{ postgres_ha_perf_metrics_exporter_cont_port }}:{{ postgres_ha_perf_metrics_exporter_cont_port }}' +{% endif %} + {% if postgres_ha_cont_networks != [] %} networks: {% for network in postgres_ha_cont_networks %} diff --git a/templates/init/alter_system.sql b/templates/init/alter_system.sql index 2f7f3d3..2d52c45 100644 --- a/templates/init/alter_system.sql +++ b/templates/init/alter_system.sql @@ -2,3 +2,7 @@ {% for key,val in postgres_ha_alter_system_settings.items() %} ALTER SYSTEM SET {{ key }} = '{{ val }}'; {% endfor %} +{% if not postgres_ha_perf_metrics_exporter_disabled %} +CREATE extension pg_stat_statements; +ALTER SYSTEM SET shared_preload_libraries = 'pg_stat_statements'; +{% endif %}