Skip to content

fix(spanner): record streaming built-in metrics per result stream - #20596

Merged
rahul2393 merged 1 commit into
mainfrom
fm/spanner-rowiter-metrics
Sep 29, 2026
Merged

rahul2393 merged 1 commit into
mainfrom
fm/spanner-rowiter-metrics

Conversation

@rahul2393

Copy link
Copy Markdown
Contributor

RowIterator recorded a built-in metrics operation on each call to Next that opened a stream. Operation latency was therefore about the time to the first result, a resumed stream was recorded as a second operation, and a non-retryable error after the first rows was never recorded as an operation. It also created a metrics tracer on every call to Next.

Record one operation per result stream instead, matching the published definitions of the client-side metrics:
https://cloud.google.com/spanner/docs/client-side-metrics-descriptions

  • The operation starts when the first stream is opened and ends when Next returns iterator.Done (OK) or an error (its status), or when Stop is called before that (OK). Its latency includes retries, backoff and the application time between calls to Next. Iterators that are neither read to the end nor stopped record nothing.
  • Every stream that is opened or resumed is an attempt of that operation, from its open until the stream ends or fails. The attempt count is still recorded when the operation ends.
  • The tracer is created when the first stream is opened and only when built-in metrics are enabled, so Next does no per-row metrics work.
  • The stream metrics interceptor sets the method before creating the stream, so a stream that fails to open is recorded as an attempt of its method. Attempts that fail before the RPC is sent are not recorded.

RowIterator recorded a built-in metrics operation on each call to Next
that opened a stream. Operation latency was therefore about the time to
the first result, a resumed stream was recorded as a second operation,
and a non-retryable error after the first rows was never recorded as an
operation. It also created a metrics tracer on every call to Next.

Record one operation per result stream instead, matching the published
definitions of the client-side metrics:
https://cloud.google.com/spanner/docs/client-side-metrics-descriptions
https://cloud.google.com/bigtable/docs/client-side-metrics-descriptions

- The operation starts when the first stream is opened and ends when
  Next returns iterator.Done (OK) or an error (its status), or when
  Stop is called before that (OK). Its latency includes retries,
  backoff and the application time between calls to Next. Iterators
  that are neither read to the end nor stopped record nothing.
- Every stream that is opened or resumed is an attempt of that
  operation, from its open until the stream ends or fails. The attempt
  count is still recorded when the operation ends.
- The tracer is created when the first stream is opened and only when
  built-in metrics are enabled, so Next does no per-row metrics work.
- The stream metrics interceptor sets the method before creating the
  stream, so a stream that fails to open is recorded as an attempt of
  its method. Attempts that fail before the RPC is sent are not
  recorded.

Dashboards will show fewer and longer streaming operations, and
mid-stream failures are now recorded as failed operations.

With built-in metrics disabled, streaming calls no longer carry a
metrics tracer, so the server timing span attributes of traced
streaming calls depend on the change that records them without a
tracer, which must land first or together with this one.
@rahul2393
rahul2393 requested review from a team as code owners September 29, 2026 10:02
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label Sep 29, 2026

@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 built-in metrics tracking for Spanner stream operations to ensure that attempts failing before an RPC is successfully initiated are correctly recorded. It moves the metrics tracer lifecycle management from RowIterator into resumableStreamDecoder, introducing startAttempt, endAttempt, and finishOperation helper methods. Additionally, it updates the gRPC stream interceptor to flag when an RPC has started and adds comprehensive unit tests to verify metrics behavior, memory allocations, and error scenarios. I have no feedback to provide as there are no review comments.

@olavloite olavloite 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.

(We should probably consider fixing this for BatchWrite as well in a follow-up PR)

@rahul2393
rahul2393 merged commit be75f7e into main Sep 29, 2026
17 checks passed
@rahul2393
rahul2393 deleted the fm/spanner-rowiter-metrics branch September 29, 2026 11:41
rahul2393 pushed a commit that referenced this pull request Oct 7, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.96.0](spanner/v1.95.1...spanner/v1.96.0)
(2026-10-07)


### Features

* **all:** Update supported go versions
([#20520](#20520))
([90413d3](90413d3))
* **spanner:** Add experimental vtprotobuf decoding with
ExperimentalBorrowRows under the spanner_vtproto build tag
([#20642](#20642))
([fdbd4f4](fdbd4f4))


### Bug Fixes

* **spanner:** Keep DCP transaction on its channel while it drains
([#20618](#20618))
([93dfc20](93dfc20))
* **spanner:** Prioritize leader replica for read-write transactions in
location-aware routing
([#20620](#20620))
([a524d66](a524d66))
* **spanner:** Record streaming built-in metrics per result stream
([#20596](#20596))
([be75f7e](be75f7e))
* **spanner:** Retry and resume BatchWrite streams on UNAVAILABLE
([#20623](#20623))
([a302277](a302277))


### Performance Improvements

* **spanner:** Skip builtin metrics tracer allocation when metrics are
disabled
([#20582](#20582))
([9971a0c](9971a0c))
* **spanner:** Skip reading response headers when no latency sink is
enabled
([#20583](#20583))
([3dcc2c3](3dcc2c3))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants