Skip to content

feat(storage): integrate OpenTelemetry metrics gating across clients and transfer_manager - #18577

Open
shradhakatyal wants to merge 6 commits into
googleapis:mainfrom
shradhakatyal:feat/gcs-otel-client-integration
Open

shradhakatyal wants to merge 6 commits into
googleapis:mainfrom
shradhakatyal:feat/gcs-otel-client-integration

Conversation

@shradhakatyal

Copy link
Copy Markdown
Contributor

Integrate the OpenTelemetry metrics gating configuration across Google Cloud Storage client classes and transfer_manager.

  • Add enable_metrics and enable_advanced_metrics parameters to Client, GrpcClient, and AsyncGrpcClient.
  • Expose metrics_enabled and advanced_metrics_enabled properties across all three client classes.
  • Preserve enable_metrics and enable_advanced_metrics across multi-process boundaries in transfer_manager (_reduce_client and _LazyClient).
  • Add unit tests in test_client.py, test_grpc_client.py, test_async_grpc_client.py, and test_transfer_manager.py.

Note

Stacked on top of #18407. To review only the changes introduced in this PR before #18407 merges, view commit 7ec58e87d3e.

- Add _opentelemetry_metrics.py with internal dev gate and environment variable parsing
- Implement is_metrics_enabled and is_advanced_metrics_enabled evaluation helpers
- Provide get_common_attributes and get_meter helpers for OpenTelemetry metrics
- Add comprehensive unit tests in test__opentelemetry_metrics.py
- Set _ENABLE_METRICS_DEV_GATE to True in test_opentelemetry_import_error_on_load
- Wrap importlib.reload(_opentelemetry_metrics) cleanup in a try...finally block
…th OTEL convention

- Rename GCP_STORAGE_PYTHON_ENABLE_DEBUG_METRICS to GCP_STORAGE_PYTHON_ENABLE_OTEL_DEBUG_METRICS
- Rename ENABLE_DEBUG_METRICS_ENV_VAR constant to ENABLE_OTEL_DEBUG_METRICS_ENV_VAR
- Update corresponding unit tests in test__opentelemetry_metrics.py
@shradhakatyal
shradhakatyal requested a review from a team as a code owner October 6, 2026 08:39

@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 introduces OpenTelemetry metrics support to the Google Cloud Storage (GCS) Python client. It adds a new _opentelemetry_metrics module to manage metrics gating, environment variable configuration, and dependency checks. The Client, GrpcClient, and AsyncGrpcClient classes are updated to accept enable_metrics and enable_advanced_metrics parameters, and transfer_manager.py is updated to support pickling these new properties. Feedback on the pull request suggests improving the robustness of client unpickling in transfer_manager.py by dynamically mapping extra positional arguments to keyword arguments when len(args) > 6, preventing potential TypeError exceptions during version mismatches.

Comment on lines +1468 to +1471
if len(args) >= 8:
kwargs.setdefault("enable_metrics", args[6])
kwargs.setdefault("enable_advanced_metrics", args[7])
args = args[:6]

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.

medium

To ensure robust backward and forward compatibility (for example, if a client is unpickled from an older or intermediate version where len(args) is 7), we should avoid strict length checks like len(args) >= 8 which could result in passing too many positional arguments to Client.__init__ and raising a TypeError. Instead, check if len(args) > 6 and dynamically map any extra positional arguments to their respective keyword arguments, slicing args to the maximum of 6 positional arguments accepted by Client.__init__.

Suggested change
if len(args) >= 8:
kwargs.setdefault("enable_metrics", args[6])
kwargs.setdefault("enable_advanced_metrics", args[7])
args = args[:6]
if len(args) > 6:
kwargs.setdefault("enable_metrics", args[6])
if len(args) > 7:
kwargs.setdefault("enable_advanced_metrics", args[7])
args = args[:6]

- Decouple is_advanced_metrics_enabled from is_metrics_enabled
- Gate get_meter on active standard or advanced metrics enablement
- Handle empty/whitespace and unrecognized values in _parse_bool_env with a warning
- Use client_setting parameter name in TypeError messages
- Expand unit tests for independent advanced metrics, env var parsing, and get_meter
@shradhakatyal
shradhakatyal force-pushed the feat/gcs-otel-client-integration branch from 7ec58e8 to 7fd2242 Compare October 6, 2026 11:50
… and missing OTel tests

- Rename client_setting to enable_metrics and enable_advanced_metrics in gating functions
- Expand get_meter docstring with Args and Returns sections
- Verify explicit enablement flags return False/None when opentelemetry is missing
…and transfer_manager

- Add enable_metrics and enable_advanced_metrics to Client, GrpcClient, and AsyncGrpcClient
- Add metrics_enabled and advanced_metrics_enabled properties across all client classes
- Preserve metrics configuration across process boundaries in transfer_manager
- Add unit tests in test_client.py, test_grpc_client.py, test_async_grpc_client.py, and test_transfer_manager.py
@shradhakatyal
shradhakatyal force-pushed the feat/gcs-otel-client-integration branch from 7fd2242 to 93b878c Compare October 7, 2026 08:10

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