Skip to content

Commit

Permalink
grpc_server: use x-correlation-id as request-id when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrifiro committed Sep 16, 2024
1 parent a76401d commit f7c5ee7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/vllm_tgis_adapter/grpc/grpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,17 @@ def _convert_output( # noqa: PLR0913
return response

@staticmethod
def request_id(context: ServicerContext) -> str: # noqa: ARG004
return uuid.uuid4().hex
def request_id(context: ServicerContext) -> str:
metadata = context.invocation_metadata()
if not metadata:
return uuid.uuid4().hex

correlation_id = dict(metadata).get("x-correlation-id")

if not correlation_id:
return uuid.uuid4().hex

return correlation_id

async def _validate_and_convert_params(
self,
Expand Down
27 changes: 27 additions & 0 deletions tests/test_grpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,30 @@ def test_lora_request(grpc_client, lora_adapter_name):
response = grpc_client.make_request("hello", adapter_id=lora_adapter_name)

assert response.text


def test_request_id(grpc_client, mocker):
from vllm_tgis_adapter.grpc.grpc_server import TextGenerationService, uuid

spy = mocker.spy(TextGenerationService, "request_id")
response = grpc_client.make_request(
"The answer to life the universe and everything is ",
metadata=[("x-correlation-id", "dummy-correlation-id")],
)
assert response.text

spy.assert_called_once()
assert spy.spy_return == "dummy-correlation-id"

spy.reset_mock()

request_id = uuid.uuid4()
mocker.patch.object(uuid, "uuid4", return_value=request_id)

response = grpc_client.make_request(
"The answer to life the universe and everything is ",
)
assert response.text

spy.assert_called_once()
assert spy.spy_return == request_id.hex
2 changes: 2 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def make_request(
model_id: str | None = None,
max_new_tokens: int = 10,
adapter_id: str | None = None,
metadata: list[tuple[str, str]] | None = None,
) -> GenerationResponse | Sequence[GenerationResponse]:
# assert model_id # FIXME: is model_id required?

Expand All @@ -143,6 +144,7 @@ def make_request(

response = self.generation_service_stub.Generate(
request=request,
metadata=metadata,
)

if single_request:
Expand Down

0 comments on commit f7c5ee7

Please sign in to comment.