Home > Mobile >  Adding a new log attribute in nginx logs after modifying an existing one
Adding a new log attribute in nginx logs after modifying an existing one

Time:10-02

I have an Ingress-NGINX Controller chart for Kubernetes having its log-format-upstream configured as:

disable-access-log: "false" 
log-format-upstream: '{"time": "$time_iso8601", "remote_addr": "$proxy_protocol_addr", "x_forwarded_for": "$proxy_add_x_forwarded_for", "request_id": "$req_id",
  "remote_user": "$remote_user", "bytes_sent": $bytes_sent, "request_time": $request_time, "status": $status, "vhost": "$host", "request_proto": "$server_protocol",
  "path": "$uri", "request_query": "$args", "request_length": $request_length, "duration": $request_time,"method": "$request_method", "http_referrer": "$http_referer",
  "http_user_agent": "$http_user_agent" }'
ingressClass: nginx 

Assuming, that the path of the request exists in fields like $http_referrer or $uri such as /questions/12345678/details.

What I want is to include another log attribute say uri_category which contains uri paths but with any dynamic numbers such as 12345678 replaced with a static text such as USER_ID irrespective where this variable information appears in the path.

So in above example I would want the new attribute uri_category to have a value of /questions/USER_ID/details.

Is it possible to achieve this using log formatter and if yes how?

CodePudding user response:

You can use map to create the uri_category variable which can then be passed to log-format-upstream.

map $uri $uri_category {
    ~(questions/)([0-9] )(/details)$ $1USER_ID$3;
}
log-format-upstream: '{"time": "$time_iso8601", "remote_addr": "$proxy_protocol_addr", "x_forwarded_for": "$proxy_add_x_forwarded_for", "request_id": "$req_id",
  "remote_user": "$remote_user", "bytes_sent": $bytes_sent, "request_time": $request_time, "status": $status, "vhost": "$host", "request_proto": "$server_protocol",
  "path": "$uri", "request_query": "$args", "request_length": $request_length, "duration": $request_time,"method": "$request_method", "http_referrer": "$http_referer",
  "http_user_agent": "$http_user_agent", "uri_category": "$uri_category" }'
  • Related