Skip to content

Commit 167752b

Browse files
feat: log ASN (#8309)
* feat: log ip_src_asnum in nginx * feat: log asn from gunicorn
1 parent b255883 commit 167752b

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

dev/build/gunicorn.conf.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,21 @@ def _describe_request(req):
6464
start and end of handling a request. E.g., do not include a timestamp.
6565
"""
6666
client_ip = "-"
67+
asn = "-"
6768
cf_ray = "-"
6869
for header, value in req.headers:
6970
header = header.lower()
7071
if header == "cf-connecting-ip":
7172
client_ip = value
73+
elif header == "x-ip-src-asnum":
74+
asn = value
7275
elif header == "cf-ray":
7376
cf_ray = value
7477
if req.query:
7578
path = f"{req.path}?{req.query}"
7679
else:
7780
path = req.path
78-
return f"{req.method} {path} (client_ip={client_ip}, cf_ray={cf_ray})"
81+
return f"{req.method} {path} (client_ip={client_ip}, asn={asn}, cf_ray={cf_ray})"
7982

8083

8184
def pre_request(worker, req):

ietf/utils/jsonlogger.py

+1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ def add_fields(self, log_record, record, message_dict):
3131
log_record.setdefault("cf_connecting_ip", record.args["{cf-connecting-ip}i"])
3232
log_record.setdefault("cf_connecting_ipv6", record.args["{cf-connecting-ipv6}i"])
3333
log_record.setdefault("cf_ray", record.args["{cf-ray}i"])
34+
log_record.setdefault("asn", record.args["{x-ip-src-asnum}i"])
3435
log_record.setdefault("is_authenticated", record.args["{x-datatracker-is-authenticated}o"])

k8s/nginx-logging.conf

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Define JSON log format - must be loaded before config that references it
1+
# Define JSON log format - must be loaded before config that references it.
2+
# Note that each line is fully enclosed in single quotes. Commas in arrays are
3+
# intentionally inside the single quotes.
24
log_format ietfjson escape=json
35
'{'
46
'"time":"$${keepempty}time_iso8601",'
@@ -16,5 +18,6 @@ log_format ietfjson escape=json
1618
'"x_forwarded_proto":"$${keepempty}http_x_forwarded_proto",'
1719
'"cf_connecting_ip":"$${keepempty}http_cf_connecting_ip",'
1820
'"cf_connecting_ipv6":"$${keepempty}http_cf_connecting_ipv6",'
19-
'"cf_ray":"$${keepempty}http_cf_ray"'
21+
'"cf_ray":"$${keepempty}http_cf_ray",'
22+
'"asn":"$${keepempty}http_x_ip_src_asnum"'
2023
'}';

0 commit comments

Comments
 (0)