UPDATE
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import json
|
||||
import os
|
||||
import gzip
|
||||
from datetime import datetime, timezone
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
|
||||
LOG_FILE = os.environ.get("OPA_REQUEST_LOG_FILE", "/logs/opa.log")
|
||||
PORT = int(os.environ.get("OPA_REQUEST_LOG_PORT", "8282"))
|
||||
|
||||
|
||||
class DecisionLogHandler(BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
content_length = int(self.headers.get("Content-Length", "0"))
|
||||
raw_payload = self.rfile.read(content_length) if content_length else b""
|
||||
content_encoding = self.headers.get("Content-Encoding", "").lower()
|
||||
|
||||
if content_encoding == "gzip" and raw_payload:
|
||||
raw_payload = gzip.decompress(raw_payload)
|
||||
|
||||
payload = raw_payload.decode("utf-8") if raw_payload else ""
|
||||
|
||||
entry = {
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"path": self.path,
|
||||
"headers": dict(self.headers.items()),
|
||||
"body": json.loads(payload) if payload else None,
|
||||
}
|
||||
|
||||
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
|
||||
with open(LOG_FILE, "a", encoding="utf-8") as log_file:
|
||||
log_file.write(json.dumps(entry, ensure_ascii=True) + "\n")
|
||||
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
self.wfile.write(b"ok")
|
||||
|
||||
def log_message(self, format, *args):
|
||||
return
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
server = ThreadingHTTPServer(("0.0.0.0", PORT), DecisionLogHandler)
|
||||
server.serve_forever()
|
||||
Reference in New Issue
Block a user