-
Notifications
You must be signed in to change notification settings - Fork 163
refactor(BA-4378): Add _observe helper to GQLMetricMiddleware #8773
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
base: main
Are you sure you want to change the base?
Conversation
Extracted three repeated observe_request call sites in GQLMetricMiddleware.resolve() into a local _observe closure to reduce duplication. Changed `raise e` to bare `raise` for idiomatic traceback preservation. Added unit tests.
Add changelog entry documenting the extraction of the _observe helper closure in GQLMetricMiddleware.resolve() to reduce code duplication.
Remove obsolete test files for the legacy GraphQL metric middleware. These tests are no longer needed after the behavior-preserving refactoring that simplified the middleware implementation while maintaining the same functionality. Files removed: - tests/unit/manager/api/gql_legacy/BUILD - tests/unit/manager/api/gql_legacy/test_gql_metric_middleware.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Refactors the legacy GraphQL metrics middleware to reduce duplication in how request metrics are recorded during resolver execution, while preserving existing runtime behavior.
Changes:
- Extracts repeated
metric_observer.observe_request(...)calls inGQLMetricMiddleware.resolve()into a local_observe(...)helper. - Switches
raise eto bareraiseto preserve original tracebacks on exceptions. - Adds a Towncrier misc fragment documenting the refactor.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/ai/backend/manager/api/gql_legacy/schema.py |
Introduces _observe closure and updates exception re-raising for better traceback preservation. |
changes/8772.misc.md |
Adds release-note fragment describing the refactor. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Refactor the _observe closure to accept a single error parameter instead of separate error_code and success kwargs. The function now uses pattern matching to derive error_code from the error type, and the try/except blocks are consolidated into a single except BaseException handler. This reduces duplication and makes the error handling flow more straightforward.
Add comprehensive unit tests for the _observe helper method in GQLMetricMiddleware, covering sync resolver timing, error handling (both BackendAIError and generic exceptions), and anonymous operation name handling for both sync and async resolvers.
da0e628 to
33e8763
Compare
resolves #8776 (BA-4378)
Overview
Extracts three repeated
observe_requestcall sites inGQLMetricMiddleware.resolve()into a local_observeclosure that captures the common context parameters, reducing each call site to a single line. Also changesraise eto bareraisefor idiomatic traceback preservation. This is a pure refactoring with no behavior change.Problem Statement
GQLMetricMiddleware.resolve()contained three near-identicalobserve_request(...)call sites, each passing 7 keyword argumentsraise einstead of bareraiseis non-idiomatic and can alter traceback presentationChecklist: (if applicable)
ai.backend.testdocsdirectory