Skip to content

Commit 2ffbd27

Browse files
committed
feat: optimize logging for request and asset upload processes, and enhance debug timing functionality
1 parent 65e51c6 commit 2ffbd27

5 files changed

Lines changed: 29 additions & 35 deletions

File tree

api/asset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ async def upload_asset(
125125
"asset upload request completed",
126126
extra={
127127
"event": "asset_upload_request_completed",
128-
"filename": file.filename,
128+
"upload_filename": file.filename,
129129
"content_type": file.content_type,
130130
"upload_request_ms": round(
131131
(time.perf_counter() - request_started_at) * 1000,

core/app.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ async def log_request_lifecycle(request: Request, call_next):
7474
request_id = uuid4().hex
7575
request.state.request_id = request_id
7676
logger.info(
77-
"request started %s %s",
78-
request.method,
79-
request.url.path,
77+
"request started",
8078
extra={
8179
"event": "request_started",
8280
"request_id": request_id,
@@ -91,10 +89,7 @@ async def log_request_lifecycle(request: Request, call_next):
9189
return response
9290
finally:
9391
logger.info(
94-
"request completed %s %s status=%s",
95-
request.method,
96-
request.url.path,
97-
status_code,
92+
"request completed",
9893
extra={
9994
"event": "request_completed",
10095
"request_id": request_id,

db/engine.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@
3737
logger = logging.getLogger(__name__)
3838

3939

40+
def is_pool_logging_enabled() -> bool:
41+
return bool(
42+
get_bool_env("DB_POOL_LOGGING", False)
43+
or get_bool_env("API_DEBUG_TIMING", False)
44+
)
45+
46+
4047
def _install_pool_logging(engine):
48+
if not is_pool_logging_enabled():
49+
return
50+
4151
@event.listens_for(engine, "checkout")
4252
def log_checkout(dbapi_connection, connection_record, connection_proxy):
4353
connection_record.info["checked_out_at"] = time.perf_counter()

services/gcs_helper.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ def get_storage_bucket(client=None, bucket: str = None):
7979
def _log_stage(stage: str, started_at: float, **extra):
8080
if not is_debug_timing_enabled():
8181
return
82+
record_extra = {
83+
"event": "gcs_stage_timing",
84+
"stage": stage,
85+
"duration_ms": round((time.perf_counter() - started_at) * 1000, 2),
86+
}
87+
if "filename" in extra:
88+
record_extra["upload_filename"] = extra.pop("filename")
8289
logger.info(
8390
"gcs stage timing",
84-
extra={
85-
"event": "gcs_stage_timing",
86-
"stage": stage,
87-
"duration_ms": round((time.perf_counter() - started_at) * 1000, 2),
88-
**extra,
89-
},
91+
extra={**record_extra, **extra},
9092
)
9193

9294

services/sample_helper.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import logging
2-
import time
3-
41
from fastapi import HTTPException
52
from fastapi_pagination.ext.sqlalchemy import paginate
63
from sqlalchemy import select
@@ -21,9 +18,6 @@
2118
)
2219
from services.query_helper import order_sort_filter
2320

24-
logger = logging.getLogger(__name__)
25-
26-
2721
THING_RESPONSE_BASE = (
2822
joinedload(Sample.field_activity)
2923
.joinedload(FieldActivity.field_event)
@@ -60,14 +54,17 @@
6054
CONTACT_RESPONSE_THING_ASSOCIATIONS = CONTACT_RESPONSE_PARTICIPANT.selectinload(
6155
Contact.thing_associations
6256
)
57+
CONTACT_RESPONSE_THING = CONTACT_RESPONSE_THING_ASSOCIATIONS.selectinload(
58+
ThingContactAssociation.thing
59+
)
6360

6461

6562
CONTACT_RESPONSE_LOADER_OPTIONS = (
6663
CONTACT_RESPONSE_PARTICIPANT.selectinload(Contact.emails),
6764
CONTACT_RESPONSE_PARTICIPANT.selectinload(Contact.phones),
6865
CONTACT_RESPONSE_PARTICIPANT.selectinload(Contact.addresses),
6966
CONTACT_RESPONSE_PARTICIPANT.selectinload(Contact.incomplete_nma_phones),
70-
CONTACT_RESPONSE_THING_ASSOCIATIONS.selectinload(ThingContactAssociation.thing),
67+
CONTACT_RESPONSE_THING,
7168
CONTACT_RESPONSE_PARTICIPANT.selectinload(Contact.notes),
7269
)
7370

@@ -103,8 +100,10 @@ def get_db_samples(
103100
return paginate(query)
104101

105102

106-
def get_sample_by_id_with_relationships(session: Session, sample_id: int) -> Sample:
107-
started_at = time.perf_counter()
103+
def get_sample_by_id_with_relationships(
104+
session: Session,
105+
sample_id: int,
106+
) -> Sample:
108107
sql = select(Sample).where(Sample.id == sample_id)
109108
sql = sql.options(*SAMPLE_LOADER_OPTIONS)
110109
sample = session.execute(sql).unique().scalar_one_or_none()
@@ -113,16 +112,4 @@ def get_sample_by_id_with_relationships(session: Session, sample_id: int) -> Sam
113112
status_code=HTTP_404_NOT_FOUND,
114113
detail=f"Sample with ID {sample_id} not found.",
115114
)
116-
117-
duration_ms = round((time.perf_counter() - started_at) * 1000, 2)
118-
logger.info(
119-
"sample lookup completed sample_id=%s duration_ms=%s",
120-
sample_id,
121-
duration_ms,
122-
extra={
123-
"event": "sample_lookup_completed",
124-
"sample_id": sample_id,
125-
"duration_ms": duration_ms,
126-
},
127-
)
128115
return sample

0 commit comments

Comments
 (0)