Skip to content

Commit

Permalink
(BSR)[API] feat: log provider id
Browse files Browse the repository at this point in the history
Update: add a public_key section inside the extra dict sent to the
logger after each request. It will contain the provider id, only for
public api routes.
  • Loading branch information
jbaudet-pass committed Oct 15, 2024
1 parent 249c98e commit 47f9c0f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/src/pcapi/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ def get_api_key_offerer_id() -> int | None:
)


def get_api_key_provider_id() -> int | None:
return (
flask.g.current_api_key.providerId
if _is_within_app_context() and hasattr(flask.g, "current_api_key") and flask.g.current_api_key
else None
)


def monkey_patch_logger_makeRecord() -> None:
def makeRecord(self, name, level, fn, lno, msg, args, exc_info, func=None, extra=None, sinfo=None): # type: ignore[no-untyped-def] # pylint: disable=too-many-positional-arguments
"""Make a record but store ``extra`` arguments in an ``extra``
Expand Down Expand Up @@ -190,6 +198,7 @@ def format(self, record: logging.LogRecord) -> str:

json_record = {
"api_key_offerer_id": get_api_key_offerer_id(),
"api_key_provider_id": get_api_key_provider_id(),
"logging.googleapis.com/trace": get_or_set_correlation_id(),
"module": record.name,
"severity": record.levelname,
Expand Down
8 changes: 8 additions & 0 deletions api/src/pcapi/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def before_request() -> None:
)
sentry_sdk.set_tag("correlation-id", get_or_set_correlation_id())
g.request_start = time.perf_counter()
g.log_request_details_extra = {}


@app.after_request
Expand Down Expand Up @@ -104,6 +105,13 @@ def log_request_details(response: flask.wrappers.Response) -> flask.wrappers.Res
else:
extra["duration"] = duration

try:
extra.update(g.log_request_details_extra)
except AttributeError:
logger.warning("g.log_request_details_extra was not available in log_request_details", exc_info=True)
except Exception: # pylint: disable=broad-exception-caught
logger.warning("g.log_request_details_extra does not seem to contain a valid dict", exc_info=True)

logger.info("HTTP request at %s", request.path, extra=extra)

return response
Expand Down
8 changes: 8 additions & 0 deletions api/src/pcapi/validation/routes/users_authentifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ def _fill_current_api_key() -> None:
app_authorization_credentials = authorization_header.replace(mandatory_authorization_type, "")
g.current_api_key = find_api_key(app_authorization_credentials)

if g.current_api_key is not None:
g.log_request_details_extra = {
"public_api": {
"api_key": g.current_api_key.id,
"provider_id": g.current_api_key.providerId,
}
}


def _get_current_api_key() -> ApiKey | None:
assert "current_api_key" in g, "Can only be used in a route wrapped with api_key_required"
Expand Down

0 comments on commit 47f9c0f

Please sign in to comment.