Welcome! Log In Create A New Profile

Advanced

Print header lua script

Posted by Aleksandar Lazic 
Aleksandar Lazic
Print header lua script
January 10, 2018 11:40PM
Hi.

I have the need to print the request headers which reaches the haproxy.

Today I was sitting down and started to write this small script.

Due to the fact that this is my first lua & haproxy script I'm sure it's
not the most efficient version ;-)

I appreciate any feedback ;-)

### print_headers.lua
core.register_action("print-headers",{ "http-req" },
function(transaction)
--[[
transaction is of class TXN.
TXN contains contains a property 'http' which is an instance
of HAProxy HTTP class
]]

local hdr = transaction.http:req_get_headers()

for key,value in pairs(hdr) do
local mystr = key
mystr = mystr .. ": "
for _ ,val2 in pairs(value) do
mystr = mystr .. val2
end
core.Info(mystr)
end

end)

###

### haproxy.cfg
global
log /dev/log local1 debug
lua-load /etc/haproxy/print_headers.lua

defaults
log global
mode http

listen proxy001
....
http-request lua.print-headers
....
###

One issue is that I have every entry twice in the log, I assume this
could be because I defined the loglevel "debug" and the 'core.Info' is
in info level

###
Jan 10 23:22:58 app001 haproxy[26681]: accept-encoding: deflate, gzip
Jan 10 23:22:58 app001 haproxy[26681]: accept-encoding: deflate, gzip
Jan 10 23:22:58 app001 haproxy[26681]: content-type: application/json
Jan 10 23:22:58 app001 haproxy[26681]: content-type: application/json
Jan 10 23:22:58 app001 haproxy[26681]: user-agent: curl/7.47.0
Jan 10 23:22:58 app001 haproxy[26681]: user-agent: curl/7.47.0
Jan 10 23:22:58 app001 haproxy[26681]: x-request-id: 3
Jan 10 23:22:58 app001 haproxy[26681]: x-request-id: 3
Jan 10 23:22:58 app001 haproxy[26681]: content-length: 63
Jan 10 23:22:58 app001 haproxy[26681]: content-length: 63
Jan 10 23:22:58 app001 haproxy[26681]: host: MY_HOST:1234
Jan 10 23:22:58 app001 haproxy[26681]: host: MY_HOST:1234
Jan 10 23:22:58 app001 haproxy[26681]: accept: */*
Jan 10 23:22:58 app001 haproxy[26681]: accept: */*
###

Thanks for feedback.

aleks
Aleksandar Lazic
Re: Print header lua script
January 11, 2018 01:40PM
Hi.

I have written a blog post how to use this lua scirpt in a dockerized
haproxy.

https://www.me2digital.com/blog/2018/01/show-headers-in-haproxy/

I appreciate any feedback ;-)

Best regards
Aleks

------ Originalnachricht ------
Von: "Aleksandar Lazic" <[email protected]>
An: haproxy@formilux.org
Gesendet: 10.01.2018 23:33:01
Betreff: Print header lua script

>Hi.
>
>I have the need to print the request headers which reaches the haproxy.
>
>Today I was sitting down and started to write this small script.
>
>Due to the fact that this is my first lua & haproxy script I'm sure
>it's not the most efficient version ;-)
>
>I appreciate any feedback ;-)
>
>### print_headers.lua
>core.register_action("print-headers",{ "http-req" },
>function(transaction)
> --[[
> transaction is of class TXN.
> TXN contains contains a property 'http' which is an instance
> of HAProxy HTTP class
> ]]
>
> local hdr = transaction.http:req_get_headers()
>
> for key,value in pairs(hdr) do
> local mystr = key
> mystr = mystr .. ": "
> for _ ,val2 in pairs(value) do
> mystr = mystr .. val2
> end
> core.Info(mystr)
> end
>
>end)
>
>###
>
>### haproxy.cfg
>global
> log /dev/log local1 debug
> lua-load /etc/haproxy/print_headers.lua
>
>defaults
> log global
> mode http
>
>listen proxy001
> ....
> http-request lua.print-headers
> ....
>###
>
>One issue is that I have every entry twice in the log, I assume this
>could be because I defined the loglevel "debug" and the 'core.Info' is
>in info level
>
>###
>Jan 10 23:22:58 app001 haproxy[26681]: accept-encoding: deflate, gzip
>Jan 10 23:22:58 app001 haproxy[26681]: accept-encoding: deflate, gzip
>Jan 10 23:22:58 app001 haproxy[26681]: content-type: application/json
>Jan 10 23:22:58 app001 haproxy[26681]: content-type: application/json
>Jan 10 23:22:58 app001 haproxy[26681]: user-agent: curl/7.47.0
>Jan 10 23:22:58 app001 haproxy[26681]: user-agent: curl/7.47.0
>Jan 10 23:22:58 app001 haproxy[26681]: x-request-id: 3
>Jan 10 23:22:58 app001 haproxy[26681]: x-request-id: 3
>Jan 10 23:22:58 app001 haproxy[26681]: content-length: 63
>Jan 10 23:22:58 app001 haproxy[26681]: content-length: 63
>Jan 10 23:22:58 app001 haproxy[26681]: host: MY_HOST:1234
>Jan 10 23:22:58 app001 haproxy[26681]: host: MY_HOST:1234
>Jan 10 23:22:58 app001 haproxy[26681]: accept: */*
>Jan 10 23:22:58 app001 haproxy[26681]: accept: */*
>###
>
>Thanks for feedback.
>
>aleks
>
>
Sorry, only registered users may post in this forum.

Click here to login