Skip to content

Commit

Permalink
haproxy: Add optional log-format (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimnydev authored Jul 8, 2024
1 parent b10d907 commit c9a634f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
12 changes: 4 additions & 8 deletions roles/confd/templates/haproxy.tmpl.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ global
defaults
mode tcp
log global
option tcplog
{% if haproxy_log_format is defined %}
log-format '{{ haproxy_log_format }}'
{% endif %}
retries 2
timeout queue 5s
timeout connect 5s
Expand All @@ -32,7 +36,6 @@ listen master
bind {{ inventory_hostname }}:{{ haproxy_listen_port.master }}
{% endif %}
maxconn {{ haproxy_maxconn.master }}
option tcplog
option httpchk OPTIONS /primary
http-check expect status 200
default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions
Expand All @@ -53,7 +56,6 @@ listen master_direct
bind {{ inventory_hostname }}:{{ haproxy_listen_port.master_direct }}
{% endif %}
maxconn {{ haproxy_maxconn.master }}
option tcplog
option httpchk OPTIONS /primary
http-check expect status 200
default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions
Expand All @@ -68,7 +70,6 @@ listen replicas
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /replica?lag={{ patroni_maximum_lag_on_replica }}{{ '&' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand All @@ -94,7 +95,6 @@ listen replicas_direct
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas_direct }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /replica?lag={{ patroni_maximum_lag_on_replica }}{{ '&' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand All @@ -114,7 +114,6 @@ listen replicas_sync
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas_sync }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /sync{{ '?' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand All @@ -140,7 +139,6 @@ listen replicas_sync_direct
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas_sync_direct }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /sync{{ '?' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand All @@ -160,7 +158,6 @@ listen replicas_async
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas_async }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /async?lag={{ patroni_maximum_lag_on_replica }}{{ '&' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand All @@ -186,7 +183,6 @@ listen replicas_async_direct
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas_async_direct }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /async?lag={{ patroni_maximum_lag_on_replica }}{{ '&' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand Down
12 changes: 4 additions & 8 deletions roles/haproxy/templates/haproxy.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ global
defaults
mode tcp
log global
option tcplog
{% if haproxy_log_format is defined %}
log-format '{{ haproxy_log_format }}'
{% endif %}
retries 2
timeout queue 5s
timeout connect 5s
Expand All @@ -32,7 +36,6 @@ listen master
bind {{ inventory_hostname }}:{{ haproxy_listen_port.master }}
{% endif %}
maxconn {{ haproxy_maxconn.master }}
option tcplog
option httpchk OPTIONS /primary
http-check expect status 200
default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions
Expand All @@ -55,7 +58,6 @@ listen master_direct
bind {{ inventory_hostname }}:{{ haproxy_listen_port.master_direct }}
{% endif %}
maxconn {{ haproxy_maxconn.master }}
option tcplog
option httpchk OPTIONS /primary
http-check expect status 200
default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions
Expand All @@ -71,7 +73,6 @@ listen replicas
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /replica?lag={{ patroni_maximum_lag_on_replica }}{{ '&' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand Down Expand Up @@ -99,7 +100,6 @@ listen replicas_direct
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas_direct }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /replica?lag={{ patroni_maximum_lag_on_replica }}{{ '&' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand All @@ -120,7 +120,6 @@ listen replicas_sync
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas_sync }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /sync{{ '?' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand Down Expand Up @@ -148,7 +147,6 @@ listen replicas_sync_direct
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas_sync_direct }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /sync{{ '?' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand All @@ -169,7 +167,6 @@ listen replicas_async
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas_async }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /async?lag={{ patroni_maximum_lag_on_replica }}{{ '&' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand Down Expand Up @@ -197,7 +194,6 @@ listen replicas_async_direct
bind {{ inventory_hostname }}:{{ haproxy_listen_port.replicas_async_direct }}
{% endif %}
maxconn {{ haproxy_maxconn.replica }}
option tcplog
{% if balancer_tags | default('') | length > 0 %}
option httpchk OPTIONS /async?lag={{ patroni_maximum_lag_on_replica }}{{ '&' + balancer_tags.split(',') | map('trim') | map('regex_replace', '([^=]+)=(.*)', 'tag_\\1=\\2') | join('&') }}
{% else %}
Expand Down
29 changes: 29 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ haproxy_maxconn:
haproxy_timeout:
client: "60m"
server: "60m"
# Optionally declare log format for haproxy.
# Uncomment following lines (and remove extra space in front of variable definition) for JSON structured log format.
# haproxy_log_format: "{
# \"pid\":%pid,\
# \"haproxy_frontend_type\":\"tcp\",\
# \"haproxy_process_concurrent_connections\":%ac,\
# \"haproxy_frontend_concurrent_connections\":%fc,\
# \"haproxy_backend_concurrent_connections\":%bc,\
# \"haproxy_server_concurrent_connections\":%sc,\
# \"haproxy_backend_queue\":%bq,\
# \"haproxy_server_queue\":%sq,\
# \"haproxy_queue_wait_time\":%Tw,\
# \"haproxy_server_wait_time\":%Tc,\
# \"response_time\":%Td,\
# \"session_duration\":%Tt,\
# \"request_termination_state\":\"%tsc\",\
# \"haproxy_server_connection_retries\":%rc,\
# \"remote_addr\":\"%ci\",\
# \"remote_port\":%cp,\
# \"frontend_addr\":\"%fi\",\
# \"frontend_port\":%fp,\
# \"frontend_ssl_version\":\"%sslv\",\
# \"frontend_ssl_ciphers\":\"%sslc\",\
# \"haproxy_frontend_name\":\"%f\",\
# \"haproxy_backend_name\":\"%b\",\
# \"haproxy_server_name\":\"%s\",\
# \"response_size\":%B,\
# \"request_size\":%U\
# }"

# keepalived (if 'cluster_vip' is specified and 'with_haproxy_load_balancing' is 'true')
keepalived_virtual_router_id: "{{ cluster_vip.split('.')[3] | int }}" # The last octet of 'cluster_vip' IP address is used by default.
Expand Down

0 comments on commit c9a634f

Please sign in to comment.