Skip to content

fix(spanner): record streaming operation completion when stream finishes - #18704

Open
sinhasubham wants to merge 1 commit into
mainfrom
fix-operation-latency
Open

sinhasubham wants to merge 1 commit into
mainfrom
fix-operation-latency

Conversation

@sinhasubham

@sinhasubham sinhasubham commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes an issue where operation_latencies and operation_count client metrics were not being emitted for streaming queries (ExecuteStreamingSql).

Key Changes

  • Operation Completion Lifecycle: Wrapped stream consumption in _restart_on_unavailable in a try...finally block (sync and async in snapshot.py) to guarantee metrics_tracer.record_operation_completion() is called when streams finish normally, exit early via break, or raise unhandled exceptions.
  • Stream Termination Handling: Updated _StreamingResponseWrapper and _AsyncStreamingResponseWrapper in metrics_interceptor.py to trigger attempt metric recording when encountering last=True on partial result sets.
  • Status Integrity: Preserved unpopulated operation status convention rather than defaulting to "OK" upon caught gRPC stream exceptions.
  • Unit Test Coverage: Added comprehensive test coverage for early break, generator cancellation, and last=True chunk handling across both sync and async paths.

Validation

Verified using live test runs on project span-cloud-testing (instance=ranjithmrtest, database=gsql_enterprise_final) confirming that operation_latencies and attempt_latencies are actively emitted to Cloud Monitoring for ExecuteStreamingSql.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the Spanner client's metrics tracking to ensure that streaming operation latencies are recorded immediately upon stream completion and exclude consumer processing delays. This is achieved by tracking stream completion in both synchronous and asynchronous snapshot iterators and recording metrics accordingly. However, the reviewer identified a critical issue in metrics_interceptor.py where the code attempts to check for a last attribute on the yielded stream items. Since the production PartialResultSet protobuf messages do not contain a last field, this check will always evaluate to False and act as dead code. It is recommended to remove these redundant checks.

@sinhasubham

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the Spanner client to record operation completion metrics immediately when the stream finishes or when a PartialResultSet with last=True is observed, rather than waiting for background stream draining. The feedback suggests simplifying the boolean checks in metrics_interceptor.py by removing the explicit 'is True' comparisons to align with PEP 8 guidelines.

@sinhasubham
sinhasubham force-pushed the fix-operation-latency branch from 1b8a925 to 31b7a08 Compare October 27, 2026 06:44
…empt latency upon stream completion

Wait until the streaming response completes (either via PartialResultSet.last=True
or iterator exhaustion/closure) before recording operation completion and attempt
completion metrics in _restart_on_unavailable and _StreamingResponseWrapper /
_AsyncStreamingResponseWrapper.

Fixes #18656
@sinhasubham
sinhasubham force-pushed the fix-operation-latency branch from 31b7a08 to e37b728 Compare October 27, 2026 10:29
@sinhasubham
sinhasubham marked this pull request as ready for review October 27, 2026 10:47
@sinhasubham
sinhasubham requested a review from a team as a code owner October 27, 2026 10:47
@sinhasubham
sinhasubham requested a review from rahul2393 October 27, 2026 10:53

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant