Skip to content

Commit 217faf8

Browse files
speedstorm1copybara-github
authored andcommitted
chore: Logging the Vertex notebook environment for every API request
PiperOrigin-RevId: 652977926
1 parent 8f53902 commit 217faf8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

google/cloud/aiplatform/initializer.py

+30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818

1919
from concurrent import futures
20+
import enum
2021
import functools
2122
import inspect
2223
import logging
@@ -53,6 +54,14 @@
5354
_TOP_GOOGLE_CONSTRUCTOR_METHOD_TAG = "top_google_constructor_method"
5455

5556

57+
class _Product(enum.Enum):
58+
"""Notebook product types."""
59+
60+
WORKBENCH_INSTANCE = "WORKBENCH_INSTANCE"
61+
COLAB_ENTERPRISE = "COLAB_ENTERPRISE"
62+
WORKBENCH_CUSTOM_CONTAINER = "WORKBENCH_CUSTOM_CONTAINER"
63+
64+
5665
class _Config:
5766
"""Stores common parameters and options for API calls."""
5867

@@ -110,6 +119,7 @@ def __init__(self):
110119
self._api_endpoint = None
111120
self._api_transport = None
112121
self._request_metadata = None
122+
self._resource_type = None
113123

114124
def init(
115125
self,
@@ -242,6 +252,7 @@ def init(
242252
self._service_account = service_account
243253
if request_metadata is not None:
244254
self._request_metadata = request_metadata
255+
self._resource_type = None
245256

246257
# Finally, perform secondary state updates
247258
if experiment_tensorboard and not isinstance(experiment_tensorboard, bool):
@@ -370,6 +381,21 @@ def experiment_name(self) -> Optional[str]:
370381
"""Default experiment name, if provided."""
371382
return metadata._experiment_tracker.experiment_name
372383

384+
def get_resource_type(self) -> _Product:
385+
"""Returns the resource type from environment variables."""
386+
if self._resource_type:
387+
return self._resource_type
388+
389+
vertex_product = os.getenv("VERTEX_PRODUCT")
390+
if vertex_product == "COLAB_ENTERPRISE":
391+
self._resource_type = _Product.COLAB_ENTERPRISE
392+
if vertex_product == "WORKBENCH_CUSTOM_CONTAINER":
393+
self._resource_type = _Product.WORKBENCH_CUSTOM_CONTAINER
394+
if vertex_product == "WORKBENCH_INSTANCE":
395+
self._resource_type = _Product.WORKBENCH_INSTANCE
396+
397+
return self._resource_type
398+
373399
def get_client_options(
374400
self,
375401
location_override: Optional[str] = None,
@@ -489,6 +515,10 @@ def create_client(
489515
except Exception: # pylint: disable=broad-exception-caught
490516
pass
491517

518+
resource_type = self.get_resource_type()
519+
if resource_type:
520+
gapic_version += f"+environment+{resource_type.value}"
521+
492522
if telemetry._tool_names_to_append:
493523
# Must append to gapic_version due to b/259738581.
494524
gapic_version = f"{gapic_version}+tools+{'+'.join(telemetry._tool_names_to_append[::-1])}"

0 commit comments

Comments
 (0)