| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Optimize hot-path execution in the Spanner client's built-in metrics
subsystem and resolve subtle regex boundary and concurrency issues:
- Correctness & Regex Parsing:
- Add negative lookbehind `(?<![a-zA-Z0-9_-])` to GFE and AFE timing
regexes to prevent false-positive matches (e.g. `safe; dur=55`).
- Guard regex evaluations in `extract_front_end_latencies` with fast string
containment checks (`"gfet4t7" in text`, `"afe" in text`).
- Extract helper `_extract_metric_latency` and flatten header detection.
- Caching & Allocations:
- Add bounded LRU cache (`maxsize=128`) for resource path parsing,
yielding a ~9x speedup matching the Java client's caching strategy.
- Add bounded LRU cache (`maxsize=64`) for RPC method name formatting.
- Introduce `_ObservableDict` to achieve zero-allocation steady-state
OpenTelemetry attribute caching with O(1) invalidation on mutation.
- Consolidate RPC attempt preparation in `MetricsInterceptor._prepare_attempt`.
- Concurrency & Resource Safety:
- Extract `_BaseAsyncResponseWrapper` and synchronize `_metrics_recorded`
via `threading.Lock` across `cancel()`, `__del__()`, and `_record_metrics()`.
- Safely close unawaited initial metadata coroutines on cancellation.
- Guarantee ContextVar token reset in `MetricsCapture.__exit__` via `finally`.
- Harden `_safe_decode_utf8` to return `""` when passed `None`.
There was a problem hiding this comment.
This pull request optimizes and hardens the Spanner metrics collection system. Key changes include caching resource path parsing and method name formatting using lru_cache, introducing thread-safe locking and cancellation handling in the response wrappers, caching OpenTelemetry attributes with an _ObservableDict to invalidate the cache on modifications, and refactoring metadata parsing to safely handle various formats. Extensive unit tests have been added to cover these optimizations and edge cases. There are no review comments to address, and I have no feedback to provide.
Sorry, something went wrong.
|
/gemini review |
Sorry, something went wrong.
There was a problem hiding this comment.
This pull request optimizes metrics collection in the Google Cloud Spanner client by introducing LRU caching for resource path parsing and method name formatting, adding thread-safe locking to response wrappers, and implementing an attribute caching mechanism in MetricsTracer using a custom _ObservableDict. The feedback highlights two important improvements: catching OverflowError alongside ValueError when parsing latency values to prevent potential crashes on extremely large numbers, and overriding the in-place union operator (__ior__) in _ObservableDict to ensure the cache invalidation callback is consistently triggered.
Sorry, something went wrong.
|
_async_intercept detects streams with hasattr(response, "anext"), but grpc.aio UnaryStreamCall/StreamStreamCall only define aiter. Real ExecuteStreamingSql/StreamingRead calls are wrapped in _AsyncUnaryResponseWrapper, so no metrics are recorded when the stream ends. With this PR's del change, every successful async stream is now recorded as CANCELLED when it is garbage-collected (it was OK before). The tests miss this because their mocks define anext |
Sorry, something went wrong.
@sinhasubham This is not a problem that is introduced in this PR, but an existing bug on main that should not be fixed here, but rather in a separate PR. I've opened #18581 for that. Setting the status to Cancelled when __del__ is called for a stream that has not finished is correct. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Optimize hot-path execution in the Spanner client's built-in metrics subsystem and resolve subtle regex boundary and concurrency issues:
Correctness & Regex Parsing:
Caching & Allocations:
Concurrency & Resource Safety: