Skip to content

fix(spanner): respect universe_domain and client_options in metrics exporter - #19172

Open
sakthivelmanii wants to merge 4 commits into
mainfrom
fix/spanner-metrics-exporter-universe-domain
Open

sakthivelmanii wants to merge 4 commits into
mainfrom
fix/spanner-metrics-exporter-universe-domain

Conversation

@sakthivelmanii

Copy link
Copy Markdown
Contributor

Problem

  • CloudMonitoringMetricsExporter always connected to monitoring.googleapis.com, ignoring client_options.universe_domain and GOOGLE_CLOUD_UNIVERSE_DOMAIN.
  • Clients with a custom client_options.api_endpoint still exported built-in metrics to monitoring.googleapis.com.
  • Client.__init__ passed raw project and credentials instead of resolved self.project and self._credentials, causing fallback to ambient ADC.

Solution

  • Pass resolved self.project, self._credentials, and filtered monitoring client_options to _initialize_metrics and CloudMonitoringMetricsExporter.
  • Skip built-in metrics export when a custom non-default api_endpoint is configured.
  • Configure MetricServiceClient via a transport callable so universe_domain and credentials validation work natively, re-scope Spanner-only scoped credentials to monitoring.write, and avoid ADC project fallback when explicit credentials are provided.
  • Add unit tests for both sync and async clients and the metrics exporter.

…xporter

## Problem
- `CloudMonitoringMetricsExporter` always connected to `monitoring.googleapis.com`, ignoring `client_options.universe_domain` and `GOOGLE_CLOUD_UNIVERSE_DOMAIN`.
- Clients with a custom `client_options.api_endpoint` still exported built-in metrics to `monitoring.googleapis.com`.
- `Client.__init__` passed raw `project` and `credentials` instead of resolved `self.project` and `self._credentials`, causing fallback to ambient ADC.

## Solution
- Pass resolved `self.project`, `self._credentials`, and filtered monitoring `client_options` to `_initialize_metrics` and `CloudMonitoringMetricsExporter`.
- Skip built-in metrics export when a custom non-default `api_endpoint` is configured.
- Configure `MetricServiceClient` via a transport callable so `universe_domain` and credentials validation work natively, re-scope Spanner-only scoped credentials to `monitoring.write`, and avoid ADC project fallback when explicit credentials are provided.
- Add unit tests for both sync and async clients and the metrics exporter.
@sakthivelmanii
sakthivelmanii requested a review from a team as a code owner November 21, 2026 21:13

@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 updates both the synchronous and asynchronous Spanner clients to propagate client_options (such as universe_domain and api_endpoint) to the built-in metrics exporter, skipping metrics export when a custom endpoint is configured. Feedback on the changes highlights that google.api_core.client_options is not explicitly imported in either client file, which could lead to a NameError. Additionally, it is recommended to use getattr when accessing attributes from the caller-supplied client_options object to prevent potential attribute errors.

Comment on lines +154 to +175
if isinstance(client_options, dict):
client_options = google.api_core.client_options.from_dict(
client_options
)
# Skip metrics export when a custom Spanner api_endpoint is set.
has_custom_endpoint = bool(
client_options and client_options.api_endpoint
)
if (
not _get_spanner_emulator_host()
and not emulator_host
and not isinstance(credentials, AnonymousCredentials)
and not has_custom_endpoint
):
# Only forward universe_domain to Cloud Monitoring.
monitoring_options = (
google.api_core.client_options.ClientOptions(
universe_domain=client_options.universe_domain
)
if client_options and client_options.universe_domain
else None
)

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.

high

The module google.api_core.client_options is not explicitly imported in this file. To maintain consistency with fully qualified name references and avoid potential NameErrors, explicitly import google.api_core.client_options. Additionally, use getattr when retrieving attributes like api_endpoint and universe_domain from the caller-supplied client_options object, as they are not guaranteed to be defined.

                    import google.api_core.client_options

                    if isinstance(client_options, dict):
                        client_options = google.api_core.client_options.from_dict(
                            client_options
                        )
                    # Skip metrics export when a custom Spanner api_endpoint is set.
                    api_endpoint = getattr(client_options, "api_endpoint", None)
                    has_custom_endpoint = bool(api_endpoint)
                    if (
                        not _get_spanner_emulator_host()
                        and not emulator_host
                        and not isinstance(credentials, AnonymousCredentials)
                        and not has_custom_endpoint
                    ):
                        # Only forward universe_domain to Cloud Monitoring.
                        universe_domain = getattr(client_options, "universe_domain", None)
                        monitoring_options = (
                            google.api_core.client_options.ClientOptions(
                                universe_domain=universe_domain
                            )
                            if universe_domain
                            else None
                        )
References
  1. Use getattr when retrieving attributes from external, caller-supplied objects that are not guaranteed to have those attributes defined, rather than accessing them directly.
  2. Maintain consistency in how modules are referenced. If a module is imported and referenced using its fully qualified name, avoid refactoring references to use a shorter imported name unless the entire file is updated for consistency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use getattr for api_endpoint and universe_domain. google.api_core.client_options is already imported at the top of the file.

Comment on lines +133 to +152
if isinstance(client_options, dict):
client_options = google.api_core.client_options.from_dict(
client_options
)
has_custom_endpoint = bool(
client_options and client_options.api_endpoint
)
if (
not _get_spanner_emulator_host()
and (not emulator_host)
and (not isinstance(credentials, AnonymousCredentials))
and (not has_custom_endpoint)
):
monitoring_options = (
google.api_core.client_options.ClientOptions(
universe_domain=client_options.universe_domain
)
if client_options and client_options.universe_domain
else None
)

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.

high

The module google.api_core.client_options is not explicitly imported in this file. To maintain consistency with fully qualified name references and avoid potential NameErrors, explicitly import google.api_core.client_options. Additionally, use getattr when retrieving attributes like api_endpoint and universe_domain from the caller-supplied client_options object, as they are not guaranteed to be defined.

                    import google.api_core.client_options

                    if isinstance(client_options, dict):
                        client_options = google.api_core.client_options.from_dict(
                            client_options
                        )
                    api_endpoint = getattr(client_options, "api_endpoint", None)
                    has_custom_endpoint = bool(api_endpoint)
                    if (
                        not _get_spanner_emulator_host()
                        and (not emulator_host)
                        and (not isinstance(credentials, AnonymousCredentials))
                        and (not has_custom_endpoint)
                    ):
                        universe_domain = getattr(client_options, "universe_domain", None)
                        monitoring_options = (
                            google.api_core.client_options.ClientOptions(
                                universe_domain=universe_domain
                            )
                            if universe_domain
                            else None
                        )
References
  1. Use getattr when retrieving attributes from external, caller-supplied objects that are not guaranteed to have those attributes defined, rather than accessing them directly.
  2. Maintain consistency in how modules are referenced. If a module is imported and referenced using its fully qualified name, avoid refactoring references to use a shorter imported name unless the entire file is updated for consistency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated via CrossSync from _async/client.py to use getattr for api_endpoint and universe_domain. google.api_core.client_options is already imported at the top of the file.

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