Skip to content

Commit

Permalink
refactored timestamp and thread_id
Browse files Browse the repository at this point in the history
  • Loading branch information
DinisCruz committed Sep 21, 2024
1 parent a6db710 commit 1fab160
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
38 changes: 19 additions & 19 deletions osbot_fast_api/api/Fast_API__Http_Event.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import logging
import time
from decimal import Decimal
from fastapi import Response, Request

from osbot_fast_api.api.Fast_API__Http_Event__Info import Fast_API__Http_Event__Info
from osbot_fast_api.api.Fast_API__Http_Event__Request import Fast_API__Http_Event__Request
from osbot_fast_api.api.Fast_API__Http_Event__Response import Fast_API__Http_Event__Response
from osbot_fast_api.api.Fast_API__Http_Event__Traces import Fast_API__Http_Event__Traces
from osbot_utils.base_classes.Type_Safe import Type_Safe
from osbot_utils.helpers.Random_Guid import Random_Guid
from osbot_utils.helpers.trace.Trace_Call import Trace_Call
from osbot_utils.utils.Misc import timestamp_utc_now, current_thread_id, str_to_bytes
from osbot_utils.utils.Objects import pickle_to_bytes
from decimal import Decimal
from fastapi import Response, Request
from osbot_fast_api.api.Fast_API__Http_Event__Info import Fast_API__Http_Event__Info
from osbot_fast_api.api.Fast_API__Http_Event__Request import Fast_API__Http_Event__Request
from osbot_fast_api.api.Fast_API__Http_Event__Response import Fast_API__Http_Event__Response
from osbot_fast_api.api.Fast_API__Http_Event__Traces import Fast_API__Http_Event__Traces
from osbot_utils.base_classes.Type_Safe import Type_Safe
from osbot_utils.helpers.Random_Guid import Random_Guid
from osbot_utils.helpers.trace.Trace_Call import Trace_Call
from osbot_utils.utils.Misc import timestamp_utc_now, current_thread_id, str_to_bytes
from osbot_utils.utils.Objects import pickle_to_bytes

HEADER_NAME__FAST_API_REQUEST_ID = 'fast-api-request-id'
HEADER_NAME__CACHE_CONTROL = "cache-control"
Expand All @@ -28,11 +27,9 @@ class Fast_API__Http_Event(Type_Safe):
http_event_response : Fast_API__Http_Event__Response
http_event_traces : Fast_API__Http_Event__Traces
request_id : Random_Guid # todo: rename to http_event_id
timestamp : int
thread_id : int

def add_log_message(self, message_text, level:int = logging.INFO):
timestamp_delta = timestamp_utc_now() - self.timestamp
timestamp_delta = timestamp_utc_now() - self.http_event_info.timestamp
message = dict( level = level ,
text = message_text ,
timestamp = timestamp_delta)
Expand All @@ -41,8 +38,8 @@ def add_log_message(self, message_text, level:int = logging.INFO):
def add_traces(self, trace_call: Trace_Call):
view_model = trace_call.view_data()
view_model_bytes = pickle_to_bytes(view_model)
self.traces.append(view_model_bytes)
self.traces_count += len(view_model)
self.http_event_traces.traces.append(view_model_bytes)
self.http_event_traces.traces_count += len(view_model)
self.add_log_message(f"added {len(view_model)} traces")

def messages(self):
Expand All @@ -53,24 +50,27 @@ def messages(self):
return messages

def on_request(self, request: Request):
self.timestamp = timestamp_utc_now()

# http_event_request
self.http_event_request.headers = dict(request.headers)
self.http_event_request.host_name = request.url.hostname
self.http_event_request.method = request.method
self.http_event_request.path = request.url.path
self.http_event_request.port = request.url.port
self.http_event_request.start_time = Decimal(time.time())

# http_event_info
self.http_event_info.domain = request.headers.get('cloudfront-domain' )
self.http_event_info.client_country = request.headers.get('cloudfront-viewer-country')
self.http_event_info.client_city = request.headers.get('cloudfront-viewer-city' )
self.http_event_info.client_ip = request.client.host
self.http_event_info.timestamp = timestamp_utc_now()
self.http_event_info.thread_id = current_thread_id()

self.thread_id = current_thread_id()
self.set_request_headers(request)

def on_response(self, response: Response):
# http_event_response
self.http_event_response.end_time = Decimal(time.time())
self.http_event_request.duration = self.http_event_response.end_time - self.http_event_request.start_time # todo: move this normalisation logic into the Http_Event_* classes
self.http_event_request.start_time = self.http_event_request.start_time .quantize(Decimal('0.001')) # make sure these duration objects doing have move that 3 decimal points
Expand Down
2 changes: 2 additions & 0 deletions osbot_fast_api/api/Fast_API__Http_Event__Info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ class Fast_API__Http_Event__Info(Type_Safe):
client_country : str = None
client_ip : str = None
domain : str = None
timestamp : int
thread_id : int
37 changes: 18 additions & 19 deletions tests/unit/api/test_Fast_API__Http_Events.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ def test_on_http_request(self):
assert _.requests_data[self.request_id] == self.request_data

#message_timestamp = self.request_data.log_messages[0].get('timestamp')
expected_data = { 'http_event_info' : { 'client_city' : None ,
'client_country' : None ,
'client_ip' : 'pytest' ,
'domain' : None ,
'fast_api_name' : '' ,
'log_messages' : [] },
expected_data = { 'http_event_info' : { 'client_city' : None ,
'client_country' : None ,
'client_ip' : 'pytest' ,
'domain' : None ,
'fast_api_name' : '' ,
'log_messages' : [] ,
'thread_id' : self.request_data.http_event_info.thread_id ,
'timestamp' : self.request_data.http_event_info.timestamp },
'http_event_request' : { 'duration' : None ,
'headers' : {} ,
'host_name' : None ,
Expand All @@ -82,10 +84,7 @@ def test_on_http_request(self):
'status_code' : None },
'http_event_traces' : { 'traces' : [] ,
'traces_count' : 0 },
'request_id' : self.request_id ,
'thread_id' : self.request_data.thread_id ,
'timestamp' : self.request_data.timestamp ,
}
'request_id' : self.request_id }
assert self.request_data.json() == expected_data

def test_on_http_response(self):
Expand All @@ -103,12 +102,14 @@ def test_on_http_response(self):



expected_data = { 'http_event_info' : { 'client_city' : None ,
'client_country' : None ,
'client_ip' : 'pytest' ,
'domain' : None ,
'fast_api_name' : '' ,
'log_messages' : [] },
expected_data = { 'http_event_info' : { 'client_city' : None ,
'client_country' : None ,
'client_ip' : 'pytest' ,
'domain' : None ,
'fast_api_name' : '' ,
'thread_id' : self.request_data.http_event_info.thread_id ,
'timestamp' : self.request_data.http_event_info.timestamp ,
'log_messages' : [] },
'http_event_request' : { 'duration' : Decimal('0.001') ,
'host_name' : None ,
'headers' : {} ,
Expand All @@ -123,9 +124,7 @@ def test_on_http_response(self):
'end_time' : self.request_data.http_event_response.end_time ,
'status_code' : 200 },
'http_event_traces' : { 'traces' : [] ,
'traces_count' : 0 },
'thread_id' : self.request_data.thread_id ,
'timestamp' : self.request_data.timestamp }
'traces_count' : 0 }}

assert self.request_data.http_event_request.duration == Decimal(0.001).quantize(Decimal('0.001'))
assert self.request_data.json() == expected_data
Expand Down
19 changes: 9 additions & 10 deletions tests/unit/utils/testing/test_Mock_Obj__Fast_API__Request_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ def test_create(self):
assert type(_) is Fast_API__Http_Event
res_content_type = self.mock_request_data.content_type
res_content_length = str(len(self.mock_request_data.res_content))
expected_data = {'http_event_info' : { 'client_city' : self.mock_request_data.city ,
'client_country': self.mock_request_data.country ,
'client_ip' : 'pytest' ,
'domain' : self.mock_request_data.domain ,
'fast_api_name' : self.mock_request_data.fast_api_name ,
'log_messages' : [] },
expected_data = {'http_event_info' : { 'client_city' : self.mock_request_data.city ,
'client_country': self.mock_request_data.country ,
'client_ip' : 'pytest' ,
'domain' : self.mock_request_data.domain ,
'fast_api_name' : self.mock_request_data.fast_api_name ,
'log_messages' : [] ,
'thread_id' : _.http_event_info.thread_id ,
'timestamp' : _.http_event_info.timestamp },
'http_event_request' : { 'duration' : Decimal('0.000') ,
'headers' : self.mock_request_data.req_headers_data ,
'host_name' : self.mock_request_data.hostname ,
Expand All @@ -65,10 +67,7 @@ def test_create(self):
'fast-api-request-id': _.request_id },
'status_code' : self.mock_request_data.res_status_code },
'http_event_traces' : { 'traces' : [] ,
'traces_count' : 0 },
'thread_id' : _.thread_id ,
'timestamp' : _.timestamp ,
}
'traces_count' : 0 }}
assert request_data.json() == expected_data


Expand Down

0 comments on commit 1fab160

Please sign in to comment.