如何在 haproxy 日志中捕获唯一 ID?

如何在 haproxy 日志中捕获唯一 ID?

我正在尝试捕获日志中每个请求的唯一 ID。我以为我可以capture request header X-Unique-ID len 16在标头上使用,但它不起作用。有什么建议吗?

global
log /dev/log local1
maxconn 4096
user haproxy
group haproxy
daemon
stats socket /var/run/haproxy.sock mode 600 level admin
spread-checks 5

defaults
log    global
mode    http
option    httplog
option  dontlog-normal
option log-separate-errors
option    dontlognull
retries    2
option redispatch
maxconn 2000
timeout connect 5s
timeout client  15s
timeout server  10s
#option forwardfor
#option http-server-close


frontend http-in
bind *:8080
monitor-uri /haproxy-health
unique-id-header X-Unique-ID
unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%Ts_%rt:%pid

capture request header Host len 32
capture request header User-Agent len 200
capture request header X-Unique-ID len 16
rspidel ^X-Powered-By:.*

default_backend application-backend

backend application-backend
balance leastconn
option httpclose
option forwardfor
option httpchk

http-check send-state
http-send-name-header Node-Name
#http-check expect status 200
#cookie JSESSIONID prefix

server test1 192.168.1.73:80
#server 10.0.1.102:80 cookie A check

答案1

要记录唯一 ID,您需要创建使用 %ID 标签的自定义日志格式。无需使用“捕获请求标头”来记录该格式。

使用自定义日志格式重写上面的示例:

frontend http-in
    bind *:8080
    monitor-uri /haproxy-health
    unique-id-header X-Unique-ID
    unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%Ts_%rt:%pid

    capture request header Host len 32
    capture request header User-Agent len 200
    rspidel ^X-Powered-By:.*
    log-format %{+Q}r\ %ST\ "%CC"\ "%hr"\ "%CS"\ "%hs"\ %ID

通过这样的配置,您将获得类似这样的日志消息:

Sep 10 17:37:55 lb2 haproxy[28497]: "GET /favicon.ico HTTP/1.1" 200 "-" "{172.20.0.20:8999|Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36}" "-" "" AC140001:C1B8_AC140014:2327_55F1BFF3_0003:6F51

最后一个条目是唯一的 ID。

您可以在以下位置查看所有登录的可能性http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#8.2.4

相关内容