forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Serve] Expose serve request id from http request/resp (ray-project#3…
…5789) User can inject request id: ``` @serve.deployment class Model: def __call__(self) -> int: return 1 serve.run(Model.bind()) resp = requests.get("http://localhost:8000", headers={"RAY_SERVE_REQUEST_ID": "123-234"}) ``` Signed-off-by: e428265 <arvind.chandramouli@lmco.com>
- Loading branch information
1 parent
890880c
commit 8b0e067
Showing
4 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import pytest | ||
import requests | ||
from fastapi import FastAPI | ||
import starlette | ||
|
||
import ray | ||
from ray import serve | ||
from ray.serve._private.constants import RAY_SERVE_REQUEST_ID_HEADER | ||
|
||
|
||
def test_request_id_header_by_default(ray_shutdown): | ||
"""Test that a request_id is generated by default and returned as a header.""" | ||
|
||
@serve.deployment | ||
class Model: | ||
def __call__(self): | ||
request_id = ray.serve.context._serve_request_context.get().request_id | ||
return request_id | ||
|
||
serve.run(Model.bind()) | ||
resp = requests.get("http://localhost:8000") | ||
assert resp.status_code == 200 | ||
assert RAY_SERVE_REQUEST_ID_HEADER in resp.headers | ||
assert resp.text == resp.headers[RAY_SERVE_REQUEST_ID_HEADER] | ||
|
||
|
||
@pytest.mark.parametrize("deploy_type", ["basic", "fastapi", "starlette_resp"]) | ||
def test_user_provided_request_id_header(ray_shutdown, deploy_type): | ||
"""Test that a user-provided request_id is propagated to the | ||
replica and returned as a header.""" | ||
|
||
if deploy_type == "fastapi": | ||
app = FastAPI() | ||
|
||
@serve.deployment | ||
@serve.ingress(app) | ||
class Model: | ||
@app.get("/") | ||
def say_hi(self) -> int: | ||
request_id = ray.serve.context._serve_request_context.get().request_id | ||
assert request_id == "123-234" | ||
return 1 | ||
|
||
elif deploy_type == "basic": | ||
|
||
@serve.deployment | ||
class Model: | ||
def __call__(self) -> int: | ||
request_id = ray.serve.context._serve_request_context.get().request_id | ||
assert request_id == "123-234" | ||
return 1 | ||
|
||
else: | ||
|
||
@serve.deployment | ||
class Model: | ||
def __call__(self) -> int: | ||
request_id = ray.serve.context._serve_request_context.get().request_id | ||
assert request_id == "123-234" | ||
return starlette.responses.Response("1", media_type="application/json") | ||
|
||
serve.run(Model.bind()) | ||
|
||
resp = requests.get( | ||
"http://localhost:8000", headers={RAY_SERVE_REQUEST_ID_HEADER: "123-234"} | ||
) | ||
assert resp.status_code == 200 | ||
assert resp.json() == 1 | ||
assert RAY_SERVE_REQUEST_ID_HEADER in resp.headers | ||
assert resp.headers[RAY_SERVE_REQUEST_ID_HEADER] == "123-234" | ||
|
||
|
||
if __name__ == "__main__": | ||
import sys | ||
|
||
sys.exit(pytest.main(["-v", "-s", __file__])) |