feat: log ASN (#8309)
* feat: log ip_src_asnum in nginx * feat: log asn from gunicorn
This commit is contained in:
parent
b25588397d
commit
167752ba76
|
@ -64,18 +64,21 @@ def _describe_request(req):
|
||||||
start and end of handling a request. E.g., do not include a timestamp.
|
start and end of handling a request. E.g., do not include a timestamp.
|
||||||
"""
|
"""
|
||||||
client_ip = "-"
|
client_ip = "-"
|
||||||
|
asn = "-"
|
||||||
cf_ray = "-"
|
cf_ray = "-"
|
||||||
for header, value in req.headers:
|
for header, value in req.headers:
|
||||||
header = header.lower()
|
header = header.lower()
|
||||||
if header == "cf-connecting-ip":
|
if header == "cf-connecting-ip":
|
||||||
client_ip = value
|
client_ip = value
|
||||||
|
elif header == "x-ip-src-asnum":
|
||||||
|
asn = value
|
||||||
elif header == "cf-ray":
|
elif header == "cf-ray":
|
||||||
cf_ray = value
|
cf_ray = value
|
||||||
if req.query:
|
if req.query:
|
||||||
path = f"{req.path}?{req.query}"
|
path = f"{req.path}?{req.query}"
|
||||||
else:
|
else:
|
||||||
path = req.path
|
path = req.path
|
||||||
return f"{req.method} {path} (client_ip={client_ip}, cf_ray={cf_ray})"
|
return f"{req.method} {path} (client_ip={client_ip}, asn={asn}, cf_ray={cf_ray})"
|
||||||
|
|
||||||
|
|
||||||
def pre_request(worker, req):
|
def pre_request(worker, req):
|
||||||
|
|
|
@ -31,4 +31,5 @@ class GunicornRequestJsonFormatter(DatatrackerJsonFormatter):
|
||||||
log_record.setdefault("cf_connecting_ip", record.args["{cf-connecting-ip}i"])
|
log_record.setdefault("cf_connecting_ip", record.args["{cf-connecting-ip}i"])
|
||||||
log_record.setdefault("cf_connecting_ipv6", record.args["{cf-connecting-ipv6}i"])
|
log_record.setdefault("cf_connecting_ipv6", record.args["{cf-connecting-ipv6}i"])
|
||||||
log_record.setdefault("cf_ray", record.args["{cf-ray}i"])
|
log_record.setdefault("cf_ray", record.args["{cf-ray}i"])
|
||||||
|
log_record.setdefault("asn", record.args["{x-ip-src-asnum}i"])
|
||||||
log_record.setdefault("is_authenticated", record.args["{x-datatracker-is-authenticated}o"])
|
log_record.setdefault("is_authenticated", record.args["{x-datatracker-is-authenticated}o"])
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
# Define JSON log format - must be loaded before config that references it
|
# Define JSON log format - must be loaded before config that references it.
|
||||||
|
# Note that each line is fully enclosed in single quotes. Commas in arrays are
|
||||||
|
# intentionally inside the single quotes.
|
||||||
log_format ietfjson escape=json
|
log_format ietfjson escape=json
|
||||||
'{'
|
'{'
|
||||||
'"time":"$${keepempty}time_iso8601",'
|
'"time":"$${keepempty}time_iso8601",'
|
||||||
|
@ -16,5 +18,6 @@ log_format ietfjson escape=json
|
||||||
'"x_forwarded_proto":"$${keepempty}http_x_forwarded_proto",'
|
'"x_forwarded_proto":"$${keepempty}http_x_forwarded_proto",'
|
||||||
'"cf_connecting_ip":"$${keepempty}http_cf_connecting_ip",'
|
'"cf_connecting_ip":"$${keepempty}http_cf_connecting_ip",'
|
||||||
'"cf_connecting_ipv6":"$${keepempty}http_cf_connecting_ipv6",'
|
'"cf_connecting_ipv6":"$${keepempty}http_cf_connecting_ipv6",'
|
||||||
'"cf_ray":"$${keepempty}http_cf_ray"'
|
'"cf_ray":"$${keepempty}http_cf_ray",'
|
||||||
|
'"asn":"$${keepempty}http_x_ip_src_asnum"'
|
||||||
'}';
|
'}';
|
||||||
|
|
Loading…
Reference in a new issue