Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,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.

Expand All @@ -151,17 +151,38 @@ 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
)
# Skip metrics export when a custom Spanner api_endpoint is set.
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
):
# 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
)
Comment on lines +154 to +178

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.

meter_provider = MeterProvider(
metric_readers=[
PeriodicExportingMetricReader(
CloudMonitoringMetricsExporter(
project_id=project,
credentials=credentials,
client_options=monitoring_options,
),
export_interval_millis=METRIC_EXPORT_INTERVAL_MS,
),
Expand Down Expand Up @@ -421,7 +442,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)

Expand Down
31 changes: 28 additions & 3 deletions packages/google-cloud-spanner/google/cloud/spanner_v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

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.

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,
)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from google.api.monitored_resource_pb2 import (
MonitoredResource,
) # pylint: disable=no-name-in-module
from google.api_core import client_options as client_options_lib
from google.api_core.exceptions import (
DeadlineExceeded,
InvalidArgument,
Expand Down Expand Up @@ -95,6 +96,16 @@
("grpc.primary_user_agent", _USER_AGENT),
]

# Scopes that authorize writing time series to Cloud Monitoring.
_MONITORING_WRITE_SCOPE = "https://www.googleapis.com/auth/monitoring.write"
_MONITORING_SCOPES = frozenset(
{
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/monitoring",
_MONITORING_WRITE_SCOPE,
}
)


# pylint is unable to resolve members of protobuf objects
# pylint: disable=no-member
Expand All @@ -110,6 +121,11 @@ class CloudMonitoringMetricsExporter(MetricExporter):
Args:
project_id: project id of your Google Cloud project.
client: Client to upload metrics to Google Cloud Monitoring.
credentials: Optional credentials for authenticating with Cloud
Monitoring.
client_options: Optional client options (for example,
``universe_domain`` or ``api_endpoint``) passed to
``MetricServiceClient``.
"""

# Based on the cloud_monitoring exporter found here: https://github.com/GoogleCloudPlatform/opentelemetry-operations-python/blob/main/opentelemetry-exporter-gcp-monitoring/src/opentelemetry/exporter/cloud_monitoring/__init__.py
Expand All @@ -119,28 +135,47 @@ def __init__(
project_id: Optional[str] = None,
client: Optional["MetricServiceClient"] = None,
credentials: Optional[ga_credentials.Credentials] = None,
client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None,
):
"""Initialize a custom exporter to send metrics for the Spanner Service Metrics."""
# Default preferred_temporality is all CUMULATIVE so need to customize
super().__init__()

# Create a new GRPC Client for Google Cloud Monitoring if not provided
self.client = client or MetricServiceClient(
transport=MetricServiceGrpcTransport(
channel=MetricServiceGrpcTransport.create_channel(
options=_OPTIONS,
credentials=credentials,
if client is not None:
self.client = client
else:
# Re-scope credentials pinned to Spanner-only scopes for Cloud Monitoring.
monitoring_credentials = credentials
if (
isinstance(credentials, ga_credentials.Scoped)
and not credentials.requires_scopes
and not _MONITORING_SCOPES.intersection(credentials.scopes or ())
):
monitoring_credentials = credentials.with_scopes(
[_MONITORING_WRITE_SCOPE]
)

# Pass transport/channel callables so MetricServiceClient resolves
# universe_domain and validates credentials while keeping _OPTIONS.
self.client = MetricServiceClient(
credentials=monitoring_credentials,
client_options=client_options,
transport=lambda **kwargs: MetricServiceGrpcTransport(
**kwargs,
channel=lambda *a, **kw: MetricServiceGrpcTransport.create_channel(
*a, **{**kw, "options": _OPTIONS}
),
),
)
)

# Set project information
self.project_id: str
# Avoid ambient ADC project fallback when explicit credentials are provided.
if not project_id:
_, default_project_id = google.auth.default()
self.project_id = str(default_project_id)
else:
self.project_id = project_id
project_id = getattr(credentials, "project_id", None)
if not project_id and credentials is None:
_, project_id = google.auth.default()
if not project_id:
raise ValueError("project_id could not be determined.")
self.project_id = str(project_id)
self.project_name = self.client.common_project_path(self.project_id)

def _batch_write(self, series: List["TimeSeries"], timeout_millis: float) -> None:
Expand Down
Loading
Loading