|
| 1 | +# Monitoring Postgres with OpenTelemetry and Last9 |
| 2 | + |
| 3 | +A guide for setting up Postgres monitoring using OpenTelemetry Collector with Last9. It collects Postgres metrics, and logs from Postgres and sends them to Last9. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +### 1. Prerequisites |
| 8 | + |
| 9 | +Ensure Docker and Docker Compose are installed on your system: |
| 10 | + |
| 11 | +```bash |
| 12 | +# Check Docker installation |
| 13 | +docker --version |
| 14 | + |
| 15 | +# Check Docker Compose installation |
| 16 | +docker compose version |
| 17 | +``` |
| 18 | + |
| 19 | +### 2. Configure OpenTelemetry Collector |
| 20 | + |
| 21 | +The setup uses the otel-collector-config.yaml file which defines: |
| 22 | +Prometheus receiver for scraping Postgres metrics |
| 23 | +Processors for batch processing and resource detection |
| 24 | +Last9 exporter configuration |
| 25 | +Before proceeding, update the Last9 authorization token: |
| 26 | + |
| 27 | +```bash |
| 28 | +# Edit the config file |
| 29 | +nano otel-collector-config.yaml |
| 30 | +``` |
| 31 | + |
| 32 | +In the `exporters` section, replace <LAST9_OTLP_AUTH_HEADER> with your actual Last9 authorization auth header and <LAST9_OTLP_ENDPOINT> with the endpoint URL. You can get the auth header from Last9 Integrations. |
| 33 | + |
| 34 | +### 3. Configure Postgres Exporter |
| 35 | + |
| 36 | +Update the environment variables in docker-compose.yaml: |
| 37 | + |
| 38 | +```yaml |
| 39 | +Replace the following placeholders with your actual Postgres database information: |
| 40 | +<DB_HOST>: Your Postgres database host |
| 41 | +<DB_NAME>: Your Postgres database name |
| 42 | +<DB_USER>: Your Postgres database username |
| 43 | +<DB_PASSWORD>: Your Postgres database password |
| 44 | +``` |
| 45 | +
|
| 46 | +### 4. Start the Monitoring Stack |
| 47 | +
|
| 48 | +```bash |
| 49 | +docker compose -f docker-compose.yaml up -d |
| 50 | +``` |
| 51 | + |
| 52 | +This starts: |
| 53 | +- Postgres Exporter that collects metrics from your Postgres database |
| 54 | +- OpenTelemetry Collector that receives metrics from Postgres Exporter and forwards them to Last9. |
| 55 | + |
| 56 | +### Understanding the Setup |
| 57 | + |
| 58 | +#### Postgres Exporter |
| 59 | + |
| 60 | +The Postgres Exporter connects to your Postgres database and exposes metrics in Prometheus format. It's configured to: |
| 61 | +- Connect to your database using the provided credentials |
| 62 | +- Use custom queries defined in queries.yaml |
| 63 | +- Expose metrics on port 9187 |
| 64 | + |
| 65 | +#### Custom Queries |
| 66 | + |
| 67 | +The queries.yaml file defines custom metrics to collect from Postgres. The example includes a slow_queries metric that: |
| 68 | +- Identifies queries running longer than 1 minute |
| 69 | +- Collects detailed information about these queries including: |
| 70 | + - Process ID |
| 71 | + - Database name |
| 72 | + - Username |
| 73 | + - Query text |
| 74 | + - Execution time |
| 75 | + - Wait events |
| 76 | + - Blocking processes |
| 77 | + |
| 78 | +#### OpenTelemetry Collector |
| 79 | + |
| 80 | +The OpenTelemetry Collector is configured to: |
| 81 | +- Scrape metrics from Postgres Exporter every 60 seconds |
| 82 | +- Add resource attributes like database name and environment |
| 83 | +- Process metrics in batches |
| 84 | +- Export metrics to Last9 using OTLP protocol |
| 85 | + |
| 86 | +### Verification |
| 87 | + |
| 88 | +Verify the containers are running: |
| 89 | + |
| 90 | +```bash |
| 91 | +docker ps |
| 92 | +``` |
| 93 | + |
| 94 | +Check Postgres Exporter metrics: |
| 95 | + |
| 96 | +```bash |
| 97 | +curl http://localhost:9187/metrics |
| 98 | +``` |
| 99 | + |
| 100 | +Check OpenTelemetry Collector logs: |
| 101 | + |
| 102 | +```bash |
| 103 | +docker logs otel-collector |
| 104 | +``` |
| 105 | + |
| 106 | +### Troubleshooting |
| 107 | + |
| 108 | +1. Container issues: |
| 109 | + |
| 110 | +```bash |
| 111 | +# Check container status |
| 112 | +docker ps -a |
| 113 | + |
| 114 | +# View container logs |
| 115 | +docker logs postgres-exporter |
| 116 | +docker logs otel-collector |
| 117 | +``` |
| 118 | + |
| 119 | +2. Connection issues: |
| 120 | + |
| 121 | +```bash |
| 122 | +docker logs postgres-exporter |
| 123 | +``` |
| 124 | +3. OpenTelemetry Collector issues: |
| 125 | + |
| 126 | +```bash |
| 127 | +# Check configuration |
| 128 | +docker exec otel-collector cat /etc/otel/collector/config.yaml |
| 129 | + |
| 130 | +# Restart collector |
| 131 | +docker compose restart otel-collector |
| 132 | +``` |
| 133 | + |
| 134 | +### Extending the Configuration |
| 135 | + |
| 136 | +#### Adding More Custom Queries |
| 137 | + |
| 138 | +You can extend queries.yaml to monitor additional aspects of your Postgres database: |
| 139 | +- Connection metrics |
| 140 | +- Table statistics |
| 141 | +- Index usage |
| 142 | +- Buffer cache hit ratio |
| 143 | +- Replication lag |
| 144 | + |
| 145 | +#### Monitoring Multiple Databases |
| 146 | + |
| 147 | +To monitor multiple Postgres databases: |
| 148 | +- Create separate instances of Postgres Exporter in your docker-compose.yaml |
| 149 | +- Configure each with different database credentials |
| 150 | +- Update the OpenTelemetry Collector configuration to scrape metrics from all exporters |
| 151 | + |
| 152 | +## Required Postgres Permissions |
| 153 | + |
| 154 | +The Postgres Exporter needs specific permissions to access system catalog tables and views, especially for the custom queries defined in `queries.yaml`. For the `slow_queries` query, which accesses `pg_stat_activity`, you need to create a dedicated monitoring user with appropriate permissions: |
| 155 | + |
| 156 | +```sql |
| 157 | +-- Create a dedicated user for monitoring |
| 158 | +CREATE USER postgres_exporter WITH PASSWORD 'your_secure_password'; |
| 159 | + |
| 160 | +-- Grant permissions required for monitoring |
| 161 | +GRANT pg_monitor TO postgres_exporter; |
| 162 | + |
| 163 | +-- If using PostgreSQL version earlier than 10, you'll need these specific grants instead: |
| 164 | +-- GRANT SELECT ON pg_stat_activity TO postgres_exporter; |
| 165 | +-- GRANT SELECT ON pg_stat_replication TO postgres_exporter; |
| 166 | +-- GRANT SELECT ON pg_stat_database TO postgres_exporter; |
| 167 | +``` |
| 168 | + |
| 169 | +When configuring the Postgres Exporter in your `docker-compose.yaml`, make sure to use this dedicated monitoring user: |
| 170 | + |
| 171 | +```yaml |
| 172 | +environment: |
| 173 | + - DATA_SOURCE_URI=<DB_HOST>/<DB_NAME> |
| 174 | + - DATA_SOURCE_USER=<DB_USER> |
| 175 | + - DATA_SOURCE_PASS=<DB_PASSWORD> |
| 176 | +``` |
| 177 | +
|
| 178 | +### Additional Permissions for Custom Metrics |
| 179 | +
|
| 180 | +If you add more custom queries to `queries.yaml` that access other system tables or views, you may need to grant additional permissions. For example: |
| 181 | + |
| 182 | +- For table statistics: `GRANT SELECT ON pg_statio_user_tables TO postgres_exporter;` |
| 183 | +- For index usage: `GRANT SELECT ON pg_stat_user_indexes TO postgres_exporter;` |
| 184 | +- For replication monitoring: `GRANT SELECT ON pg_stat_replication TO postgres_exporter;` |
| 185 | + |
| 186 | +Always follow the principle of least privilege by granting only the permissions necessary for monitoring purposes. |
0 commit comments