7
7
import threading
8
8
import traceback
9
9
import urllib .parse
10
+ from typing import Optional
10
11
11
12
import oci
12
- from requests import Response
13
13
14
14
from ads .config import DEBUG_TELEMETRY
15
15
@@ -47,7 +47,7 @@ def _encode_user_agent(**kwargs):
47
47
48
48
def record_event (
49
49
self , category : str = None , action : str = None , detail : str = None , ** kwargs
50
- ) -> Response :
50
+ ) -> Optional [ int ] :
51
51
"""Send a head request to generate an event record.
52
52
53
53
Parameters
@@ -64,7 +64,12 @@ def record_event(
64
64
65
65
Returns
66
66
-------
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.
68
73
"""
69
74
try :
70
75
if not category or not action :
@@ -77,17 +82,23 @@ def record_event(
77
82
logger .debug (f"Sending telemetry to endpoint: { endpoint } " )
78
83
79
84
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
87
97
except Exception as e :
88
98
if DEBUG_TELEMETRY :
89
99
logger .error (f"There is an error recording telemetry: { e } " )
90
100
traceback .print_exc ()
101
+ return None
91
102
92
103
def record_event_async (
93
104
self , category : str = None , action : str = None , detail : str = None , ** kwargs
0 commit comments