Skip to content

Commit 9bc95f6

Browse files
committed
fix: KeyError: 'request' in line 125
1 parent d873668 commit 9bc95f6

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

fastapi_elasticsearch_middleware/elasticsearch_middleware.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
5252

5353
async def intercept_send(response):
5454
nonlocal log_data
55-
if response['type'] == 'http.response.body' and "response" not in log_data.keys(): # Streaming response, we don't want to log this
55+
if response['type'] == 'http.response.body' and not log_data.get("response"): # Streaming response, we don't want to log this
5656
await send(response)
5757
return
58-
if response['type'] == 'http.response.body' and "response" in log_data.keys(): # Response part
58+
if response['type'] == 'http.response.body' and log_data.get("response"): # Response part
5959

6060
# Finishes telemetry
6161
end_time = time.time()
@@ -134,6 +134,7 @@ async def intercept_receive():
134134
def log_to_elasticsearch(self, log_data) -> None:
135135
try:
136136
self.elasticsearch_client.index(index=self.index, body=log_data)
137+
log_data["response"] = {}
137138
except Exception as e:
138139
logging.error(f"Failed to log to Elasticsearch: {str(e)}")
139140

0 commit comments

Comments
 (0)