Add the start/end times to the messages before sending them so that the

start time and end time reflect the correct time and both are not set
to 'now'.
This commit is contained in:
Josh Bonczkowski 2025-11-21 15:32:23 -05:00
parent 4f2c4832dd
commit 0713c023f2

View file

@ -365,14 +365,19 @@ class NewRelicLogger(CustomLogger):
vendor: str
) -> List[Dict[str, Any]]:
"""
Extract all messages (request + response) with sequence numbers.
Extract all messages (request + response) with sequence numbers and timestamps.
Processes request messages from kwargs["messages"] and response messages
from response_obj["choices"]. Assigns sequential numbers starting at 0.
Adds timestamps from kwargs if available (converted to epoch milliseconds).
"""
messages = []
sequence = 0
# Extract timestamps from kwargs and convert to milliseconds
start_time = kwargs.get("start_time")
end_time = kwargs.get("end_time")
# Extract request messages
request_messages = kwargs.get("messages", [])
for msg in request_messages:
@ -383,6 +388,14 @@ class NewRelicLogger(CustomLogger):
"vendor": vendor
}
# Add timestamp for request message if available (convert to milliseconds)
if start_time is not None:
# Handle both datetime objects and float timestamps
if hasattr(start_time, 'timestamp'):
message_data["timestamp"] = int(start_time.timestamp() * 1000.0)
else:
message_data["timestamp"] = int(start_time * 1000.0)
# Only add content if recording is enabled
if self._should_record_content():
message_data["content"] = self._extract_message_content(msg)
@ -404,6 +417,14 @@ class NewRelicLogger(CustomLogger):
"is_response": True
}
# Add timestamp for response message if available (convert to milliseconds)
if end_time is not None:
# Handle both datetime objects and float timestamps
if hasattr(end_time, 'timestamp'):
message_data["timestamp"] = int(end_time.timestamp() * 1000.0)
else:
message_data["timestamp"] = int(end_time * 1000.0)
# Only add content if recording is enabled
if self._should_record_content():
message_data["content"] = self._extract_message_content(message)