Skip to content

Commit 5e80776

Browse files
Add gccl metrics header to Spanner. (#3045)
1 parent 08785e4 commit 5e80776

File tree

6 files changed

+70
-7
lines changed

6 files changed

+70
-7
lines changed

spanner/google/cloud/spanner/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"""Cloud Spanner API package."""
1616

1717

18+
import pkg_resources
19+
__version__ = pkg_resources.get_distribution('google-cloud-spanner').version
20+
21+
1822
from google.cloud.spanner.client import Client
1923

2024
from google.cloud.spanner.keyset import KeyRange

spanner/google/cloud/spanner/client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from google.cloud.client import _ClientProjectMixin
3939
from google.cloud.credentials import get_credentials
4040
from google.cloud.iterator import GAXIterator
41+
from google.cloud.spanner import __version__
4142
from google.cloud.spanner._helpers import _options_with_prefix
4243
from google.cloud.spanner.instance import DEFAULT_NODE_COUNT
4344
from google.cloud.spanner.instance import Instance
@@ -152,14 +153,20 @@ def project_name(self):
152153
def instance_admin_api(self):
153154
"""Helper for session-related API calls."""
154155
if self._instance_admin_api is None:
155-
self._instance_admin_api = InstanceAdminClient()
156+
self._instance_admin_api = InstanceAdminClient(
157+
lib_name='gccl',
158+
lib_version=__version__,
159+
)
156160
return self._instance_admin_api
157161

158162
@property
159163
def database_admin_api(self):
160164
"""Helper for session-related API calls."""
161165
if self._database_admin_api is None:
162-
self._database_admin_api = DatabaseAdminClient()
166+
self._database_admin_api = DatabaseAdminClient(
167+
lib_name='gccl',
168+
lib_version=__version__,
169+
)
163170
return self._database_admin_api
164171

165172
def copy(self):

spanner/google/cloud/spanner/database.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from google.cloud.exceptions import Conflict
3131
from google.cloud.exceptions import NotFound
3232
from google.cloud.operation import register_type
33+
from google.cloud.spanner import __version__
3334
from google.cloud.spanner._helpers import _options_with_prefix
3435
from google.cloud.spanner.batch import Batch
3536
from google.cloud.spanner.session import Session
@@ -179,7 +180,8 @@ def ddl_statements(self):
179180
def spanner_api(self):
180181
"""Helper for session-related API calls."""
181182
if self._spanner_api is None:
182-
self._spanner_api = SpannerClient()
183+
self._spanner_api = SpannerClient(
184+
lib_name='gccl', lib_version=__version__)
183185
return self._spanner_api
184186

185187
def __eq__(self, other):

spanner/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
setup(
6161
name='google-cloud-spanner',
62-
version='0.23.1',
62+
version='0.23.2',
6363
description='Python Client for Cloud Spanner',
6464
long_description=README,
6565
namespace_packages=[

spanner/unit_tests/test_client.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,45 @@ def test_constructor_credentials_wo_create_scoped(self):
106106
expected_scopes = None
107107
self._constructor_test_helper(expected_scopes, creds)
108108

109+
def test_admin_api_lib_name(self):
110+
from google.cloud.spanner import __version__
111+
from google.cloud.gapic.spanner_admin_database import v1 as db
112+
from google.cloud.gapic.spanner_admin_instance import v1 as inst
113+
114+
# Get the actual admin client classes.
115+
DatabaseAdminClient = db.database_admin_client.DatabaseAdminClient
116+
InstanceAdminClient = inst.instance_admin_client.InstanceAdminClient
117+
118+
# Test that the DatabaseAdminClient is called with the gccl library
119+
# name and version.
120+
with mock.patch.object(DatabaseAdminClient, '__init__') as mock_dac:
121+
mock_dac.return_value = None
122+
client = self._make_one(
123+
credentials=_make_credentials(),
124+
project='foo',
125+
)
126+
self.assertIsInstance(client.database_admin_api,
127+
DatabaseAdminClient)
128+
mock_dac.assert_called_once()
129+
self.assertEqual(mock_dac.mock_calls[0][2]['lib_name'], 'gccl')
130+
self.assertEqual(mock_dac.mock_calls[0][2]['lib_version'],
131+
__version__)
132+
133+
# Test that the InstanceAdminClient is called with the gccl library
134+
# name and version.
135+
with mock.patch.object(InstanceAdminClient, '__init__') as mock_iac:
136+
mock_iac.return_value = None
137+
client = self._make_one(
138+
credentials=_make_credentials(),
139+
project='foo',
140+
)
141+
self.assertIsInstance(client.instance_admin_api,
142+
InstanceAdminClient)
143+
mock_iac.assert_called_once()
144+
self.assertEqual(mock_iac.mock_calls[0][2]['lib_name'], 'gccl')
145+
self.assertEqual(mock_iac.mock_calls[0][2]['lib_version'],
146+
__version__)
147+
109148
def test_instance_admin_api(self):
110149
from google.cloud._testing import _Monkey
111150
from google.cloud.spanner import client as MUT
@@ -114,14 +153,17 @@ def test_instance_admin_api(self):
114153
client = self._make_one(project=self.PROJECT, credentials=creds)
115154

116155
class _Client(object):
117-
pass
156+
def __init__(self, *args, **kwargs):
157+
self.args = args
158+
self.kwargs = kwargs
118159

119160
with _Monkey(MUT, InstanceAdminClient=_Client):
120161
api = client.instance_admin_api
121162

122163
self.assertTrue(isinstance(api, _Client))
123164
again = client.instance_admin_api
124165
self.assertTrue(again is api)
166+
self.assertEqual(api.kwargs['lib_name'], 'gccl')
125167

126168
def test_database_admin_api(self):
127169
from google.cloud._testing import _Monkey
@@ -131,14 +173,17 @@ def test_database_admin_api(self):
131173
client = self._make_one(project=self.PROJECT, credentials=creds)
132174

133175
class _Client(object):
134-
pass
176+
def __init__(self, *args, **kwargs):
177+
self.args = args
178+
self.kwargs = kwargs
135179

136180
with _Monkey(MUT, DatabaseAdminClient=_Client):
137181
api = client.database_admin_api
138182

139183
self.assertTrue(isinstance(api, _Client))
140184
again = client.database_admin_api
141185
self.assertTrue(again is api)
186+
self.assertEqual(api.kwargs['lib_name'], 'gccl')
142187

143188
def test_copy(self):
144189
credentials = _Credentials('value')

spanner/unit_tests/test_database.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import mock
1919

20+
from google.cloud.spanner import __version__
21+
2022
from google.cloud._testing import _GAXBaseAPI
2123

2224

@@ -188,7 +190,10 @@ def test_spanner_api_property(self):
188190
_client = object()
189191
_clients = [_client]
190192

191-
def _mock_spanner_client():
193+
def _mock_spanner_client(*args, **kwargs):
194+
self.assertIsInstance(args, tuple)
195+
self.assertEqual(kwargs['lib_name'], 'gccl')
196+
self.assertEqual(kwargs['lib_version'], __version__)
192197
return _clients.pop(0)
193198

194199
with _Monkey(MUT, SpannerClient=_mock_spanner_client):

0 commit comments

Comments
 (0)