add logging for prow

This commit is contained in:
2025-05-13 21:08:05 +08:00
parent 539afdd1b2
commit 99625d4dbb
2 changed files with 67 additions and 0 deletions
+1
View File
@@ -3,6 +3,7 @@ server {
listen 443 ssl; listen 443 ssl;
listen 80; listen 80;
client_max_body_size 10M; client_max_body_size 10M;
access_by_lua_file /usr/local/openresty/nginx/scripts/log_to_webhook.lua;
# disable in local test env # disable in local test env
ssl_certificate /etc/letsencrypt/live/moicen.com/fullchain.pem; # managed by Certbot ssl_certificate /etc/letsencrypt/live/moicen.com/fullchain.pem; # managed by Certbot
+66
View File
@@ -0,0 +1,66 @@
-- cd /tmp
-- wget https://luarocks.org/releases/luarocks-3.11.1.tar.gz
-- tar zxpf luarocks-3.11.1.tar.gz
-- cd luarocks-3.11.1
-- ./configure --with-lua=/usr/local/openresty/luajit
-- make
-- make install
--
-- /usr/local/bin/luarocks install lua-resty-http
--
-- /usr/local/bin/luarocks list
package.path = package.path .. ';/usr/local/openresty/nginx/scripts/?.lua';
local cjson = require "cjson"
-- Collect request details
local request_data = {
method = ngx.var.request_method,
uri = ngx.var.request_uri,
headers = ngx.req.get_headers(),
body = nil,
remote_addr = ngx.var.remote_addr,
timestamp = ngx.now(),
protocol = ngx.var.server_protocol,
query_string = ngx.var.query_string or "",
request_length = ngx.var.request_length
}
-- Capture request body (if any)
ngx.req.read_body()
local body = ngx.req.get_body_data()
if body then
request_data.body = body
elseif ngx.req.get_body_file() then
local file = ngx.req.get_body_file()
local f, err = io.open(file, "r")
if f then
request_data.body = f:read("*all")
f:close()
else
ngx.log(ngx.ERR, "Failed to read body file: ", err)
request_data.body_error = err
end
end
-- Remove sensitive headers
request_data.headers.Authorization = nil
request_data.headers.HtySudoerToken = nil
-- Encode as JSON
local json_payload, encode_err = cjson.encode(request_data)
if not json_payload then
ngx.log(ngx.ERR, "Failed to encode JSON: ", encode_err)
return
end
-- Write to log file
local log_file_path = "/tmp/prow_requests.log"
local file, err = io.open(log_file_path, "a")
if file then
file:write(json_payload .. "\n")
file:close()
else
ngx.log(ngx.ERR, "Failed to write to log file: ", err)
end