Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
34e233b
Add log_response_details middleware
dan-fernandes Jan 28, 2026
25b4f8a
Fix log statement, add comment
dan-fernandes Jan 28, 2026
b51e535
Aggregate response and request logs into single log
dan-fernandes Jan 28, 2026
128a1f8
Fix placement of request body consumption
dan-fernandes Jan 28, 2026
6593810
Fix logging test
dan-fernandes Jan 28, 2026
46738e1
Add docstring
dan-fernandes Jan 28, 2026
610f0a1
Make log test endpoint a POST
dan-fernandes Jan 28, 2026
92c383f
Merge branch 'main' into add-response-logging
dan-fernandes Feb 13, 2026
5d10315
Make request and reponse bodies truncateable
dan-fernandes Feb 13, 2026
b218b0f
Merge branch 'main' into add-response-logging
dan-fernandes Feb 17, 2026
32b5587
Update config schema
dan-fernandes Feb 17, 2026
1c77f71
Merge branch 'main' into add-response-logging
dan-fernandes Feb 17, 2026
9bc74a6
Merge branch 'main' into add-response-logging
dan-fernandes Feb 17, 2026
216eb86
Add alias to LoggingConfig.truncate_bodies
dan-fernandes Feb 17, 2026
cb00ae2
Update helm and config schemas
dan-fernandes Feb 17, 2026
4fb567e
Merge branch 'main' into add-response-logging
dan-fernandes Feb 20, 2026
1ce5131
Remove log body truncation
dan-fernandes Feb 20, 2026
d031fc5
Remove truncateBodies frm values
dan-fernandes Feb 20, 2026
0047217
Make log_request_details its own middleware, rather than factory
dan-fernandes Feb 20, 2026
bd87cd0
No longer log response body
dan-fernandes Feb 20, 2026
4e04598
Remove old comment
dan-fernandes Feb 20, 2026
c348162
Add port to request log
dan-fernandes Feb 20, 2026
3a09940
Add request debug log in receive
dan-fernandes Feb 20, 2026
ade3d01
Reformat debug log in request
dan-fernandes Feb 20, 2026
de2a5ee
Fix tests
dan-fernandes Feb 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/blueapi/service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
Response,
status,
)
from fastapi.datastructures import Address
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from fastapi.responses import RedirectResponse, StreamingResponse
from fastapi.security import OAuth2AuthorizationCodeBearer
from observability_utils.tracing import (
add_span_attributes,
Expand Down Expand Up @@ -576,14 +577,25 @@ async def add_api_version_header(


async def log_request_details(
request: Request, call_next: Callable[[Request], Awaitable[Response]]
request: Request, call_next: Callable[[Request], Awaitable[StreamingResponse]]
) -> Response:
msg = f"method: {request.method} url: {request.url} body: {await request.body()}"
"""Middleware to log all request's host, method, path, status and request and
body"""
request_body = await request.body()
client = request.client or Address("Unknown", -1)
log_message = f"{client.host}:{client.port} {request.method} {request.url.path}"
extra = {
"request_body": request_body,
}
LOGGER.debug(log_message, extra=extra)

response = await call_next(request)
log_message += f" {response.status_code}"
if request.url.path == "/healthz":
LOGGER.debug(msg)
LOGGER.debug(log_message, extra=extra)
else:
LOGGER.info(msg)
response = await call_next(request)
LOGGER.info(log_message, extra=extra)

return response


Expand Down
16 changes: 13 additions & 3 deletions tests/unit_tests/service/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,26 @@ async def test_log_request_details():
app = FastAPI()
app.middleware("http")(log_request_details)

@app.get("/")
@app.post("/")
async def root():
return {"message": "Hello World"}

client = TestClient(app)
response = client.get("/")
response = client.post("/", content="foo")

assert response.status_code == 200
logger.debug.assert_called_once_with(
"testclient:50000 POST /",
extra={
"request_body": b"foo",
},
)

logger.info.assert_called_once_with(
"method: GET url: http://testserver/ body: b''"
"testclient:50000 POST / 200",
extra={
"request_body": b"foo",
},
)


Expand Down