Skip to content

Commit 5f16929

Browse files
committed
Add logging to v2 APIs
We have added middleware logging for request/response to API calls via v2 APIs. The logging information includes the IP address, user (if authenticated), request details, and response details. Enabling or disabling this logging can be configured through setting.ENABLE_API_LOGGING.
1 parent 6d722ce commit 5f16929

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

promgen/middleware.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
caching system to set a key and then triggering the actual event from middleware
1818
"""
1919

20+
import json
2021
import logging
22+
import uuid
2123
from http import HTTPStatus
2224
from threading import local
2325

@@ -53,8 +55,46 @@ def __call__(self, request):
5355
if request.user.is_authenticated:
5456
_user.value = request.user
5557

58+
# Log all requests to our v2 API endpoints
59+
if settings.ENABLE_API_LOGGING and request.path.startswith("/rest/v2/"):
60+
try:
61+
# Generate a trace ID for each request
62+
trace_id = str(uuid.uuid4())
63+
request.trace_id = trace_id
64+
# Log the IP address of the request
65+
ip_address = request.META.get("REMOTE_ADDR")
66+
logger.info(f"[Trace ID: {trace_id}] IP Address: {ip_address}")
67+
# Log the user if authenticated
68+
if request.user.is_authenticated:
69+
logger.info(f"[Trace ID: {trace_id}] User: {request.user.username}")
70+
# Log the request details
71+
logger.info(
72+
f"[Trace ID: {request.trace_id}] Request: {request.method} {request.get_full_path()} - body size: {len(request.body) if request.body else 0} bytes"
73+
)
74+
if request.body and request.headers["Content-Type"] == "application/json":
75+
# Only log first 512 characters of the request body to avoid flooding the logs
76+
logger.info(
77+
f"[Trace ID: {request.trace_id}] Request body: {json.dumps(json.loads(request.body))[:512]}"
78+
)
79+
except Exception as e:
80+
logger.exception(
81+
f"[Trace ID: {request.trace_id}] An error occurred when parsing request: {str(e)}"
82+
)
83+
5684
response = self.get_response(request)
5785

86+
# Log all responses to our v2 API endpoints
87+
if settings.ENABLE_API_LOGGING and request.path.startswith("/rest/v2/"):
88+
try:
89+
# Log the response details
90+
logger.info(
91+
f"[Trace ID: {request.trace_id}] Response status: {response.status_code} - content size: {len(response.content)} bytes"
92+
)
93+
except Exception as e:
94+
logger.exception(
95+
f"[Trace ID: {request.trace_id}] An error occurred when logging response: {str(e)}"
96+
)
97+
5898
triggers = {
5999
"Config": trigger_write_config.send,
60100
"Rules": trigger_write_rules.send,

promgen/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,5 @@
237237
"DESCRIPTION": "Non-GET APIs require authentication. "
238238
"Please login Promgen and access the Profile page to get a Token.",
239239
}
240+
241+
ENABLE_API_LOGGING = env.bool("ENABLE_API_LOGGING", default=False)

0 commit comments

Comments
 (0)