Skip to content

Commit b30f5a6

Browse files
matthew29tangcopybara-github
authored andcommitted
feat: Add custom tool context manager for telemetry
PiperOrigin-RevId: 615181941
1 parent 75e5f6c commit b30f5a6

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

google/cloud/aiplatform/initializer.py

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from google.cloud.aiplatform.metadata import metadata
3737
from google.cloud.aiplatform.utils import resource_manager_utils
3838
from google.cloud.aiplatform.tensorboard import tensorboard_resource
39+
from google.cloud.aiplatform import telemetry
3940

4041
from google.cloud.aiplatform.compat.types import (
4142
encryption_spec as gca_encryption_spec_compat,
@@ -478,6 +479,10 @@ def create_client(
478479
except Exception: # pylint: disable=broad-exception-caught
479480
pass
480481

482+
if telemetry._tool_names_to_append:
483+
# Must append to gapic_version due to b/259738581.
484+
gapic_version = f"{gapic_version}+tools+{'+'.join(telemetry._tool_names_to_append[::-1])}"
485+
481486
user_agent = f"{constants.USER_AGENT_PRODUCT}/{gapic_version}"
482487
if appended_user_agent:
483488
user_agent = f"{user_agent} {' '.join(appended_user_agent)}"

google/cloud/aiplatform/telemetry.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2024 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
import contextlib
19+
20+
_tool_names_to_append = []
21+
22+
23+
@contextlib.contextmanager
24+
def tool_context_manager(tool_name: str) -> None:
25+
"""Context manager for appending tool name to client instantiations.
26+
27+
Most client instantiations occur at construction time. There are a few
28+
exceptions such as generate_content that uses lazy instantiation at
29+
inference time (b/328511605).
30+
31+
Example Usage:
32+
33+
aiplatform.init(...)
34+
with telemetry.tool_context_manager('ClientName'):
35+
model = GenerativeModel("gemini-pro")
36+
responses = model.generate_content("Why is the sky blue?", stream=True)
37+
38+
Args:
39+
tool_name: The name of the client library to attribute usage to
40+
41+
Returns:
42+
None
43+
"""
44+
_append_tool_name(tool_name)
45+
try:
46+
yield
47+
finally:
48+
_pop_tool_name(tool_name)
49+
50+
51+
def _append_tool_name(tool_name: str) -> None:
52+
_tool_names_to_append.append(tool_name)
53+
54+
55+
def _pop_tool_name(tool_name: str) -> None:
56+
if not _tool_names_to_append or _tool_names_to_append[-1] != tool_name:
57+
raise RuntimeError(
58+
"Tool context error detected. This can occur due to parallelization."
59+
)
60+
_tool_names_to_append.pop()

0 commit comments

Comments
 (0)