Repository navigation
fix(spanner): respect universe_domain and client_options in metrics exporter #19172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,7 +119,7 @@ def _get_spanner_log_client_options_env(): | |
| return os.getenv(LOG_CLIENT_OPTIONS_ENV_VAR, "false").lower() == "true" | ||
|
|
||
|
|
||
| def _initialize_metrics(project, credentials, emulator_host=None): | ||
| def _initialize_metrics(project, credentials, emulator_host=None, client_options=None): | ||
| """Initializes the Spanner built-in metrics. | ||
|
|
||
| This function sets up the OpenTelemetry MeterProvider and the SpannerMetricsTracerFactory. | ||
|
|
@@ -130,16 +130,36 @@ def _initialize_metrics(project, credentials, emulator_host=None): | |
| if not _metrics_monitor_initialized: | ||
| meter_provider = metrics.NoOpMeterProvider() | ||
| try: | ||
| if isinstance(client_options, dict): | ||
| client_options = google.api_core.client_options.from_dict( | ||
| client_options | ||
| ) | ||
| has_custom_endpoint = bool( | ||
| getattr(client_options, "api_endpoint", None) | ||
| ) | ||
| 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 | ||
| ) | ||
|
Comment on lines
+133
to
+155
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The module 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated via CrossSync from |
||
| meter_provider = MeterProvider( | ||
| metric_readers=[ | ||
| PeriodicExportingMetricReader( | ||
| CloudMonitoringMetricsExporter( | ||
| project_id=project, credentials=credentials | ||
| project_id=project, | ||
| credentials=credentials, | ||
| client_options=monitoring_options, | ||
| ), | ||
| export_interval_millis=METRIC_EXPORT_INTERVAL_MS, | ||
| ) | ||
|
|
@@ -367,7 +387,12 @@ def __init__( | |
| and (not disable_builtin_metrics) | ||
| and HAS_GOOGLE_CLOUD_MONITORING_INSTALLED | ||
| ): | ||
| _initialize_metrics(project, credentials, self._emulator_host) | ||
| _initialize_metrics( | ||
| self.project, | ||
| self._credentials, | ||
| self._emulator_host, | ||
| client_options=self._client_options, | ||
| ) | ||
| else: | ||
| SpannerMetricsTracerFactory(enabled=False) | ||
| self._route_to_leader_enabled = route_to_leader_enabled | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module
google.api_core.client_optionsis not explicitly imported in this file. To maintain consistency with fully qualified name references and avoid potentialNameErrors, explicitly importgoogle.api_core.client_options. Additionally, usegetattrwhen retrieving attributes likeapi_endpointanduniverse_domainfrom the caller-suppliedclient_optionsobject, as they are not guaranteed to be defined.References
getattrwhen retrieving attributes from external, caller-supplied objects that are not guaranteed to have those attributes defined, rather than accessing them directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use
getattrforapi_endpointanduniverse_domain.google.api_core.client_optionsis already imported at the top of the file.