Skip to content

Conversation

@hhoikoo
Copy link
Member

@hhoikoo hhoikoo commented Feb 11, 2026

resolves #8776 (BA-4378)

Overview

Extracts three repeated observe_request call sites in GQLMetricMiddleware.resolve() into a local _observe closure that captures the common context parameters, reducing each call site to a single line. Also changes raise e to bare raise for idiomatic traceback preservation. This is a pure refactoring with no behavior change.

Problem Statement

  • GQLMetricMiddleware.resolve() contained three near-identical observe_request(...) call sites, each passing 7 keyword arguments
  • The repetition made the method hard to read and subsequent changes (e.g., BA-4299 async timing fix) produce unnecessarily large diffs
  • raise e instead of bare raise is non-idiomatic and can alter traceback presentation

Checklist: (if applicable)

  • Milestone metadata specifying the target backport version
  • Mention to the original issue
  • Installer updates including:
    • Fixtures for db schema changes
    • New mandatory config options
  • Update of end-to-end CLI integration tests in ai.backend.test
  • API server-client counterparts (e.g., manager API -> client SDK)
  • Test case(s) to:
    • Demonstrate the difference of before/after
    • Demonstrate the flow of abstract/conceptual models with a concrete implementation
  • Documentation
    • Contents in the docs directory
    • docstrings in public interfaces and type annotations

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
Copilot AI review requested due to automatic review settings February 11, 2026 08:52
@github-actions github-actions bot added size:M 30~100 LoC comp:manager Related to Manager component labels Feb 11, 2026
@hhoikoo hhoikoo marked this pull request as draft February 11, 2026 08:54
Copy link
Contributor

Copilot AI left a 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 in GQLMetricMiddleware.resolve() into a local _observe(...) helper.
  • Switches raise e to bare raise to 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.
@github-actions github-actions bot added size:L 100~500 LoC and removed size:M 30~100 LoC labels Feb 11, 2026
@hhoikoo hhoikoo changed the title refactor(BA-4375): Add _observe helper to GQLMetricMiddleware refactor(BA-4378): Add _observe helper to GQLMetricMiddleware Feb 11, 2026
@hhoikoo hhoikoo marked this pull request as ready for review February 12, 2026 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:manager Related to Manager component size:L 100~500 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract observe_request helper in GQLMetricMiddleware

1 participant