Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

confd: haproxy: optional log-format #687

Merged
merged 6 commits into from
Jul 8, 2024

Conversation

jimnydev
Copy link
Contributor

@jimnydev jimnydev commented Jul 3, 2024

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

@vitabaks
Copy link
Owner

vitabaks commented Jul 3, 2024

ok, thanks @jimnydev

@vitabaks
Copy link
Owner

vitabaks commented Jul 3, 2024

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 jimnydev force-pushed the haproxy-log-format branch from ade4765 to 666a9a0 Compare July 3, 2024 21:45
@jimnydev
Copy link
Contributor Author

jimnydev commented Jul 3, 2024

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
Copy link
Contributor

SDV109 commented Jul 8, 2024

@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
Copy link
Contributor Author

jimnydev commented Jul 8, 2024

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
Copy link
Contributor

SDV109 commented Jul 8, 2024

@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
Copy link
Owner

vitabaks commented Jul 8, 2024

Thanks @SDV109 @jimnydev

@vitabaks vitabaks merged commit c9a634f into vitabaks:master Jul 8, 2024
16 checks passed
@jimnydev jimnydev deleted the haproxy-log-format branch July 8, 2024 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants