Skip to content

Commit

Permalink
Merge pull request #3089 from dhermes/bigtable-version-info
Browse files Browse the repository at this point in the history
Sending x-goog-api-client header in Bigtable.
  • Loading branch information
dhermes authored Mar 3, 2017
2 parents 2f38f2b + 20ffd2b commit aadb25f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 103 deletions.
3 changes: 3 additions & 0 deletions bigtable/google/cloud/bigtable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
"""Google Cloud Bigtable API package."""


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

from google.cloud.bigtable.client import Client
33 changes: 22 additions & 11 deletions bigtable/google/cloud/bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,24 @@
import os

import google.auth.credentials
from google.gax.utils import metrics
from google.longrunning import operations_grpc

from google.cloud._helpers import make_insecure_stub
from google.cloud._helpers import make_secure_stub
from google.cloud._http import DEFAULT_USER_AGENT
from google.cloud.client import _ClientFactoryMixin
from google.cloud.client import _ClientProjectMixin
from google.cloud.credentials import get_credentials
from google.cloud.environment_vars import BIGTABLE_EMULATOR

from google.cloud.bigtable import __version__
from google.cloud.bigtable._generated import bigtable_instance_admin_pb2
from google.cloud.bigtable._generated import bigtable_pb2
from google.cloud.bigtable._generated import bigtable_table_admin_pb2
from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES
from google.cloud.bigtable.instance import Instance
from google.cloud.bigtable.instance import _EXISTING_INSTANCE_LOCATION_ID
from google.cloud.client import _ClientFactoryMixin
from google.cloud.client import _ClientProjectMixin
from google.cloud._http import DEFAULT_USER_AGENT
from google.cloud.credentials import get_credentials
from google.cloud.environment_vars import BIGTABLE_EMULATOR


TABLE_ADMIN_HOST = 'bigtableadmin.googleapis.com'
Expand All @@ -67,10 +70,17 @@
READ_ONLY_SCOPE = 'https://www.googleapis.com/auth/bigtable.data.readonly'
"""Scope for reading table data."""

_METRICS_HEADERS = (
('gccl', __version__),
)
_HEADER_STR = metrics.stringify(metrics.fill(_METRICS_HEADERS))
_GRPC_EXTRA_OPTIONS = (
('x-goog-api-client', _HEADER_STR),
)
# NOTE: 'grpc.max_message_length' will no longer be recognized in
# grpcio 1.1 and later.
_MAX_MSG_LENGTH_100MB = 100 * 1024 * 1024
_GRPC_MAX_LENGTH_OPTIONS = (
_GRPC_MAX_LENGTH_OPTIONS = _GRPC_EXTRA_OPTIONS + (
('grpc.max_message_length', _MAX_MSG_LENGTH_100MB),
('grpc.max_receive_message_length', _MAX_MSG_LENGTH_100MB),
)
Expand Down Expand Up @@ -107,7 +117,7 @@ def _make_instance_stub(client):
return make_secure_stub(
client.credentials, client.user_agent,
bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
INSTANCE_ADMIN_HOST)
INSTANCE_ADMIN_HOST, extra_options=_GRPC_EXTRA_OPTIONS)
else:
return make_insecure_stub(
bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
Expand All @@ -127,9 +137,10 @@ def _make_operations_stub(client):
:returns: A gRPC stub object.
"""
if client.emulator_host is None:
return make_secure_stub(client.credentials, client.user_agent,
operations_grpc.OperationsStub,
OPERATIONS_API_HOST)
return make_secure_stub(
client.credentials, client.user_agent,
operations_grpc.OperationsStub,
OPERATIONS_API_HOST, extra_options=_GRPC_EXTRA_OPTIONS)
else:
return make_insecure_stub(operations_grpc.OperationsStub,
client.emulator_host)
Expand All @@ -148,7 +159,7 @@ def _make_table_stub(client):
return make_secure_stub(
client.credentials, client.user_agent,
bigtable_table_admin_pb2.BigtableTableAdminStub,
TABLE_ADMIN_HOST)
TABLE_ADMIN_HOST, extra_options=_GRPC_EXTRA_OPTIONS)
else:
return make_insecure_stub(
bigtable_table_admin_pb2.BigtableTableAdminStub,
Expand Down
2 changes: 1 addition & 1 deletion bigtable/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

REQUIREMENTS = [
'google-cloud-core >= 0.23.1, < 0.24dev',
'grpcio >= 1.0.2, < 2.0dev',
'google-gax>=0.15.7, <0.16dev',
]

setup(
Expand Down
139 changes: 48 additions & 91 deletions bigtable/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,24 @@ def _call_fut(self, client):

return _make_data_stub(client)

def test_without_emulator(self):
from google.cloud._testing import _Monkey
@mock.patch('google.cloud.bigtable.client.make_secure_stub',
return_value=mock.sentinel.stub)
def test_without_emulator(self, make_stub):
from google.cloud.bigtable import client as MUT

credentials = _make_credentials()
user_agent = 'you-sir-age-int'
client = _Client(credentials, user_agent)

fake_stub = object()
make_secure_stub_args = []

def mock_make_secure_stub(*args, **kwargs):
make_secure_stub_args.append(args)
make_secure_stub_args.append(kwargs)
return fake_stub

with _Monkey(MUT, make_secure_stub=mock_make_secure_stub):
result = self._call_fut(client)

extra_options = {'extra_options': (
('grpc.max_message_length', 104857600),
('grpc.max_receive_message_length', 104857600)
)}
self.assertIs(result, fake_stub)
self.assertEqual(make_secure_stub_args, [
(
client.credentials,
client.user_agent,
MUT.bigtable_pb2.BigtableStub,
MUT.DATA_API_HOST,
),
extra_options,
])
result = self._call_fut(client)
self.assertIs(result, mock.sentinel.stub)
make_stub.assert_called_once_with(
client.credentials,
client.user_agent,
MUT.bigtable_pb2.BigtableStub,
MUT.DATA_API_HOST,
extra_options=MUT._GRPC_MAX_LENGTH_OPTIONS,
)

def test_with_emulator(self):
from google.cloud._testing import _Monkey
Expand Down Expand Up @@ -103,33 +88,24 @@ def _call_fut(self, client):

return _make_instance_stub(client)

def test_without_emulator(self):
from google.cloud._testing import _Monkey
@mock.patch('google.cloud.bigtable.client.make_secure_stub',
return_value=mock.sentinel.stub)
def test_without_emulator(self, make_stub):
from google.cloud.bigtable import client as MUT

credentials = _make_credentials()
user_agent = 'you-sir-age-int'
client = _Client(credentials, user_agent)

fake_stub = object()
make_secure_stub_args = []

def mock_make_secure_stub(*args):
make_secure_stub_args.append(args)
return fake_stub

with _Monkey(MUT, make_secure_stub=mock_make_secure_stub):
result = self._call_fut(client)

self.assertIs(result, fake_stub)
self.assertEqual(make_secure_stub_args, [
(
client.credentials,
client.user_agent,
MUT.bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
MUT.INSTANCE_ADMIN_HOST,
),
])
result = self._call_fut(client)
self.assertIs(result, mock.sentinel.stub)
make_stub.assert_called_once_with(
client.credentials,
client.user_agent,
MUT.bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
MUT.INSTANCE_ADMIN_HOST,
extra_options=MUT._GRPC_EXTRA_OPTIONS,
)

def test_with_emulator(self):
from google.cloud._testing import _Monkey
Expand Down Expand Up @@ -164,35 +140,25 @@ def _call_fut(self, client):

return _make_operations_stub(client)

def test_without_emulator(self):
@mock.patch('google.cloud.bigtable.client.make_secure_stub',
return_value=mock.sentinel.stub)
def test_without_emulator(self, make_stub):
from google.longrunning import operations_grpc

from google.cloud._testing import _Monkey
from google.cloud.bigtable import client as MUT

credentials = _make_credentials()
user_agent = 'you-sir-age-int'
client = _Client(credentials, user_agent)

fake_stub = object()
make_secure_stub_args = []

def mock_make_secure_stub(*args):
make_secure_stub_args.append(args)
return fake_stub

with _Monkey(MUT, make_secure_stub=mock_make_secure_stub):
result = self._call_fut(client)

self.assertIs(result, fake_stub)
self.assertEqual(make_secure_stub_args, [
(
client.credentials,
client.user_agent,
operations_grpc.OperationsStub,
MUT.OPERATIONS_API_HOST,
),
])
result = self._call_fut(client)
self.assertIs(result, mock.sentinel.stub)
make_stub.assert_called_once_with(
client.credentials,
client.user_agent,
operations_grpc.OperationsStub,
MUT.OPERATIONS_API_HOST,
extra_options=MUT._GRPC_EXTRA_OPTIONS,
)

def test_with_emulator(self):
from google.longrunning import operations_grpc
Expand Down Expand Up @@ -229,33 +195,24 @@ def _call_fut(self, client):

return _make_table_stub(client)

def test_without_emulator(self):
from google.cloud._testing import _Monkey
@mock.patch('google.cloud.bigtable.client.make_secure_stub',
return_value=mock.sentinel.stub)
def test_without_emulator(self, make_stub):
from google.cloud.bigtable import client as MUT

credentials = _make_credentials()
user_agent = 'you-sir-age-int'
client = _Client(credentials, user_agent)

fake_stub = object()
make_secure_stub_args = []

def mock_make_secure_stub(*args):
make_secure_stub_args.append(args)
return fake_stub

with _Monkey(MUT, make_secure_stub=mock_make_secure_stub):
result = self._call_fut(client)

self.assertIs(result, fake_stub)
self.assertEqual(make_secure_stub_args, [
(
client.credentials,
client.user_agent,
MUT.bigtable_table_admin_pb2.BigtableTableAdminStub,
MUT.TABLE_ADMIN_HOST,
),
])
result = self._call_fut(client)
self.assertIs(result, mock.sentinel.stub)
make_stub.assert_called_once_with(
client.credentials,
client.user_agent,
MUT.bigtable_table_admin_pb2.BigtableTableAdminStub,
MUT.TABLE_ADMIN_HOST,
extra_options=MUT._GRPC_EXTRA_OPTIONS,
)

def test_with_emulator(self):
from google.cloud._testing import _Monkey
Expand Down

0 comments on commit aadb25f

Please sign in to comment.