vitabaks / postgresql_cluster

PostgreSQL High-Availability Cluster (based on Patroni). Automating with Ansible.
https://postgresql-cluster.org
MIT License
1.48k stars 394 forks source link

confd: haproxy: optional log-format #687

Closed jimnydev closed 2 months ago

jimnydev commented 2 months ago

New optional var for haproxy log-format. Supplied with an example of JSON structured log format (commented out by default).

vitabaks commented 2 months ago

ok, thanks @jimnydev

vitabaks commented 2 months ago

The log-format option can be specified once globally to apply to all listeners if it is used in the defaults section.

We don't need to duplicate the log-format in each listen section. This will simplify the configuration and reduce the amount of repetitive code.

@jimnydev Please check it out.

jimnydev commented 2 months ago

Sure, I initially tried to implement this with least changes to existing setup.

Defining log-format within defaults works, but only if we also deduplicate option tcplog as well. I've reset the branch and force-pushed suggested implementation.

Thanks!

SDV109 commented 2 months ago

@jimnydev, Hi, I tested your branch with the proposed solution. There is one critical error in the semantics of commented-out strings with log output in JSON format, which is why it is impossible to run any playbook, namely at the variable definition stage:

# haproxy_log_format: "{

The error will be as follows:

FAILED! => {"ansible_facts": {}, "ansible_included_var_files": [], "changed": false, "message": "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: Expecting value: line 1 column 1 (char 0)\n\nSyntax Error while loading YAML.\n  did not find expected key\n\nThe error appears to be in '/root/postgresql_cluster/vars/main.yml': line 50, column 2, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# Uncomment following lines for JSON structured log format.\n haproxy_log_format: \"{\n ^ here\nThis one looks easy to fix. It seems that there is a value started\nwith a quote, and the YAML parser is expecting to see the line ended\nwith the same kind of quote. For instance:\n\n    when: \"ok\" in result.stdout\n\nCould be written as:\n\n   when: '\"ok\" in result.stdout'\n\nOr equivalently:\n\n   when: \"'ok' in result.stdout\"\n"}

The error lies in the fact that if you remove the comment, there will be a space before defining the variable, which is an error for the YAML format, not many novice ansible users will be able to understand why they have an error, please correct it.

If you fix the semantics error, in this case the playbook works, everything is output to the log as needed in JSON format:

Jul  8 12:32:41 localhost haproxy[93368]: { "pid":93368,"haproxy_frontend_type":"tcp","haproxy_process_concurrent_connections":1,"haproxy_frontend_concurrent_connections":1,"haproxy_backend_concurrent_connections":0,"haproxy_server_concurrent_connections":0,"haproxy_backend_queue":0,"haproxy_server_queue":0,"haproxy_queue_wait_time":1,"haproxy_server_wait_time":0,"response_time":36812,"session_duration":36812,"request_termination_state":"----","haproxy_server_connection_retries":0,"remote_addr":"192.168.62.189","remote_port":46318,"frontend_addr":"192.168.62.205","frontend_port":5000,"frontend_ssl_version":"-","frontend_ssl_ciphers":"-","haproxy_frontend_name":"master","haproxy_backend_name":"master","haproxy_server_name":"pgnode001","response_size":593,"request_size":261}
jimnydev commented 2 months ago

Of course, pushed an update for that. I used my IDE to (un)comment out the block - and it automatically updates identation for yaml as well - I guess that's why I missed it. Thank you!

EDIT: now yaml lint is failing :)

SDV109 commented 2 months ago

@jimnydev Oh, this is very strange and sad, because if users do the simplest thing, just uncomment this block, they will get an error. @vitabaks FYI

vitabaks commented 2 months ago

Thanks @SDV109 @jimnydev