@@ -38,35 +38,35 @@ def __init__( # pylint: disable=too-many-positional-arguments,too-many-argument
3838 error_msg = "endpoint cannot be an empty string"
3939 logger .error (error_msg )
4040 telemetry .logs ().new_log (
41- msg = error_msg ,
42- tags = {"component" : "ClientBuilder" , "method" : "__init__" },
41+ msg = error_msg ,
42+ tags = {"component" : "ClientBuilder" , "method" : "__init__" },
4343 level = 40 # ERROR level
4444 )
4545 raise ValueError
4646 if not isinstance (retries , int ) or retries < 0 :
4747 error_msg = "retries must be a non-negative integer"
4848 logger .error (error_msg )
4949 telemetry .logs ().new_log (
50- msg = error_msg ,
51- tags = {"component" : "ClientBuilder" , "method" : "__init__" },
50+ msg = error_msg ,
51+ tags = {"component" : "ClientBuilder" , "method" : "__init__" },
5252 level = 40 # ERROR level
5353 )
5454 raise ValueError
5555 if not isinstance (initial_delay , int ) or initial_delay < 0 :
5656 error_msg = "initial_delay must be a non-negative integer"
5757 logger .error (error_msg )
5858 telemetry .logs ().new_log (
59- msg = error_msg ,
60- tags = {"component" : "ClientBuilder" , "method" : "__init__" },
59+ msg = error_msg ,
60+ tags = {"component" : "ClientBuilder" , "method" : "__init__" },
6161 level = 40 # ERROR level
6262 )
6363 raise ValueError
6464 if not isinstance (connection_timeout , int ) or connection_timeout < 0 :
6565 error_msg = "connection_timeout must be a non-negative integer"
6666 logger .error (error_msg )
6767 telemetry .logs ().new_log (
68- msg = error_msg ,
69- tags = {"component" : "ClientBuilder" , "method" : "__init__" },
68+ msg = error_msg ,
69+ tags = {"component" : "ClientBuilder" , "method" : "__init__" },
7070 level = 40 # ERROR level
7171 )
7272 raise ValueError
@@ -77,17 +77,17 @@ def __init__( # pylint: disable=too-many-positional-arguments,too-many-argument
7777 self .headers = headers
7878 self .retries = retries
7979 self .delay = initial_delay
80-
80+
8181 # Record client initialization metric
8282 telemetry .metrics ().metric_increment (
8383 name = "client.initialization" ,
8484 tags = {
85- "endpoint" : endpoint ,
85+ "endpoint" : endpoint ,
8686 "retry_strategy" : retry_strategy .name ,
8787 "connection_timeout" : str (connection_timeout )
8888 }
8989 )
90-
90+
9191 # Log initialization
9292 telemetry .logs ().new_log (
9393 msg = f"ClientBuilder initialized with endpoint { endpoint } " ,
@@ -112,13 +112,14 @@ def get_api_data(self):
112112 Returns:
113113 dict: The JSON response from the API as a dictionary.
114114 """
115- # Use the telemetry spans with context manager
116- with telemetry .traces ().span_in_context ("get_api_data" ) as (span , _ ):
115+ # Use the telemetry spans with new API
116+ span = telemetry .traces ().new_span ("get_api_data" )
117+ try :
117118 # Add span attributes
118119 span .set_attribute ("endpoint" , self .endpoint )
119120 span .set_attribute ("retry_strategy" , self .retry_strategy .name )
120121 span .set_attribute ("connection_timeout" , self .connection_timeout )
121-
122+
122123 # Log the API request
123124 telemetry .logs ().new_log (
124125 msg = f"Making API request to { self .endpoint } " ,
@@ -129,33 +130,33 @@ def get_api_data(self):
129130 },
130131 level = 20 # INFO level
131132 )
132-
133+
133134 # Record the start time for response time measurement
134135 start_time = time .time ()
135-
136+
136137 # Make the API request
137138 response = GetData .get_response (
138139 endpoint = self .endpoint ,
139140 headers = self .headers ,
140141 connection_timeout = self .connection_timeout ,
141142 )
142-
143+
143144 # Calculate response time
144145 response_time = time .time () - start_time
145-
146+
146147 # Record response time as histogram
147148 telemetry .metrics ().record_histogram (
148149 name = "api.response_time" ,
149150 tags = {"endpoint" : self .endpoint },
150151 value = response_time
151152 )
152-
153+
153154 # Record successful request metric
154155 telemetry .metrics ().metric_increment (
155156 name = "api.request.success" ,
156157 tags = {"endpoint" : self .endpoint }
157158 )
158-
159+
159160 # Log success
160161 telemetry .logs ().new_log (
161162 msg = f"API request to { self .endpoint } successful" ,
@@ -169,7 +170,9 @@ def get_api_data(self):
169170 level = 20 # INFO level
170171 )
171172
172- return response .json ()
173+ return response .json ()
174+ finally :
175+ span .end ()
173176
174177 @staticmethod
175178 def api_to_dataframe (response : dict ):
@@ -186,11 +189,12 @@ def api_to_dataframe(response: dict):
186189 Returns:
187190 DataFrame: A pandas DataFrame containing the data from the API response.
188191 """
189- # Use telemetry for this operation
190- with telemetry .traces ().span_in_context ("api_to_dataframe" ) as (span , _ ):
192+ # Use telemetry with new API
193+ span = telemetry .traces ().new_span ("api_to_dataframe" )
194+ try :
191195 response_size = len (response ) if isinstance (response , list ) else 1
192196 span .set_attribute ("response_size" , response_size )
193-
197+
194198 # Log conversion start
195199 telemetry .logs ().new_log (
196200 msg = "Converting API response to DataFrame" ,
@@ -202,17 +206,17 @@ def api_to_dataframe(response: dict):
202206 },
203207 level = 20 # INFO level
204208 )
205-
209+
206210 try :
207211 # Convert to dataframe
208212 df = GetData .to_dataframe (response )
209-
213+
210214 # Record metrics
211215 telemetry .metrics ().metric_increment (
212216 name = "dataframe.conversion.success" ,
213217 tags = {"size" : len (df )}
214218 )
215-
219+
216220 # Log success
217221 telemetry .logs ().new_log (
218222 msg = "Successfully converted API response to DataFrame" ,
@@ -224,16 +228,16 @@ def api_to_dataframe(response: dict):
224228 },
225229 level = 20 # INFO level
226230 )
227-
231+
228232 return df
229-
233+
230234 except Exception as e :
231235 # Record failure metric
232236 telemetry .metrics ().metric_increment (
233237 name = "dataframe.conversion.failure" ,
234238 tags = {"error_type" : type (e ).__name__ }
235239 )
236-
240+
237241 # Log error
238242 error_msg = f"Failed to convert API response to DataFrame: { str (e )} "
239243 telemetry .logs ().new_log (
@@ -246,6 +250,8 @@ def api_to_dataframe(response: dict):
246250 },
247251 level = 40 # ERROR level
248252 )
249-
253+
250254 # Re-raise the exception
251255 raise
256+ finally :
257+ span .end ()
0 commit comments