Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[serve] Reduce cardinality for the route metric tag #48290

Merged
merged 19 commits into from
Oct 28, 2024

Conversation

edoakes
Copy link
Contributor

@edoakes edoakes commented Oct 28, 2024

Why are these changes needed?

Modifies the route attribute used for logs and metrics to limit its cardinality (see linked issue).

After this change, the behavior will be:

  • If the user is not using FastAPI, the route will always be the route_prefix.
  • If the user is using FastAPI, the route will be the route_prefix + the matched route string.

Take the following example:

@serve.deployment
@serve.ingress(fastapi_app)
class A:
    @fastapi_app.get("/{user_id}")
    def dynamic_subpath(self, user_id: str) -> str:
        return ...

serve.run(A.bind(), route_prefix="/prefix")

Previously, this could have infinite cardinality in the metrics tags (a unique tag value for each user). With this change, the tag would instead be: "/prefix/{user_id}".

The "not found" response has also been modified to not include a route tag.

TODO:

  • Add unit tests for get_asgi_route_name
  • Add integration tests to test_metrics.py
  • Add tests for the replica request context

Related issue number

Closes #47999

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
@edoakes edoakes changed the title [WIP][serve] Reduce cardinality for the route metric tag [serve] Reduce cardinality for the route metric tag Oct 28, 2024
@edoakes edoakes marked this pull request as ready for review October 28, 2024 14:36
@edoakes edoakes added the go add ONLY when ready to merge, run all tests label Oct 28, 2024
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Copy link
Contributor

@GeneDer GeneDer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits, but overall LGTM!

only Python primitive types. Vanilla `pickle` is much faster than cloudpickle.
"""
self.asgi_scope = pickle.loads(state["pickled_asgi_scope"])
self.receive_asgi_messages = state["receive_asgi_messages"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocker: We can probably create a named tuple for state object to ensure those required keys exist and accessible.

# NOTE(edoakes): we use the route_prefix instead of the full HTTP path
# for logs & metrics to avoid high cardinality.
# See: https://github.com/ray-project/ray/issues/47999
logs_and_metrics_route = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocker: We can probably refactor this into ProxyRequest and implement them in ASGIProxyRequest and gRPCProxyRequest differently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I was highly confused at the fact that our "route" was just an app name for gRPC. the "method" field also seems to be getting overwritten to something else that actually looks like a route..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I think it depends on the context. HTTP we have methods such as "GET", "POST"...etc. gRPC has concept of service methods so we used in those in metrics/ logs. Also, in gRPC we use the app name to route so that's why the equivalent route_path was set to app names.

python/ray/serve/_private/vendored/get_asgi_route_name.py Outdated Show resolved Hide resolved
python/ray/serve/_private/vendored/get_asgi_route_name.py Outdated Show resolved Hide resolved
@edoakes edoakes merged commit 6c697b3 into ray-project:master Oct 28, 2024
5 checks passed
Jay-ju pushed a commit to Jay-ju/ray that referenced this pull request Nov 5, 2024
)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

Modifies the `route` attribute used for logs and metrics to limit its
cardinality (see linked issue).

After this change, the behavior will be:

- If the user is not using FastAPI, the `route` will always be the
`route_prefix`.
- If the user is using FastAPI, the `route` will be the `route_prefix` +
the matched route string.

Take the following example:
```
@serve.deployment
@serve.ingress(fastapi_app)
class A:
    @fastapi_app.get("/{user_id}")
    def dynamic_subpath(self, user_id: str) -> str:
        return ...

serve.run(A.bind(), route_prefix="/prefix")
```

Previously, this could have infinite cardinality in the metrics tags (a
unique tag value for each user). With this change, the tag would instead
be: `"/prefix/{user_id}"`.

The "not found" response has also been modified to *not* include a
`route` tag.

TODO:

- [x] Add unit tests for `get_asgi_route_name`
- [x] Add integration tests to `test_metrics.py`
- [x] Add tests for the replica request context

## Related issue number

Closes ray-project#47999

## Checks

- [ ] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [ ] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [ ] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Edward Oakes <[email protected]>
JP-sDEV pushed a commit to JP-sDEV/ray that referenced this pull request Nov 14, 2024
)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

Modifies the `route` attribute used for logs and metrics to limit its
cardinality (see linked issue).

After this change, the behavior will be:

- If the user is not using FastAPI, the `route` will always be the
`route_prefix`.
- If the user is using FastAPI, the `route` will be the `route_prefix` +
the matched route string.

Take the following example:
```
@serve.deployment
@serve.ingress(fastapi_app)
class A:
    @fastapi_app.get("/{user_id}")
    def dynamic_subpath(self, user_id: str) -> str:
        return ...

serve.run(A.bind(), route_prefix="/prefix")
```

Previously, this could have infinite cardinality in the metrics tags (a
unique tag value for each user). With this change, the tag would instead
be: `"/prefix/{user_id}"`.

The "not found" response has also been modified to *not* include a
`route` tag.

TODO:

- [x] Add unit tests for `get_asgi_route_name`
- [x] Add integration tests to `test_metrics.py`
- [x] Add tests for the replica request context

## Related issue number

Closes ray-project#47999

## Checks

- [ ] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [ ] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [ ] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Edward Oakes <[email protected]>
mohitjain2504 pushed a commit to mohitjain2504/ray that referenced this pull request Nov 15, 2024
)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

Modifies the `route` attribute used for logs and metrics to limit its
cardinality (see linked issue).

After this change, the behavior will be:

- If the user is not using FastAPI, the `route` will always be the
`route_prefix`.
- If the user is using FastAPI, the `route` will be the `route_prefix` +
the matched route string.

Take the following example:
```
@serve.deployment
@serve.ingress(fastapi_app)
class A:
    @fastapi_app.get("/{user_id}")
    def dynamic_subpath(self, user_id: str) -> str:
        return ...

serve.run(A.bind(), route_prefix="/prefix")
```

Previously, this could have infinite cardinality in the metrics tags (a
unique tag value for each user). With this change, the tag would instead
be: `"/prefix/{user_id}"`.

The "not found" response has also been modified to *not* include a
`route` tag.

TODO:

- [x] Add unit tests for `get_asgi_route_name`
- [x] Add integration tests to `test_metrics.py`
- [x] Add tests for the replica request context

## Related issue number

Closes ray-project#47999

## Checks

- [ ] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [ ] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [ ] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: mohitjain2504 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[serve] High cardinality for metrics that include the HTTP route
2 participants