Skip to content

feat(storage): add OpenTelemetry standard metrics for HTTP JSON transport - #18940

Draft
shradhakatyal wants to merge 7 commits into
googleapis:mainfrom
shradhakatyal:feat/gcs-otel-http-transport-metrics
Draft

shradhakatyal wants to merge 7 commits into
googleapis:mainfrom
shradhakatyal:feat/gcs-otel-http-transport-metrics

Conversation

@shradhakatyal

@shradhakatyal shradhakatyal commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds OpenTelemetry standard metrics support for the core HTTP JSON transport and client lifecycle in google-cloud-storage.

Stack Overview (review isolated commit 04b2dbd2f09 for this PR):

Key Changes

  • Client Lifecycle (client.py): Adds enable_metrics, enable_debug_metrics, and meter_provider parameters to Client.__init__, initializes _MetricsRecorder, and calls _metrics_recorder.shutdown() in Client.close().
  • HTTP JSON Transport (_http.py): Instruments Connection.api_request for per-attempt http.client.request.duration, gcp.storage.client.attempts, gcp.storage.client.ttfb, gcp.storage.client.retries, and gcp.storage.client.errors with bounded url.template normalization.
  • Operation & Pagination Helpers (_helpers.py): Adds _start_storage_operation, _PropertyMixin metrics hooks, and _InstrumentedHTTPIterator for lazy per-page pagination metrics.
  • Batch Queuing & Internal Cache Suppression (batch.py, _bucket_metadata_cache.py): Marks Batch._IS_BATCH = True to skip per-attempt metrics on in-memory queued futures and suppresses internal background BucketMetadataCache lookups from emitting user-facing metrics.

…d helpers

Implement core OpenTelemetry standard metrics infrastructure for GCS:
- Add _MetricsRecorder with the 9 standard metric instruments
- Add custom latency (165 buckets) and byte-size (45 buckets) histogram views
- Add classify_error_type for low-cardinality error.type mapping
- Add normalize_url_template for bounded HTTP url.template routing
- Add start_operation ContextVar for outermost logical operation tracking and nested call suppression

[Generated-by: AI]
- Add gcp.storage.client.retries counter and rename TTFB metric to gcp.storage.client.ttfb
- Remove gcp.client.repo from _COMMON_ATTRIBUTES
- Update _build_size_buckets to generate 48 buckets matching Go and Java SDKs
- Align per-metric attributes across logical operation, RPC/HTTP attempt, error, retry, and TTFB recorders

[Generated-by: AI]
…fy CONNECTION_ERROR

- Scope histogram View registrations to meter_name="google.cloud.storage"
- Guard normalize_url_template and _extract_server_address against malformed URIs and gRPC authority schemes
- Consolidate CONNECTION_REFUSED, CONNECTION_RESET, and CONNECTIVITY error labels into CONNECTION_ERROR

[Generated-by: AI]
…port

- Wire _MetricsRecorder lifecycle, enable_metrics, and meter_provider into Client
- Instrument Connection.api_request for per-attempt HTTP duration, TTFB, retries, and errors
- Add _start_storage_operation, _PropertyMixin, and lazy HTTPIterator page metrics helpers
- Skip per-attempt HTTP metrics while queuing sub-requests into Batch
- Suppress internal background BucketMetadataCache lookups from emitting user-facing metrics

@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 implements comprehensive OpenTelemetry metrics support for the Google Cloud Storage Python client. It introduces a _MetricsRecorder to track standard GCS metrics such as logical operations, transport attempts, retries, TTFB, request/response body sizes, and errors. Additionally, it adds context managers for suppressing metrics during internal background tasks, route normalization to prevent cardinality explosion, and canonical error classification, along with comprehensive unit tests. There are no review comments, so I have no feedback to provide.

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