Clarify observability logging loops

This commit is contained in:
copilot-swe-agent[bot]
2026-05-20 22:06:35 +00:00
committed by GitHub
Unverified
parent f6db62e07b
commit ecf5341ca0
2 changed files with 13 additions and 4 deletions
@@ -2158,10 +2158,9 @@ def _capture_messages(
normalized_messages = normalize_messages(messages)
logging_messages = prepend_instructions_to_messages(normalized_messages, system_instructions)
span_messages = [_to_otel_message(message) for message in normalized_messages]
span_message_iter = iter(span_messages)
prepended_count = len(logging_messages) - len(normalized_messages)
for index, message in enumerate(logging_messages):
otel_message = _to_otel_message(message) if index < prepended_count else next(span_message_iter)
for index, message in enumerate(logging_messages[:prepended_count]):
otel_message = _to_otel_message(message)
# Reuse the otel message representation for logging instead of calling to_dict()
# to avoid expensive Pydantic serialization overhead
logger.info(
@@ -2172,6 +2171,16 @@ def _capture_messages(
MessageListTimestampFilter.INDEX_KEY: index,
},
)
for index, message in enumerate(normalized_messages, start=prepended_count):
otel_message = span_messages[index - prepended_count]
logger.info(
otel_message,
extra={
OtelAttr.EVENT_NAME: OtelAttr.CHOICE if output else ROLE_EVENT_MAP.get(message.role),
OtelAttr.PROVIDER_NAME: provider_name,
MessageListTimestampFilter.INDEX_KEY: index,
},
)
if finish_reason:
span_messages[-1]["finish_reason"] = FINISH_REASON_MAP[finish_reason]
span.set_attribute(
@@ -3046,7 +3046,7 @@ def test_capture_messages_logs_prepended_instructions_without_serializing_them(
input_messages = json.loads(spans[0].attributes[OtelAttr.INPUT_MESSAGES])
assert [msg.get("role") for msg in input_messages] == ["user"]
assert mock_logger_info.call_count == 2
assert mock_logger_info.call_count == 2, f"Expected 2 log calls, got {mock_logger_info.call_count}"
first_call, second_call = mock_logger_info.call_args_list
assert first_call.args
assert second_call.args