Skip to content

Commit

Permalink
Add gccl metrics header to Spanner. (#3045)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored Feb 22, 2017
1 parent 08785e4 commit 5e80776
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 deletions.
4 changes: 4 additions & 0 deletions spanner/google/cloud/spanner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"""Cloud Spanner API package."""


import pkg_resources
__version__ = pkg_resources.get_distribution('google-cloud-spanner').version


from google.cloud.spanner.client import Client

from google.cloud.spanner.keyset import KeyRange
Expand Down
11 changes: 9 additions & 2 deletions spanner/google/cloud/spanner/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from google.cloud.client import _ClientProjectMixin
from google.cloud.credentials import get_credentials
from google.cloud.iterator import GAXIterator
from google.cloud.spanner import __version__
from google.cloud.spanner._helpers import _options_with_prefix
from google.cloud.spanner.instance import DEFAULT_NODE_COUNT
from google.cloud.spanner.instance import Instance
Expand Down Expand Up @@ -152,14 +153,20 @@ def project_name(self):
def instance_admin_api(self):
"""Helper for session-related API calls."""
if self._instance_admin_api is None:
self._instance_admin_api = InstanceAdminClient()
self._instance_admin_api = InstanceAdminClient(
lib_name='gccl',
lib_version=__version__,
)
return self._instance_admin_api

@property
def database_admin_api(self):
"""Helper for session-related API calls."""
if self._database_admin_api is None:
self._database_admin_api = DatabaseAdminClient()
self._database_admin_api = DatabaseAdminClient(
lib_name='gccl',
lib_version=__version__,
)
return self._database_admin_api

def copy(self):
Expand Down
4 changes: 3 additions & 1 deletion spanner/google/cloud/spanner/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from google.cloud.exceptions import Conflict
from google.cloud.exceptions import NotFound
from google.cloud.operation import register_type
from google.cloud.spanner import __version__
from google.cloud.spanner._helpers import _options_with_prefix
from google.cloud.spanner.batch import Batch
from google.cloud.spanner.session import Session
Expand Down Expand Up @@ -179,7 +180,8 @@ def ddl_statements(self):
def spanner_api(self):
"""Helper for session-related API calls."""
if self._spanner_api is None:
self._spanner_api = SpannerClient()
self._spanner_api = SpannerClient(
lib_name='gccl', lib_version=__version__)
return self._spanner_api

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion spanner/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

setup(
name='google-cloud-spanner',
version='0.23.1',
version='0.23.2',
description='Python Client for Cloud Spanner',
long_description=README,
namespace_packages=[
Expand Down
49 changes: 47 additions & 2 deletions spanner/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,45 @@ def test_constructor_credentials_wo_create_scoped(self):
expected_scopes = None
self._constructor_test_helper(expected_scopes, creds)

def test_admin_api_lib_name(self):
from google.cloud.spanner import __version__
from google.cloud.gapic.spanner_admin_database import v1 as db
from google.cloud.gapic.spanner_admin_instance import v1 as inst

# Get the actual admin client classes.
DatabaseAdminClient = db.database_admin_client.DatabaseAdminClient
InstanceAdminClient = inst.instance_admin_client.InstanceAdminClient

# Test that the DatabaseAdminClient is called with the gccl library
# name and version.
with mock.patch.object(DatabaseAdminClient, '__init__') as mock_dac:
mock_dac.return_value = None
client = self._make_one(
credentials=_make_credentials(),
project='foo',
)
self.assertIsInstance(client.database_admin_api,
DatabaseAdminClient)
mock_dac.assert_called_once()
self.assertEqual(mock_dac.mock_calls[0][2]['lib_name'], 'gccl')
self.assertEqual(mock_dac.mock_calls[0][2]['lib_version'],
__version__)

# Test that the InstanceAdminClient is called with the gccl library
# name and version.
with mock.patch.object(InstanceAdminClient, '__init__') as mock_iac:
mock_iac.return_value = None
client = self._make_one(
credentials=_make_credentials(),
project='foo',
)
self.assertIsInstance(client.instance_admin_api,
InstanceAdminClient)
mock_iac.assert_called_once()
self.assertEqual(mock_iac.mock_calls[0][2]['lib_name'], 'gccl')
self.assertEqual(mock_iac.mock_calls[0][2]['lib_version'],
__version__)

def test_instance_admin_api(self):
from google.cloud._testing import _Monkey
from google.cloud.spanner import client as MUT
Expand All @@ -114,14 +153,17 @@ def test_instance_admin_api(self):
client = self._make_one(project=self.PROJECT, credentials=creds)

class _Client(object):
pass
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

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

self.assertTrue(isinstance(api, _Client))
again = client.instance_admin_api
self.assertTrue(again is api)
self.assertEqual(api.kwargs['lib_name'], 'gccl')

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

class _Client(object):
pass
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

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

self.assertTrue(isinstance(api, _Client))
again = client.database_admin_api
self.assertTrue(again is api)
self.assertEqual(api.kwargs['lib_name'], 'gccl')

def test_copy(self):
credentials = _Credentials('value')
Expand Down
7 changes: 6 additions & 1 deletion spanner/unit_tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import mock

from google.cloud.spanner import __version__

from google.cloud._testing import _GAXBaseAPI


Expand Down Expand Up @@ -188,7 +190,10 @@ def test_spanner_api_property(self):
_client = object()
_clients = [_client]

def _mock_spanner_client():
def _mock_spanner_client(*args, **kwargs):
self.assertIsInstance(args, tuple)
self.assertEqual(kwargs['lib_name'], 'gccl')
self.assertEqual(kwargs['lib_version'], __version__)
return _clients.pop(0)

with _Monkey(MUT, SpannerClient=_mock_spanner_client):
Expand Down

0 comments on commit 5e80776

Please sign in to comment.