Record streaming attempt and front-end metrics when PartialResultSet.last=True is observed

Open
#18,656 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@olavloite is already working on this.

Since Oct 13, 2026.

Assessment

This issue has not been assessed yet.

Description

api: spanner
Description

In google-cloud-spanner, streaming queries that receive PartialResultSet.last = True hand off the underlying response iterator to _GLOBAL_STREAM_DRAINER to drain trailing metadata to EOF asynchronously on a background worker thread (in snapshot.py).

However, _StreamingResponseWrapper in metrics_interceptor.py currently defers recording attempt completion (attempt_latencies) and front-end timing metrics (gfe_latencies, afe_latencies) until StopIteration is raised:
https://github.com/googleapis/google-cloud-python/blob/main/packages/google-cloud-spanner/google/cloud/spanner_v1/metrics/metrics_interceptor.py

This causes two issues:

  1. Metric Timing Inaccuracy: Attempt latency should measure when the operation actually completes from an outside observer's perspective (i.e. when PartialResultSet.last = True is received by the caller), rather than including time spent waiting in the background drainer queue or consuming trailing stream metadata.
  2. Race Condition with Metrics Readers: Callers that complete a query and immediately collect or inspect metrics observe missing attempt_latencies, gfe_latencies, and afe_latencies because the background drainer thread has not yet reached EOF.
Proposed Solution

The client should measure attempt completion and record front-end metrics as soon as PartialResultSet.last == True is observed on the stream, rather than waiting for StopIteration during background stream draining.

Dominant language
Python
Stars
5.4k
Forks
1.8k
Avg merge
2d 7h
Merged PRs (30d)
173

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from googleapis/google-cloud-python

All issues in googleapis/google-cloud-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.