Skip to content

Commit 374f8c0

Browse files
committed
Sending x-goog-api-client header in Bigtable.
We missed this because it does not use GAPIC (the most recent generated Bigtable GAPIC surface is 5+ months old).
1 parent a3596d6 commit 374f8c0

File tree

4 files changed

+74
-103
lines changed

4 files changed

+74
-103
lines changed

bigtable/google/cloud/bigtable/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@
1515
"""Google Cloud Bigtable API package."""
1616

1717

18+
from pkg_resources import get_distribution
19+
__version__ = get_distribution('google-cloud-bigtable').version
20+
1821
from google.cloud.bigtable.client import Client

bigtable/google/cloud/bigtable/client.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,24 @@
3232
import os
3333

3434
import google.auth.credentials
35+
from google.gax.utils import metrics
3536
from google.longrunning import operations_grpc
3637

3738
from google.cloud._helpers import make_insecure_stub
3839
from google.cloud._helpers import make_secure_stub
40+
from google.cloud._http import DEFAULT_USER_AGENT
41+
from google.cloud.client import _ClientFactoryMixin
42+
from google.cloud.client import _ClientProjectMixin
43+
from google.cloud.credentials import get_credentials
44+
from google.cloud.environment_vars import BIGTABLE_EMULATOR
45+
46+
from google.cloud.bigtable import __version__
3947
from google.cloud.bigtable._generated import bigtable_instance_admin_pb2
4048
from google.cloud.bigtable._generated import bigtable_pb2
4149
from google.cloud.bigtable._generated import bigtable_table_admin_pb2
4250
from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES
4351
from google.cloud.bigtable.instance import Instance
4452
from google.cloud.bigtable.instance import _EXISTING_INSTANCE_LOCATION_ID
45-
from google.cloud.client import _ClientFactoryMixin
46-
from google.cloud.client import _ClientProjectMixin
47-
from google.cloud._http import DEFAULT_USER_AGENT
48-
from google.cloud.credentials import get_credentials
49-
from google.cloud.environment_vars import BIGTABLE_EMULATOR
5053

5154

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

73+
_METRICS_HEADERS = (
74+
('gccl', __version__),
75+
)
76+
_HEADER_STR = metrics.stringify(metrics.fill(_METRICS_HEADERS))
77+
_GRPC_EXTRA_OPTIONS = (
78+
('x-goog-api-client', _HEADER_STR),
79+
)
7080
# NOTE: 'grpc.max_message_length' will no longer be recognized in
7181
# grpcio 1.1 and later.
7282
_MAX_MSG_LENGTH_100MB = 100 * 1024 * 1024
73-
_GRPC_MAX_LENGTH_OPTIONS = (
83+
_GRPC_MAX_LENGTH_OPTIONS = _GRPC_EXTRA_OPTIONS + (
7484
('grpc.max_message_length', _MAX_MSG_LENGTH_100MB),
7585
('grpc.max_receive_message_length', _MAX_MSG_LENGTH_100MB),
7686
)
@@ -107,7 +117,7 @@ def _make_instance_stub(client):
107117
return make_secure_stub(
108118
client.credentials, client.user_agent,
109119
bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
110-
INSTANCE_ADMIN_HOST)
120+
INSTANCE_ADMIN_HOST, extra_options=_GRPC_EXTRA_OPTIONS)
111121
else:
112122
return make_insecure_stub(
113123
bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
@@ -127,9 +137,10 @@ def _make_operations_stub(client):
127137
:returns: A gRPC stub object.
128138
"""
129139
if client.emulator_host is None:
130-
return make_secure_stub(client.credentials, client.user_agent,
131-
operations_grpc.OperationsStub,
132-
OPERATIONS_API_HOST)
140+
return make_secure_stub(
141+
client.credentials, client.user_agent,
142+
operations_grpc.OperationsStub,
143+
OPERATIONS_API_HOST, extra_options=_GRPC_EXTRA_OPTIONS)
133144
else:
134145
return make_insecure_stub(operations_grpc.OperationsStub,
135146
client.emulator_host)
@@ -148,7 +159,7 @@ def _make_table_stub(client):
148159
return make_secure_stub(
149160
client.credentials, client.user_agent,
150161
bigtable_table_admin_pb2.BigtableTableAdminStub,
151-
TABLE_ADMIN_HOST)
162+
TABLE_ADMIN_HOST, extra_options=_GRPC_EXTRA_OPTIONS)
152163
else:
153164
return make_insecure_stub(
154165
bigtable_table_admin_pb2.BigtableTableAdminStub,

bigtable/setup.py

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

5252
REQUIREMENTS = [
5353
'google-cloud-core >= 0.23.1, < 0.24dev',
54-
'grpcio >= 1.0.2, < 2.0dev',
54+
'google-gax>=0.15.7, <0.16dev',
5555
]
5656

5757
setup(

bigtable/unit_tests/test_client.py

Lines changed: 48 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,24 @@ def _call_fut(self, client):
3636

3737
return _make_data_stub(client)
3838

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

4344
credentials = _make_credentials()
4445
user_agent = 'you-sir-age-int'
4546
client = _Client(credentials, user_agent)
4647

47-
fake_stub = object()
48-
make_secure_stub_args = []
49-
50-
def mock_make_secure_stub(*args, **kwargs):
51-
make_secure_stub_args.append(args)
52-
make_secure_stub_args.append(kwargs)
53-
return fake_stub
54-
55-
with _Monkey(MUT, make_secure_stub=mock_make_secure_stub):
56-
result = self._call_fut(client)
57-
58-
extra_options = {'extra_options': (
59-
('grpc.max_message_length', 104857600),
60-
('grpc.max_receive_message_length', 104857600)
61-
)}
62-
self.assertIs(result, fake_stub)
63-
self.assertEqual(make_secure_stub_args, [
64-
(
65-
client.credentials,
66-
client.user_agent,
67-
MUT.bigtable_pb2.BigtableStub,
68-
MUT.DATA_API_HOST,
69-
),
70-
extra_options,
71-
])
48+
result = self._call_fut(client)
49+
self.assertIs(result, mock.sentinel.stub)
50+
make_stub.assert_called_once_with(
51+
client.credentials,
52+
client.user_agent,
53+
MUT.bigtable_pb2.BigtableStub,
54+
MUT.DATA_API_HOST,
55+
extra_options=MUT._GRPC_MAX_LENGTH_OPTIONS,
56+
)
7257

7358
def test_with_emulator(self):
7459
from google.cloud._testing import _Monkey
@@ -103,33 +88,24 @@ def _call_fut(self, client):
10388

10489
return _make_instance_stub(client)
10590

106-
def test_without_emulator(self):
107-
from google.cloud._testing import _Monkey
91+
@mock.patch('google.cloud.bigtable.client.make_secure_stub',
92+
return_value=mock.sentinel.stub)
93+
def test_without_emulator(self, make_stub):
10894
from google.cloud.bigtable import client as MUT
10995

11096
credentials = _make_credentials()
11197
user_agent = 'you-sir-age-int'
11298
client = _Client(credentials, user_agent)
11399

114-
fake_stub = object()
115-
make_secure_stub_args = []
116-
117-
def mock_make_secure_stub(*args):
118-
make_secure_stub_args.append(args)
119-
return fake_stub
120-
121-
with _Monkey(MUT, make_secure_stub=mock_make_secure_stub):
122-
result = self._call_fut(client)
123-
124-
self.assertIs(result, fake_stub)
125-
self.assertEqual(make_secure_stub_args, [
126-
(
127-
client.credentials,
128-
client.user_agent,
129-
MUT.bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
130-
MUT.INSTANCE_ADMIN_HOST,
131-
),
132-
])
100+
result = self._call_fut(client)
101+
self.assertIs(result, mock.sentinel.stub)
102+
make_stub.assert_called_once_with(
103+
client.credentials,
104+
client.user_agent,
105+
MUT.bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
106+
MUT.INSTANCE_ADMIN_HOST,
107+
extra_options=MUT._GRPC_EXTRA_OPTIONS,
108+
)
133109

134110
def test_with_emulator(self):
135111
from google.cloud._testing import _Monkey
@@ -164,35 +140,25 @@ def _call_fut(self, client):
164140

165141
return _make_operations_stub(client)
166142

167-
def test_without_emulator(self):
143+
@mock.patch('google.cloud.bigtable.client.make_secure_stub',
144+
return_value=mock.sentinel.stub)
145+
def test_without_emulator(self, make_stub):
168146
from google.longrunning import operations_grpc
169-
170-
from google.cloud._testing import _Monkey
171147
from google.cloud.bigtable import client as MUT
172148

173149
credentials = _make_credentials()
174150
user_agent = 'you-sir-age-int'
175151
client = _Client(credentials, user_agent)
176152

177-
fake_stub = object()
178-
make_secure_stub_args = []
179-
180-
def mock_make_secure_stub(*args):
181-
make_secure_stub_args.append(args)
182-
return fake_stub
183-
184-
with _Monkey(MUT, make_secure_stub=mock_make_secure_stub):
185-
result = self._call_fut(client)
186-
187-
self.assertIs(result, fake_stub)
188-
self.assertEqual(make_secure_stub_args, [
189-
(
190-
client.credentials,
191-
client.user_agent,
192-
operations_grpc.OperationsStub,
193-
MUT.OPERATIONS_API_HOST,
194-
),
195-
])
153+
result = self._call_fut(client)
154+
self.assertIs(result, mock.sentinel.stub)
155+
make_stub.assert_called_once_with(
156+
client.credentials,
157+
client.user_agent,
158+
operations_grpc.OperationsStub,
159+
MUT.OPERATIONS_API_HOST,
160+
extra_options=MUT._GRPC_EXTRA_OPTIONS,
161+
)
196162

197163
def test_with_emulator(self):
198164
from google.longrunning import operations_grpc
@@ -229,33 +195,24 @@ def _call_fut(self, client):
229195

230196
return _make_table_stub(client)
231197

232-
def test_without_emulator(self):
233-
from google.cloud._testing import _Monkey
198+
@mock.patch('google.cloud.bigtable.client.make_secure_stub',
199+
return_value=mock.sentinel.stub)
200+
def test_without_emulator(self, make_stub):
234201
from google.cloud.bigtable import client as MUT
235202

236203
credentials = _make_credentials()
237204
user_agent = 'you-sir-age-int'
238205
client = _Client(credentials, user_agent)
239206

240-
fake_stub = object()
241-
make_secure_stub_args = []
242-
243-
def mock_make_secure_stub(*args):
244-
make_secure_stub_args.append(args)
245-
return fake_stub
246-
247-
with _Monkey(MUT, make_secure_stub=mock_make_secure_stub):
248-
result = self._call_fut(client)
249-
250-
self.assertIs(result, fake_stub)
251-
self.assertEqual(make_secure_stub_args, [
252-
(
253-
client.credentials,
254-
client.user_agent,
255-
MUT.bigtable_table_admin_pb2.BigtableTableAdminStub,
256-
MUT.TABLE_ADMIN_HOST,
257-
),
258-
])
207+
result = self._call_fut(client)
208+
self.assertIs(result, mock.sentinel.stub)
209+
make_stub.assert_called_once_with(
210+
client.credentials,
211+
client.user_agent,
212+
MUT.bigtable_table_admin_pb2.BigtableTableAdminStub,
213+
MUT.TABLE_ADMIN_HOST,
214+
extra_options=MUT._GRPC_EXTRA_OPTIONS,
215+
)
259216

260217
def test_with_emulator(self):
261218
from google.cloud._testing import _Monkey

0 commit comments

Comments
 (0)