Skip to content

Implement response size logging #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/spaceone/core/service/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import copy
import functools
import json
import logging
import time
from typing import Generator, Union, Literal
from typing import Generator, Union, Literal, Any

from opentelemetry import trace
from opentelemetry.trace import format_trace_id
Expand Down Expand Up @@ -244,7 +245,10 @@ def _pipeline(
if print_info_log:

process_time = time.time() - start_time
_LOGGER.info(f"(RESPONSE) => SUCCESS (Time = {process_time:.2f}s)")
response_size = _get_response_size(response_or_iterator)
_LOGGER.info(
f"(RESPONSE) => SUCCESS (Time = {process_time:.2f}s, Size = {response_size} bytes)",
)

return response_or_iterator

Expand All @@ -265,6 +269,23 @@ def _pipeline(
delete_transaction()


def _get_response_size(response_or_iterator: Any) -> int:
try:
print(type(response_or_iterator))
Copy link
Preview

Copilot AI Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the debug print statement or replace it with _LOGGER.debug to prevent unintended output in production.

Suggested change
print(type(response_or_iterator))
_LOGGER.debug(f"Type of response_or_iterator: {type(response_or_iterator)}")

Copilot uses AI. Check for mistakes.

if isinstance(response_or_iterator, dict):
response_size = len(json.dumps(response_or_iterator, ensure_ascii=False))
elif isinstance(response_or_iterator, (bytes, bytearray)):
response_size = len(response_or_iterator)
elif response_or_iterator is None:
response_size = 0
else:
response_size = -1
except Exception:
response_size = -1

return response_size


def _error_handler(
error_type: _ERROR_TYPE,
error: ERROR_BASE,
Expand Down
Loading