Skip to content

Commit b0600c8

Browse files
committed
Handle telemetry request when object does not exist.
1 parent 9ba4820 commit b0600c8

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

ads/telemetry/client.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import threading
88
import traceback
99
import urllib.parse
10+
from typing import Optional
1011

1112
import oci
12-
from requests import Response
1313

1414
from ads.config import DEBUG_TELEMETRY
1515

@@ -47,7 +47,7 @@ def _encode_user_agent(**kwargs):
4747

4848
def record_event(
4949
self, category: str = None, action: str = None, detail: str = None, **kwargs
50-
) -> Response:
50+
) -> Optional[int]:
5151
"""Send a head request to generate an event record.
5252
5353
Parameters
@@ -64,7 +64,12 @@ def record_event(
6464
6565
Returns
6666
-------
67-
Response
67+
int
68+
The status code for the telemetry request.
69+
200: The the object exists for the telemetry request
70+
404: The the object does not exist for the telemetry request.
71+
Note that for telemetry purpose, the object does not need to be exist.
72+
`None` will be returned if the telemetry request failed.
6873
"""
6974
try:
7075
if not category or not action:
@@ -77,17 +82,23 @@ def record_event(
7782
logger.debug(f"Sending telemetry to endpoint: {endpoint}")
7883

7984
self.os_client.base_client.user_agent = self._encode_user_agent(**kwargs)
80-
response: oci.response.Response = self.os_client.head_object(
81-
namespace_name=self.namespace,
82-
bucket_name=self.bucket,
83-
object_name=f"telemetry/{category}/{action}",
84-
)
85-
logger.debug(f"Telemetry status: {response.status}")
86-
return response
85+
try:
86+
response: oci.response.Response = self.os_client.head_object(
87+
namespace_name=self.namespace,
88+
bucket_name=self.bucket,
89+
object_name=f"telemetry/{category}/{action}",
90+
)
91+
logger.debug(f"Telemetry status: {response.status}")
92+
return response.status
93+
except oci.exceptions.ServiceError as ex:
94+
if ex.status == 404:
95+
return ex.status
96+
raise ex
8797
except Exception as e:
8898
if DEBUG_TELEMETRY:
8999
logger.error(f"There is an error recording telemetry: {e}")
90100
traceback.print_exc()
101+
return None
91102

92103
def record_event_async(
93104
self, category: str = None, action: str = None, detail: str = None, **kwargs

0 commit comments

Comments
 (0)