Skip to content

Commit 49b15c4

Browse files
committed
add user information
1 parent f7a12a2 commit 49b15c4

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

api/src/middleware/request_context_middleware.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
import time
32
from starlette.types import ASGIApp, Receive, Scope, Send
43

@@ -63,13 +62,17 @@ def log_api_access(
6362
"""
6463
latency = time.time() - start_time
6564
request = self.create_http_request(scope, request_context, status_code, content_length, latency)
66-
headers = {
67-
k.decode().lower(): v.decode()
68-
for k, v in scope.get("headers", [])
65+
headers = {k.decode().lower(): v.decode() for k, v in scope.get("headers", [])}
66+
headers_to_log = {
67+
k: headers.get(k, "")
68+
for k in [
69+
"origin",
70+
"referer",
71+
]
72+
if headers.get(k)
6973
}
7074
self.logger.info(
71-
{ "user_id": request_context.user_id if request_context.user_id else "NO_AUTH",
72-
"headers": headers },
75+
{"user_id": request_context.user_id if request_context.user_id else "", "headers": headers_to_log},
7376
extra={
7477
"context": {
7578
"http_request": request,

0 commit comments

Comments
 (0)