diff --git a/src/eligibility_signposting_api/views/eligibility.py b/src/eligibility_signposting_api/views/eligibility.py index df512b08e..628962369 100644 --- a/src/eligibility_signposting_api/views/eligibility.py +++ b/src/eligibility_signposting_api/views/eligibility.py @@ -31,9 +31,11 @@ @eligibility_blueprint.before_request def before_request() -> None: logger.info( - "X-Request-ID: %s, X-Correlation-ID: %s", - request.headers.get("X-Request-ID"), - request.headers.get("X-Correlation-ID"), + "request details", + extra={ + "X-Request-ID": request.headers.get("X-Request-ID"), + "X-Correlation-ID": request.headers.get("X-Correlation-ID"), + }, ) AuditContext.add_request_details(request) diff --git a/tests/unit/views/test_eligibility.py b/tests/unit/views/test_eligibility.py index 546c37c07..b8a140d80 100644 --- a/tests/unit/views/test_eligibility.py +++ b/tests/unit/views/test_eligibility.py @@ -690,25 +690,26 @@ def test_build_response_include_values_that_are_not_null(client: FlaskClient): @pytest.mark.parametrize( - ("headers", "expected_log_msg"), + ("headers", "expected_request_id"), [ + ({"X-Request-ID": "test-request-id-123"}, "test-request-id-123"), ( - {"X-Request-ID": "test-request-id-123", "X-Correlation-ID": "test-correlation-id-456"}, - "X-Request-ID: test-request-id-123, X-Correlation-ID: test-correlation-id-456", - ), - ( - {"X-Request-ID": "", "X-Correlation-ID": ""}, - "X-Request-ID: , X-Correlation-ID: ", + {"X-Request-ID": ""}, + "", ), ( {}, # No headers provided - "X-Request-ID: None, X-Correlation-ID: None", + None, ), ], ) -def test_request_id_and_correlation_id_logging_variants( - app: Flask, client: FlaskClient, caplog, headers, expected_log_msg +def test_request_id_from_header_logging_variants( + app: Flask, client: FlaskClient, caplog, headers: dict[str, str], expected_request_id: str ): + """ + This test checks that the x-request-ID is logged so that it can be used to correlate logs + with that of the logs from api-gateway + """ with ( get_app_container(app).override.service(EligibilityService, new=FakeEligibilityService()), get_app_container(app).override.service(AuditService, new=FakeAuditService()), @@ -716,6 +717,13 @@ def test_request_id_and_correlation_id_logging_variants( with caplog.at_level(logging.INFO): response = client.get("/patient-check/12345", headers=headers) - log_messages = [record.getMessage() for record in caplog.records] - assert any(expected_log_msg in message for message in log_messages) + request_id_logged = False + for record in caplog.records: + request_id = getattr(record, "X-Request-ID", None) + + if request_id == expected_request_id: + request_id_logged = True + break + + assert request_id_logged assert response.status_code == HTTPStatus.OK