Repository navigation
fix(spanner): respect universe_domain and client_options in metrics exporter - #19172
sakthivelmanii wants to merge 4 commits into
Conversation
…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.
There was a problem hiding this comment.
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.
| 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 | ||
| ) |
There was a problem hiding this comment.
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
- Use
getattrwhen retrieving attributes from external, caller-supplied objects that are not guaranteed to have those attributes defined, rather than accessing them directly. - 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.
There was a problem hiding this comment.
Updated to use getattr for api_endpoint and universe_domain. google.api_core.client_options is already imported at the top of the file.
| 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 | ||
| ) |
There was a problem hiding this comment.
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
- Use
getattrwhen retrieving attributes from external, caller-supplied objects that are not guaranteed to have those attributes defined, rather than accessing them directly. - 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.
There was a problem hiding this comment.
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.
Problem
CloudMonitoringMetricsExporteralways connected tomonitoring.googleapis.com, ignoringclient_options.universe_domainandGOOGLE_CLOUD_UNIVERSE_DOMAIN.client_options.api_endpointstill exported built-in metrics tomonitoring.googleapis.com.Client.__init__passed rawprojectandcredentialsinstead of resolvedself.projectandself._credentials, causing fallback to ambient ADC.Solution
self.project,self._credentials, and filtered monitoringclient_optionsto_initialize_metricsandCloudMonitoringMetricsExporter.api_endpointis configured.MetricServiceClientvia a transport callable souniverse_domainand credentials validation work natively, re-scope Spanner-only scoped credentials tomonitoring.write, and avoid ADC project fallback when explicit credentials are provided.