Skip to content

Commit 00b7b85

Browse files
Feature/log req id correlation (#223)
* log request id and correlation id * reformatted: log request id * reformatted: log request id * added docstring
1 parent c2e30c7 commit 00b7b85

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

src/eligibility_signposting_api/views/eligibility.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
@eligibility_blueprint.before_request
3232
def before_request() -> None:
3333
logger.info(
34-
"X-Request-ID: %s, X-Correlation-ID: %s",
35-
request.headers.get("X-Request-ID"),
36-
request.headers.get("X-Correlation-ID"),
34+
"request details",
35+
extra={
36+
"X-Request-ID": request.headers.get("X-Request-ID"),
37+
"X-Correlation-ID": request.headers.get("X-Correlation-ID"),
38+
},
3739
)
3840
AuditContext.add_request_details(request)
3941

tests/unit/views/test_eligibility.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -690,32 +690,40 @@ def test_build_response_include_values_that_are_not_null(client: FlaskClient):
690690

691691

692692
@pytest.mark.parametrize(
693-
("headers", "expected_log_msg"),
693+
("headers", "expected_request_id"),
694694
[
695+
({"X-Request-ID": "test-request-id-123"}, "test-request-id-123"),
695696
(
696-
{"X-Request-ID": "test-request-id-123", "X-Correlation-ID": "test-correlation-id-456"},
697-
"X-Request-ID: test-request-id-123, X-Correlation-ID: test-correlation-id-456",
698-
),
699-
(
700-
{"X-Request-ID": "", "X-Correlation-ID": ""},
701-
"X-Request-ID: , X-Correlation-ID: ",
697+
{"X-Request-ID": ""},
698+
"",
702699
),
703700
(
704701
{}, # No headers provided
705-
"X-Request-ID: None, X-Correlation-ID: None",
702+
None,
706703
),
707704
],
708705
)
709-
def test_request_id_and_correlation_id_logging_variants(
710-
app: Flask, client: FlaskClient, caplog, headers, expected_log_msg
706+
def test_request_id_from_header_logging_variants(
707+
app: Flask, client: FlaskClient, caplog, headers: dict[str, str], expected_request_id: str
711708
):
709+
"""
710+
This test checks that the x-request-ID is logged so that it can be used to correlate logs
711+
with that of the logs from api-gateway
712+
"""
712713
with (
713714
get_app_container(app).override.service(EligibilityService, new=FakeEligibilityService()),
714715
get_app_container(app).override.service(AuditService, new=FakeAuditService()),
715716
):
716717
with caplog.at_level(logging.INFO):
717718
response = client.get("/patient-check/12345", headers=headers)
718719

719-
log_messages = [record.getMessage() for record in caplog.records]
720-
assert any(expected_log_msg in message for message in log_messages)
720+
request_id_logged = False
721+
for record in caplog.records:
722+
request_id = getattr(record, "X-Request-ID", None)
723+
724+
if request_id == expected_request_id:
725+
request_id_logged = True
726+
break
727+
728+
assert request_id_logged
721729
assert response.status_code == HTTPStatus.OK

0 commit comments

Comments
 (0)