Skip to content

Commit 73415b2

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 72ce344 commit 73415b2

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

promgen/middleware.py

+47
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,53 @@ 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()}"
73+
)
74+
if request.body and request.headers["Content-Type"] == "application/json":
75+
logger.info(
76+
f"[Trace ID: {request.trace_id}] Request body: {json.loads(request.body)}"
77+
)
78+
except Exception as e:
79+
logger.exception(
80+
f"[Trace ID: {request.trace_id}] An error occurred when parsing request: {str(e)}"
81+
)
82+
5683
response = self.get_response(request)
5784

85+
# Log all responses to our v2 API endpoints
86+
if settings.ENABLE_API_LOGGING and request.path.startswith("/rest/v2/"):
87+
try:
88+
# Log the response details
89+
logger.info(
90+
f"[Trace ID: {request.trace_id}] Response status: {response.status_code}"
91+
)
92+
if (
93+
hasattr(response, "content")
94+
and response.headers["Content-Type"] == "application/json"
95+
and response.content
96+
):
97+
logger.info(
98+
f"[Trace ID: {request.trace_id}] Response body: {json.loads(response.content)}"
99+
)
100+
except Exception as e:
101+
logger.exception(
102+
f"[Trace ID: {request.trace_id}] An error occurred when parsing response: {str(e)}"
103+
)
104+
58105
triggers = {
59106
"Config": trigger_write_config.send,
60107
"Rules": trigger_write_rules.send,

promgen/settings.py

+2
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,5 @@
239239
"DESCRIPTION": "Non-GET APIs require authentication. "
240240
"Please login Promgen and access the Profile page to get a Token.",
241241
}
242+
243+
ENABLE_API_LOGGING = env.bool("ENABLE_API_LOGGING", default=False)

0 commit comments

Comments
 (0)