Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 13 additions & 2 deletions apps/demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
INSTANCE = os.getenv("INSTANCE", ROLE)

async def health(request):
return web.json_response({"status": "ok", "service": INSTANCE})
req_id = request.headers.get("X-Request-ID")
data = {"status": "ok", "service": INSTANCE}
if req_id:
data["requestId"] = req_id
headers = {"X-Request-ID": req_id} if req_id else {}
return web.json_response(data, headers=headers)

async def index(request):
req_id = request.headers.get("X-Request-ID")
headers = {"X-Request-ID": req_id} if req_id else {}
if ROLE == "web":
return web.Response(
text=("<!doctype html><html><head><meta charset='utf-8'>"
Expand All @@ -17,8 +24,12 @@ async def index(request):
"<p>Try <a href='/api/'>/api/</a> and WebSocket /ws/.</p>"
"</body></html>"),
content_type="text/html",
headers=headers,
)
return web.json_response({"service": ROLE, "instance": INSTANCE, "path": request.path})
data = {"service": ROLE, "instance": INSTANCE, "path": request.path}
if req_id:
data["requestId"] = req_id
return web.json_response(data, headers=headers)

async def ws_handler(request):
if ROLE != "realtime":
Expand Down
4 changes: 4 additions & 0 deletions nginx/snippets/proxy.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Request-ID $request_id;
proxy_connect_timeout 3s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
proxy_set_header Connection "";
proxy_hide_header X-Powered-By;
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_next_upstream_tries 2;
11 changes: 11 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ def test_proxy_forwarding_invariants(self):
self.assertIn('proxy_set_header X-Forwarded-Proto $scheme;', proxy)
self.assertIn('proxy_set_header X-Request-ID $request_id;', proxy)
self.assertIn('proxy_connect_timeout 3s;', proxy)
self.assertIn('proxy_read_timeout 30s;', proxy)
self.assertIn('proxy_send_timeout 30s;', proxy)
self.assertIn('proxy_set_header Connection "";', proxy)
self.assertIn('proxy_hide_header X-Powered-By;', proxy)
self.assertIn('proxy_next_upstream error timeout http_502 http_503 http_504;', proxy)
self.assertIn('proxy_next_upstream_tries 2;', proxy)

def test_rate_limiting_and_http_status(self):
nginx_cfg = load('nginx/nginx.conf')
Expand Down Expand Up @@ -124,5 +129,11 @@ def test_gzip_compression_and_buffer_tuning(self):
self.assertIn('client_body_buffer_size 128k;', cfg)
self.assertIn('large_client_header_buffers 4 8k;', cfg)

def test_trace_id_propagation_in_demo_app(self):
source = load('apps/demo/app.py')
self.assertIn('request.headers.get("X-Request-ID")', source)
self.assertIn('data["requestId"] = req_id', source)
self.assertIn('"X-Request-ID": req_id', source)

if __name__ == '__main__':
unittest.main()
Loading