Beispiel #1
0
async def request_id_middleware(request: BaseRequest,
                                handler: Callable) -> StreamResponse:
    request_id_token = set_request_id(request.headers.get("X-Request-Id"))
    request_id = get_request_id()
    try:
        if sentry_sdk:
            with configure_scope() as scope:
                scope.set_tag("request_id", request_id)
        response = await handler(request)
        response.headers["X-Request-Id"] = get_request_id()
        return response
    finally:
        reset_request_id(request_id_token)
Beispiel #2
0
def test_request_id_generated_if_bad_given(uuid4_mock, caplog):
    set_request_id("bad_request_id")
    assert get_request_id() == uuid4_mock
    assert "Received invalid request id" in caplog.text
Beispiel #3
0
def test_request_id_generated_and_saved_if_not_given(uuid4_mock):
    set_request_id()
    assert get_request_id() == uuid4_mock
Beispiel #4
0
def test_request_id_saved_if_given():
    request_id = uuid4().hex
    set_request_id(request_id)
    assert get_request_id() == request_id
Beispiel #5
0
 def filter(self, record: logging.LogRecord) -> bool:
     record.__dict__.update({"request_id": get_request_id()})
     return True