Skip to content

fix: hotfix to context manager #98

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
Mar 10, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "api-to-dataframe"
version = "1.5.1"
version = "1.5.2"
description = "A package to convert API responses to pandas dataframe"
authors = ["IvanildoBarauna <[email protected]>"]
readme = "README.md"
Expand Down
68 changes: 37 additions & 31 deletions src/api_to_dataframe/controller/client_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,35 @@ def __init__( # pylint: disable=too-many-positional-arguments,too-many-argument
error_msg = "endpoint cannot be an empty string"
logger.error(error_msg)
telemetry.logs().new_log(
msg=error_msg,
tags={"component": "ClientBuilder", "method": "__init__"},
msg=error_msg,
tags={"component": "ClientBuilder", "method": "__init__"},
level=40 # ERROR level
)
raise ValueError
if not isinstance(retries, int) or retries < 0:
error_msg = "retries must be a non-negative integer"
logger.error(error_msg)
telemetry.logs().new_log(
msg=error_msg,
tags={"component": "ClientBuilder", "method": "__init__"},
msg=error_msg,
tags={"component": "ClientBuilder", "method": "__init__"},
level=40 # ERROR level
)
raise ValueError
if not isinstance(initial_delay, int) or initial_delay < 0:
error_msg = "initial_delay must be a non-negative integer"
logger.error(error_msg)
telemetry.logs().new_log(
msg=error_msg,
tags={"component": "ClientBuilder", "method": "__init__"},
msg=error_msg,
tags={"component": "ClientBuilder", "method": "__init__"},
level=40 # ERROR level
)
raise ValueError
if not isinstance(connection_timeout, int) or connection_timeout < 0:
error_msg = "connection_timeout must be a non-negative integer"
logger.error(error_msg)
telemetry.logs().new_log(
msg=error_msg,
tags={"component": "ClientBuilder", "method": "__init__"},
msg=error_msg,
tags={"component": "ClientBuilder", "method": "__init__"},
level=40 # ERROR level
)
raise ValueError
Expand All @@ -77,17 +77,17 @@ def __init__( # pylint: disable=too-many-positional-arguments,too-many-argument
self.headers = headers
self.retries = retries
self.delay = initial_delay

# Record client initialization metric
telemetry.metrics().metric_increment(
name="client.initialization",
tags={
"endpoint": endpoint,
"endpoint": endpoint,
"retry_strategy": retry_strategy.name,
"connection_timeout": str(connection_timeout)
}
)

# Log initialization
telemetry.logs().new_log(
msg=f"ClientBuilder initialized with endpoint {endpoint}",
Expand All @@ -112,13 +112,14 @@ def get_api_data(self):
Returns:
dict: The JSON response from the API as a dictionary.
"""
# Use the telemetry spans with context manager
with telemetry.traces().span_in_context("get_api_data") as (span, _):
# Use the telemetry spans with new API
span = telemetry.traces().new_span("get_api_data")
try:
# Add span attributes
span.set_attribute("endpoint", self.endpoint)
span.set_attribute("retry_strategy", self.retry_strategy.name)
span.set_attribute("connection_timeout", self.connection_timeout)

# Log the API request
telemetry.logs().new_log(
msg=f"Making API request to {self.endpoint}",
Expand All @@ -129,33 +130,33 @@ def get_api_data(self):
},
level=20 # INFO level
)

# Record the start time for response time measurement
start_time = time.time()

# Make the API request
response = GetData.get_response(
endpoint=self.endpoint,
headers=self.headers,
connection_timeout=self.connection_timeout,
)

# Calculate response time
response_time = time.time() - start_time

# Record response time as histogram
telemetry.metrics().record_histogram(
name="api.response_time",
tags={"endpoint": self.endpoint},
value=response_time
)

# Record successful request metric
telemetry.metrics().metric_increment(
name="api.request.success",
tags={"endpoint": self.endpoint}
)

# Log success
telemetry.logs().new_log(
msg=f"API request to {self.endpoint} successful",
Expand All @@ -169,7 +170,9 @@ def get_api_data(self):
level=20 # INFO level
)

return response.json()
return response.json()
finally:
span.end()

@staticmethod
def api_to_dataframe(response: dict):
Expand All @@ -186,11 +189,12 @@ def api_to_dataframe(response: dict):
Returns:
DataFrame: A pandas DataFrame containing the data from the API response.
"""
# Use telemetry for this operation
with telemetry.traces().span_in_context("api_to_dataframe") as (span, _):
# Use telemetry with new API
span = telemetry.traces().new_span("api_to_dataframe")
try:
response_size = len(response) if isinstance(response, list) else 1
span.set_attribute("response_size", response_size)

# Log conversion start
telemetry.logs().new_log(
msg="Converting API response to DataFrame",
Expand All @@ -202,17 +206,17 @@ def api_to_dataframe(response: dict):
},
level=20 # INFO level
)

try:
# Convert to dataframe
df = GetData.to_dataframe(response)

# Record metrics
telemetry.metrics().metric_increment(
name="dataframe.conversion.success",
tags={"size": len(df)}
)

# Log success
telemetry.logs().new_log(
msg="Successfully converted API response to DataFrame",
Expand All @@ -224,16 +228,16 @@ def api_to_dataframe(response: dict):
},
level=20 # INFO level
)

return df

except Exception as e:
# Record failure metric
telemetry.metrics().metric_increment(
name="dataframe.conversion.failure",
tags={"error_type": type(e).__name__}
)

# Log error
error_msg = f"Failed to convert API response to DataFrame: {str(e)}"
telemetry.logs().new_log(
Expand All @@ -246,6 +250,8 @@ def api_to_dataframe(response: dict):
},
level=40 # ERROR level
)

# Re-raise the exception
raise
finally:
span.end()
Loading