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 all commits
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
24 changes: 22 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,22 @@ def _pipeline(
delete_transaction()


def _get_response_size(response_or_iterator: Any) -> int:
try:
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