|
| 1 | +# DB Enrichment Project |
| 2 | + |
| 3 | +This project sets up a local environment for enriching logs with data from a PostgreSQL database using Logstash. The setup includes Docker containers for Logstash and PostgreSQL, along with the necessary configuration files and scripts. The purpose of this project is to test JDBC connection string parameters to avoid Logstash's bad behavior when the PostgreSQL table is locked or slow. |
| 4 | + |
| 5 | +The following sections provide detailed instructions on how to set up and use the provided Docker containers, as well as how to send logs for enrichment. |
| 6 | + |
| 7 | +```bash |
| 8 | +├── Makefile # Docker-compose shortcut (up|down) |
| 9 | +├── docker-compose.yaml # Docker-compose manifest file |
| 10 | +├── logstash |
| 11 | +│ ├── Dockerfile # Logstash Dockerfile |
| 12 | +│ ├── config |
| 13 | +│ │ ├── logstash.yml # Logstash configuration file |
| 14 | +│ │ └── pipelines.yml # Logstash pipelines configuration |
| 15 | +│ └── pipeline |
| 16 | +│ └── pipeline.cfg # Main Logstash pipeline configuration |
| 17 | +├── postgres |
| 18 | +│ └── init.sql # PostgreSQL initialization script |
| 19 | +└── send-log.sh # Script to send logs to Logstash |
| 20 | +``` |
| 21 | + |
| 22 | +# Set up the containers |
| 23 | + |
| 24 | +To start the containers, use the following command: |
| 25 | + |
| 26 | +```bash |
| 27 | +make up |
| 28 | +``` |
| 29 | + |
| 30 | +To stop the containers, use the following command: |
| 31 | + |
| 32 | +```bash |
| 33 | +make down |
| 34 | +``` |
| 35 | + |
| 36 | +# Send logs |
| 37 | + |
| 38 | +The script [send-log.sh](./send-log.sh) is used to send logs to Logstash through HTTP. There are two types of logs: enriched logs and non-enriched logs. |
| 39 | + |
| 40 | +To send logs that will be enriched from the database, use the following command: |
| 41 | + |
| 42 | +```shell |
| 43 | +./send-log.sh db |
| 44 | +``` |
| 45 | + |
| 46 | +To send logs that will not be enriched, use the following command: |
| 47 | + |
| 48 | +```shell |
| 49 | +./send-log.sh |
| 50 | +``` |
| 51 | + |
| 52 | +# JDBC String Connection Details |
| 53 | + |
| 54 | +More details about JDBC_CONNECTION_STRING parameters: |
| 55 | + |
| 56 | +- https://jdbc.postgresql.org/documentation/use/ |
| 57 | +- https://www.postgresql.org/docs/current/runtime-config-client.html |
| 58 | + |
| 59 | +JDBC connection string used in this stack: |
| 60 | + |
| 61 | +``` |
| 62 | +jdbc:postgresql://postgres:5432/db?ApplicationName=logstash&loginTimeout=10&socketTimeout=10&options=-c%20statement_timeout=5000%20-c%20lock_timeout=1000 |
| 63 | +``` |
| 64 | + |
| 65 | +- `ApplicationName=logstash`: Sets the application name to "logstash" for easier identification in PostgreSQL logs. |
| 66 | +- `loginTimeout=10`: Specifies the maximum time in seconds to wait for a connection to be established. If the connection cannot be established within this time, an error is thrown. |
| 67 | +- `socketTimeout=10`: Sets the maximum time in seconds for reading data from the database. If the database does not respond within this time, the connection is closed. |
| 68 | +- `options=-c statement_timeout=5000 -c lock_timeout=1000`: Passes additional configuration parameters to PostgreSQL: |
| 69 | + - `statement_timeout=5000`: Sets the maximum time in milliseconds that a query can run before being terminated. In this case, queries running longer than 5 seconds will be aborted. |
| 70 | + - `lock_timeout=1000`: Sets the maximum time in milliseconds to wait for a lock on a table. If the lock cannot be acquired within 1 second, an error is thrown. |
| 71 | + |
| 72 | +# SQL Command to Lock Table |
| 73 | + |
| 74 | +With auto-commit off, when you execute the command below, the table will be locked. When you commit, the table will be unlocked. |
| 75 | + |
| 76 | +```sql |
| 77 | +LOCK TABLE table_users IN ACCESS EXCLUSIVE MODE; |
| 78 | +``` |
| 79 | + |
| 80 | +# How to Simulate a Slow Query |
| 81 | + |
| 82 | +To test how Logstash handles slow queries, you can simulate a delay in the PostgreSQL response using the `pg_sleep` function. This function pauses the execution for a specified number of seconds. In the example below, the query will sleep for 7 seconds before returning the results. |
| 83 | + |
| 84 | +Use the following SQL command to simulate a slow query: |
| 85 | + |
| 86 | +```sql |
| 87 | +SELECT |
| 88 | + pg_sleep(7)::text, |
| 89 | + user_name, |
| 90 | + user_email, |
| 91 | + user_group |
| 92 | +FROM table_users |
| 93 | +WHERE id = ? |
| 94 | +``` |
| 95 | + |
| 96 | +In this query: |
| 97 | +- `pg_sleep(7)::text` introduces a 7-second delay. |
| 98 | +- `user_name`, `user_email`, and `user_group` are the columns being retrieved from the `table_users` table. |
| 99 | +- `id = ?` is a placeholder for the user ID you want to query. |
| 100 | + |
| 101 | +This setup helps you observe how Logstash behaves when the database query takes longer to execute. |
0 commit comments