Skip to content
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
1 change: 1 addition & 0 deletions changelog.d/4219.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The economy routes now log a request they refuse with 400 or 404 (a year outside the country's years, an unknown region or other invalid setting, or a policy the country does not have) at WARNING instead of ERROR, so refused requests no longer form error groups in the telemetry error report. Every other failure is still logged at ERROR.
21 changes: 13 additions & 8 deletions policyengine_api/services/economy_budget_window_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
ImpactPolicies,
SimulationOptions,
)
from policyengine_api.services.economy_request_failures import (
log_economy_request_failure,
)
from policyengine_api.utils import budget_window as budget_window_utils


Expand All @@ -54,8 +57,11 @@ def get_budget_window_economic_impact(
the SPM selection or a stored result cannot be certified, or, before any
stored state is read, when the start year is outside the country's
``ECONOMY_YEARS`` (``DATASET_YEAR_UNAVAILABLE``). A window that starts in
those years may end after them.
those years may end after them. Every exception is logged and re-raised:
a refused request at WARNING, any other failure at ERROR (see
``log_economy_request_failure``).
"""
setup_options: EconomicImpactSetupOptions | None = None
set_runtime_context(
country_id=impact_request.country_id,
policy_id=impact_request.policy_id,
Expand Down Expand Up @@ -101,13 +107,12 @@ def get_budget_window_economic_impact(
budget_window_setup,
max_active_years=max_active_years,
)
except Exception as e:
logger.log_struct(
{
"message": "Error getting budget-window economic impact",
"error_type": type(e).__name__,
},
severity="ERROR",
except Exception as error:
log_economy_request_failure(
error,
request_validated=setup_options is not None,
failure_message="Error getting budget-window economic impact",
refusal_message="Refused budget-window economic impact request",
)
raise

Expand Down
64 changes: 64 additions & 0 deletions policyengine_api/services/economy_request_failures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Log a failed economy request at a severity that matches its cause.

The daily telemetry error report groups the API's records at severity ERROR or
above (``.github/scripts/telemetry_error_issues``). A request the economy
routes refuse with a client error is not a failure of the API, so it is logged
at WARNING and forms no error group.
"""

from typing import Any

from policyengine_api.gcp_logging import logger
from policyengine_api.services.policy_service import PolicyNotFoundError
from policyengine_api.spm import SPMValidationError

REFUSED_REQUEST_SEVERITY = "WARNING"
FAILED_REQUEST_SEVERITY = "ERROR"


def is_refused_request(error: Exception, *, request_validated: bool) -> bool:
"""Return whether ``error`` refuses the request rather than failing it.

A request is refused when it names a policy its country does not have
(PolicyNotFoundError, which the routes answer with 404), or when request
validation raises a ValueError, which they answer with 400: a year outside
the country's years, an unknown region, a dataset, breakdown or SPM setting
the API does not offer. Validation ends once the request's setup options
are built, before any stored result is read or any job is sent, and
``request_validated`` says whether it had ended when ``error`` was raised.

A ValueError raised after validation is not a refusal, although the route
also answers it with 400: it reports a state the API did not expect, such
as an unknown stored status, or a typed failure the simulation service
stored for the request.
"""
if isinstance(error, PolicyNotFoundError):
return True
return not request_validated and isinstance(error, ValueError)


def log_economy_request_failure(
error: Exception,
*,
request_validated: bool,
failure_message: str,
refusal_message: str,
) -> None:
"""Log ``error``, which an economy request raised, before it is re-raised.

A refusal (see ``is_refused_request``) is logged with ``refusal_message``
at WARNING, and any other failure with ``failure_message`` at ERROR. Both
record the exception's type, and an SPMValidationError's code, never its
message.
"""
refused = is_refused_request(error, request_validated=request_validated)
record: dict[str, Any] = {
"message": refusal_message if refused else failure_message,
"error_type": type(error).__name__,
}
if isinstance(error, SPMValidationError):
record["error_code"] = error.code
logger.log_struct(
record,
severity=REFUSED_REQUEST_SEVERITY if refused else FAILED_REQUEST_SEVERITY,
)
20 changes: 12 additions & 8 deletions policyengine_api/services/economy_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
RuntimeBundleIdentity,
SimulationOptions,
)
from policyengine_api.services.economy_request_failures import (
log_economy_request_failure,
)
from policyengine_api.services.economy_simulation_requests import (
SimulationRequestMixin,
)
Expand Down Expand Up @@ -173,7 +176,9 @@ def get_economic_impact(
"computing" while the calculation runs, or "error" with a ``message``.
A year outside the country's ``ECONOMY_YEARS`` raises
SPMValidationError (``DATASET_YEAR_UNAVAILABLE``) before any stored
result is read. Unexpected failures are logged and re-raised.
result is read. Every exception is logged and re-raised: a refused
request at WARNING, any other failure at ERROR (see
``log_economy_request_failure``).
"""
set_runtime_context(
country_id=impact_request.country_id,
Expand All @@ -197,13 +202,12 @@ def get_economic_impact(
setup_options=economic_impact_setup_options,
)

except Exception as e:
logger.log_struct(
{
"message": "Error getting economic impact",
"error_type": type(e).__name__,
},
severity="ERROR",
except Exception as error:
log_economy_request_failure(
error,
request_validated=economic_impact_setup_options is not None,
failure_message="Error getting economic impact",
refusal_message="Refused economic impact request",
)
raise
finally:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/services/economy_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"policyengine_api.services.economy_budget_window_submission",
"policyengine_api.services.economy_impact_execution",
"policyengine_api.services.economy_impact_types",
"policyengine_api.services.economy_request_failures",
"policyengine_api.services.economy_stored_impacts",
)
MOCK_MODAL_JOB_ID = "fc-test123xyz"
Expand Down
Loading
Loading