From 1fbdac86b91960f82b1b253a4b29bb1d92c34521 Mon Sep 17 00:00:00 2001 From: Michael Huneke Date: Fri, 6 Dec 2024 15:49:59 -0500 Subject: [PATCH] Move packages / PR comments --- api/src/{util => adapters}/newrelic/__init__.py | 0 api/src/{util => adapters}/newrelic/events.py | 14 ++++---------- api/src/app.py | 6 +++--- 3 files changed, 7 insertions(+), 13 deletions(-) rename api/src/{util => adapters}/newrelic/__init__.py (100%) rename api/src/{util => adapters}/newrelic/events.py (83%) diff --git a/api/src/util/newrelic/__init__.py b/api/src/adapters/newrelic/__init__.py similarity index 100% rename from api/src/util/newrelic/__init__.py rename to api/src/adapters/newrelic/__init__.py diff --git a/api/src/util/newrelic/events.py b/api/src/adapters/newrelic/events.py similarity index 83% rename from api/src/util/newrelic/events.py rename to api/src/adapters/newrelic/events.py index 401369344..9c6da0309 100644 --- a/api/src/util/newrelic/events.py +++ b/api/src/adapters/newrelic/events.py @@ -1,7 +1,7 @@ import logging import sys from types import TracebackType -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any import newrelic.agent @@ -11,15 +11,13 @@ EVENT_API_ATTRIBUTE_LIMIT = 255 -def record_custom_event(event_type: str, params: Dict[Any, Any]) -> None: +def record_custom_event(event_type: str, params: dict[Any, Any]) -> None: # legacy aliases params["request.uri"] = params.get("request.path") params["request.headers.x-amzn-requestid"] = params.get("request_id") - # legacy FineosError attribute aliases params["api.request.method"] = params.get("request.method") params["api.request.uri"] = params.get("request.path") - params["api.request.headers.x-amzn-requestid"] = params.get("request_id") # If there are more custom attributes than the limit, the agent will upload # a partial payload, dropping keys after hitting its limit @@ -41,9 +39,7 @@ def record_custom_event(event_type: str, params: Dict[Any, Any]) -> None: ) -def log_and_capture_exception( - msg: str, extra: Optional[Dict] = None, only_warn: bool = False -) -> None: +def log_and_capture_exception(msg: str, extra: dict | None = None, only_warn: bool = False) -> None: """ Sometimes we want to record custom messages that do not match an exception message. For example, when a ValidationError contains multiple issues, we want to log and record each one individually in New Relic. Injecting a new exception with a @@ -53,9 +49,7 @@ def log_and_capture_exception( """ info = sys.exc_info() - info_with_readable_msg: Union[ - BaseException, Tuple[type, BaseException, Optional[TracebackType]] - ] + info_with_readable_msg: BaseException | tuple[type, BaseException, TracebackType | None] if info[0] is None: exc = Exception(msg) diff --git a/api/src/app.py b/api/src/app.py index fd5e6deb4..592050371 100644 --- a/api/src/app.py +++ b/api/src/app.py @@ -13,6 +13,7 @@ import src.api.feature_flags.feature_flag_config as feature_flag_config import src.logging import src.logging.flask_logger as flask_logger +from src.adapters.newrelic import init_newrelic from src.api.agencies_v1 import agency_blueprint as agencies_v1_blueprint from src.api.extracts_v1 import extract_blueprint as extracts_v1_blueprint from src.api.healthcheck import healthcheck_blueprint @@ -29,10 +30,11 @@ from src.search.backend.load_search_data_blueprint import load_search_data_blueprint from src.task import task_blueprint from src.util.env_config import PydanticBaseEnvConfig -from src.util.newrelic import init_newrelic logger = logging.getLogger(__name__) +init_newrelic() + TITLE = "Simpler Grants API" API_OVERALL_VERSION = "v0" API_DESCRIPTION = """ @@ -68,8 +70,6 @@ def create_app() -> APIFlask: if os.getenv("ENVIRONMENT") == "local": initialize_jwt_auth() - init_newrelic() - return app