Skip to content

Commit ed4098d

Browse files
add missing request id
1 parent f4240ce commit ed4098d

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

ads/aqua/extension/aqua_ws_msg_handler.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*--
32

4-
# Copyright (c) 2024 Oracle and/or its affiliates.
3+
# Copyright (c) 2024, 2025 Oracle and/or its affiliates.
54
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
65

76
import traceback
7+
import uuid
88
from abc import abstractmethod
99
from http.client import responses
1010
from typing import List
@@ -34,7 +34,7 @@ def __init__(self, message: str):
3434
self.telemetry = TelemetryClient(
3535
bucket=AQUA_TELEMETRY_BUCKET, namespace=AQUA_TELEMETRY_BUCKET_NS
3636
)
37-
except:
37+
except Exception:
3838
pass
3939

4040
@staticmethod
@@ -66,24 +66,31 @@ def write_error(self, status_code, **kwargs):
6666
"message": message,
6767
"service_payload": service_payload,
6868
"reason": reason,
69+
"request_id": str(uuid.uuid4()),
6970
}
7071
exc_info = kwargs.get("exc_info")
7172
if exc_info:
72-
logger.error("".join(traceback.format_exception(*exc_info)))
73+
logger.error(
74+
f"Error Request ID: {reply['request_id']}\n"
75+
f"Error: {''.join(traceback.format_exception(*exc_info))}"
76+
)
7377
e = exc_info[1]
7478
if isinstance(e, HTTPError):
7579
reply["message"] = e.log_message or message
7680
reply["reason"] = e.reason
77-
else:
78-
logger.warning(reply["message"])
81+
82+
logger.error(
83+
f"Error Request ID: {reply['request_id']}\n"
84+
f"Error: {reply['message']} {reply['reason']}"
85+
)
7986
# telemetry may not be present if there is an error while initializing
8087
if hasattr(self, "telemetry"):
8188
aqua_api_details = kwargs.get("aqua_api_details", {})
8289
self.telemetry.record_event_async(
8390
category="aqua/error",
8491
action=str(status_code),
8592
value=reason,
86-
**aqua_api_details
93+
**aqua_api_details,
8794
)
8895
response = AquaWsError(
8996
status=status_code,

ads/cli.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*--
32

4-
# Copyright (c) 2021, 2024 Oracle and/or its affiliates.
3+
# Copyright (c) 2021, 2025 Oracle and/or its affiliates.
54
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
65

6+
import logging
77
import sys
88
import traceback
9-
from dataclasses import is_dataclass
9+
import uuid
1010

1111
import fire
1212

@@ -27,7 +27,7 @@
2727
)
2828
logger.debug(ex)
2929
logger.debug(traceback.format_exc())
30-
exit()
30+
sys.exit()
3131

3232
# https://packaging.python.org/en/latest/guides/single-sourcing-package-version/#single-sourcing-the-package-version
3333
if sys.version_info >= (3, 8):
@@ -122,8 +122,9 @@ def exit_program(ex: Exception, logger: "logging.Logger") -> None:
122122
... exit_program(e, logger)
123123
"""
124124

125-
logger.debug(traceback.format_exc())
126-
logger.error(str(ex))
125+
request_id = str(uuid.uuid4())
126+
logger.debug(f"Error Request ID: {request_id}\nError: {traceback.format_exc()}")
127+
logger.error(f"Error Request ID: {request_id}\n" f"Error: {str(ex)}")
127128

128129
exit_code = getattr(ex, "exit_code", 1)
129130
logger.error(f"Exit code: {exit_code}")

tests/unitary/with_extras/aqua/test_cli.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*--
33

4-
# Copyright (c) 2024 Oracle and/or its affiliates.
4+
# Copyright (c) 2024, 2025 Oracle and/or its affiliates.
55
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
66

77
import logging
88
import os
99
import subprocess
10+
import uuid
1011
from importlib import reload
1112
from unittest import TestCase
1213
from unittest.mock import call, patch
@@ -148,6 +149,7 @@ def test_aqua_cli(self, mock_logger, mock_aqua_command, mock_fire, mock_serializ
148149
]
149150
)
150151
@patch("sys.argv", ["ads", "aqua", "--error-option"])
152+
@patch("uuid.uuid4")
151153
@patch("fire.Fire")
152154
@patch("ads.aqua.cli.AquaCommand")
153155
@patch("ads.aqua.logger.error")
@@ -162,11 +164,17 @@ def test_aqua_cli_with_error(
162164
mock_logger_error,
163165
mock_aqua_command,
164166
mock_fire,
167+
mock_uuid,
165168
):
166169
"""Tests when Aqua Cli gracefully exit when error raised."""
167170
mock_fire.side_effect = mock_side_effect
168171
from ads.cli import cli
169172

173+
uuid_value = "12345678-1234-5678-1234-567812345678"
174+
mock_uuid.return_value = uuid.UUID(uuid_value)
175+
expected_logging_message = type(expected_logging_message)(
176+
f"Error Request ID: {uuid_value}\nError: {str(expected_logging_message)}"
177+
)
170178
cli()
171179
calls = [
172180
call(expected_logging_message),

0 commit comments

Comments
 (0)