Skip to content

Commit 781e850

Browse files
authored
Merge pull request #238 from ecmwf-projects/COPDS-2390-reduce-traceback-length-in-logs
Reduce tracebacks length in logs
2 parents cbf26c2 + da1ae20 commit 781e850

File tree

5 files changed

+36
-40
lines changed

5 files changed

+36
-40
lines changed

cads_processing_api_service/clients.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class DatabaseClient(ogc_api_processes_fastapi.clients.BaseClient):
8383
"DeleteJob": "Dismiss the job identifed by `job_id`.",
8484
}
8585

86+
@exceptions.exception_logger
8687
def get_processes(
8788
self,
8889
limit: int | None = fastapi.Query(10, ge=1, le=10000),
@@ -139,6 +140,7 @@ def get_processes(
139140
process_list._pagination_query_params = pagination_query_params
140141
return process_list
141142

143+
@exceptions.exception_logger
142144
def get_process(
143145
self,
144146
response: fastapi.Response,
@@ -188,6 +190,7 @@ def get_process(
188190

189191
return process_description
190192

193+
@exceptions.exception_logger
191194
def post_process_execution(
192195
self,
193196
request: fastapi.Request,
@@ -327,6 +330,7 @@ def post_process_execution(
327330
status_info.message = job_message
328331
return status_info
329332

333+
@exceptions.exception_logger
330334
def get_jobs(
331335
self,
332336
processID: list[str] | None = fastapi.Query(None),
@@ -460,6 +464,7 @@ def get_jobs(
460464

461465
return job_list
462466

467+
@exceptions.exception_logger
463468
def get_job(
464469
self,
465470
job_id: str = fastapi.Path(...),
@@ -594,6 +599,7 @@ def get_job(
594599
status_info = utils.make_status_info(job=job, **kwargs)
595600
return status_info
596601

602+
@exceptions.exception_logger
597603
def get_job_results(
598604
self,
599605
job_id: str = fastapi.Path(...),
@@ -656,6 +662,7 @@ def get_job_results(
656662
handle_download_metrics(job.process_id, results)
657663
return results
658664

665+
@exceptions.exception_logger
659666
def delete_job(
660667
self,
661668
job_id: str = fastapi.Path(...),

cads_processing_api_service/constraints.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from . import adaptors, db_utils, exceptions, models, utils
1010

1111

12+
@exceptions.exception_logger
1213
def apply_constraints(
1314
process_id: str = fastapi.Path(...),
1415
execution_content: models.Execute = fastapi.Body(...),

cads_processing_api_service/costing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import cads_catalogue
2323
import fastapi
2424

25-
from . import adaptors, costing, db_utils, models, utils
25+
from . import adaptors, costing, db_utils, exceptions, models, utils
2626

2727
COST_THRESHOLDS = {"api": "max_costs", "ui": "max_costs_portal"}
2828

@@ -32,6 +32,7 @@ class RequestOrigin(str, enum.Enum):
3232
ui = "ui"
3333

3434

35+
@exceptions.exception_logger
3536
def estimate_cost(
3637
process_id: str = fastapi.Path(...),
3738
request_origin: RequestOrigin = fastapi.Query("api"),

cads_processing_api_service/exceptions.py

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License
1616

17+
import functools
1718
import traceback
19+
from typing import Any, Callable
1820

1921
import attrs
2022
import fastapi
@@ -63,26 +65,28 @@ class RateLimitExceeded(ogc_api_processes_fastapi.exceptions.OGCAPIException):
6365
retry_after: int = 0
6466

6567

66-
def get_exc_group_tb(exc_traceback: list[str]) -> list[str]:
67-
"""Get and return only the Exception Group Traceback.
68-
69-
Parameters
70-
----------
71-
exc_traceback : list[str]
72-
List of lines in the traceback.
73-
74-
Returns
75-
-------
76-
list[str]
77-
List of lines in the traceback.
78-
"""
79-
exc_group_tb = []
80-
if "Exception Group Traceback" in exc_traceback[0]:
81-
exc_group_tb.append(exc_traceback[0])
82-
for line in exc_traceback[1:]:
83-
if line.lstrip(" ").startswith(("+", "|")):
84-
exc_group_tb.append(line)
85-
return exc_group_tb
68+
def exception_logger(f: Callable[..., Any]) -> Callable[..., Any]:
69+
@functools.wraps(f)
70+
def wrapper(*args: Any, **kwargs: Any) -> Any:
71+
try:
72+
res = f(*args, **kwargs)
73+
except requests.exceptions.ReadTimeout:
74+
raise
75+
except Exception as exc:
76+
if isinstance(exc, ogc_api_processes_fastapi.exceptions.OGCAPIException):
77+
exc_title = exc.title
78+
else:
79+
exc_title = "internal server error"
80+
logger.error(
81+
exc_title,
82+
exception="".join(
83+
traceback.TracebackException.from_exception(exc).format()
84+
),
85+
)
86+
raise
87+
return res
88+
89+
return wrapper
8690

8791

8892
def format_exception_content(
@@ -135,11 +139,6 @@ def exception_handler(
135139
fastapi.responses.JSONResponse
136140
JSON response.
137141
"""
138-
logger.error(
139-
exc.title,
140-
exception="".join(traceback.TracebackException.from_exception(exc).format()),
141-
url=str(request.url),
142-
)
143142
out = fastapi.responses.JSONResponse(
144143
status_code=exc.status_code,
145144
content=format_exception_content(exc=exc, request=request),
@@ -164,11 +163,6 @@ def rate_limit_exceeded_exception_handler(
164163
fastapi.responses.JSONResponse
165164
JSON response.
166165
"""
167-
logger.error(
168-
exc.title,
169-
exception="".join(traceback.TracebackException.from_exception(exc).format()),
170-
url=str(request.url),
171-
)
172166
out = fastapi.responses.JSONResponse(
173167
status_code=exc.status_code,
174168
content=format_exception_content(exc=exc, request=request),
@@ -223,14 +217,6 @@ def general_exception_handler(
223217
-------
224218
fastapi.responses.JSONResponse
225219
"""
226-
exc_traceback = list(traceback.TracebackException.from_exception(exc).format())
227-
exc_group_traceback = get_exc_group_tb(exc_traceback)
228-
logger.error(
229-
"internal server error",
230-
exception="".join(
231-
exc_group_traceback if exc_group_traceback else exc_traceback
232-
),
233-
)
234220
out = fastapi.responses.JSONResponse(
235221
status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR,
236222
content=models.Exception(

cads_processing_api_service/translators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import fastapi
2121
import structlog
2222

23-
from . import config
23+
from . import config, exceptions
2424

2525
SETTINGS = config.settings
2626

@@ -335,6 +335,7 @@ def format_api_request(
335335
return api_request
336336

337337

338+
@exceptions.exception_logger
338339
def get_api_request(
339340
process_id: str = fastapi.Path(...),
340341
request: dict[str, Any] = fastapi.Body(...),

0 commit comments

Comments
 (0)