Skip to content

Commit eabe4ba

Browse files
authoredMar 12, 2025··
fix: correct user agent header to track calls as veneer instead of gapic (#909)
* fix: correct user agent header to track calls as veneer instead of gapic * add unit test * move where client info is changed * correction * change header for v1beta2, too
1 parent 0af12b1 commit eabe4ba

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
 

‎google/cloud/bigquery_storage_v1/client.py

+16
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
from __future__ import absolute_import
2323

24+
from google.api_core import gapic_v1
2425
import google.api_core.gapic_v1.method
2526

27+
from google.cloud.bigquery_storage_v1 import gapic_version as package_version
2628
from google.cloud.bigquery_storage_v1 import reader
2729
from google.cloud.bigquery_storage_v1.services import big_query_read, big_query_write
2830

@@ -31,13 +33,22 @@
3133
"https://www.googleapis.com/auth/cloud-platform",
3234
)
3335

36+
VENEER_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
37+
client_library_version=package_version.__version__
38+
)
39+
3440

3541
class BigQueryReadClient(big_query_read.BigQueryReadClient):
3642
"""Client for interacting with BigQuery Storage API.
3743
3844
The BigQuery storage API can be used to read data stored in BigQuery.
3945
"""
4046

47+
def __init__(self, **kwargs):
48+
if "client_info" not in kwargs:
49+
kwargs["client_info"] = VENEER_CLIENT_INFO
50+
super().__init__(**kwargs)
51+
4152
def read_rows(
4253
self,
4354
name,
@@ -140,3 +151,8 @@ def read_rows(
140151

141152
class BigQueryWriteClient(big_query_write.BigQueryWriteClient):
142153
__doc__ = big_query_write.BigQueryWriteClient.__doc__
154+
155+
def __init__(self, **kwargs):
156+
if "client_info" not in kwargs:
157+
kwargs["client_info"] = VENEER_CLIENT_INFO
158+
super().__init__(**kwargs)

‎google/cloud/bigquery_storage_v1beta2/client.py

+16
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
This is the base from which all interactions with the API occur.
2020
"""
2121

22+
from google.api_core import gapic_v1
2223
import google.api_core.gapic_v1.method
2324
import google.api_core.retry
2425

26+
from google.cloud.bigquery_storage_v1 import gapic_version as package_version
2527
from google.cloud.bigquery_storage_v1 import reader
2628
from google.cloud.bigquery_storage_v1beta2.services import (
2729
big_query_read,
@@ -33,13 +35,22 @@
3335
"https://www.googleapis.com/auth/cloud-platform",
3436
)
3537

38+
VENEER_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
39+
client_library_version=package_version.__version__
40+
)
41+
3642

3743
class BigQueryReadClient(big_query_read.BigQueryReadClient):
3844
"""Client for interacting with BigQuery Storage API.
3945
4046
The BigQuery storage API can be used to read data stored in BigQuery.
4147
"""
4248

49+
def __init__(self, **kwargs):
50+
if "client_info" not in kwargs:
51+
kwargs["client_info"] = VENEER_CLIENT_INFO
52+
super().__init__(**kwargs)
53+
4354
def read_rows(
4455
self,
4556
name,
@@ -142,3 +153,8 @@ def read_rows(
142153

143154
class BigQueryWriteClient(big_query_write.BigQueryWriteClient):
144155
__doc__ = big_query_write.BigQueryWriteClient.__doc__
156+
157+
def __init__(self, **kwargs):
158+
if "client_info" not in kwargs:
159+
kwargs["client_info"] = VENEER_CLIENT_INFO
160+
super().__init__(**kwargs)

‎tests/unit/test_client_v1.py

+30
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import importlib
1516
from unittest import mock
1617

1718
from google.api_core.gapic_v1 import client_info
19+
from google.auth import credentials
1820
import pytest
1921

2022
from google.cloud.bigquery_storage import types
@@ -115,3 +117,31 @@ def test_read_rows(mock_transport, client_under_test):
115117
mock_transport.create_read_session.read_rows(
116118
expected_request, metadata=mock.ANY, timeout=mock.ANY
117119
)
120+
121+
122+
@pytest.mark.parametrize(
123+
"module_under_test",
124+
["google.cloud.bigquery_storage_v1", "google.cloud.bigquery_storage_v1beta2"],
125+
)
126+
def test_init_default_client_info(module_under_test):
127+
from google.api_core.gapic_v1.client_info import METRICS_METADATA_KEY
128+
129+
mut = importlib.import_module(module_under_test)
130+
131+
creds = mock.Mock(spec=credentials.Credentials)
132+
client = mut.BigQueryWriteClient(credentials=creds)
133+
134+
installed_version = mut.__version__
135+
expected_client_info = f"gccl/{installed_version}"
136+
137+
for wrapped_method in client.transport._wrapped_methods.values():
138+
user_agent = next(
139+
(
140+
header_value
141+
for header, header_value in wrapped_method._metadata
142+
if header == METRICS_METADATA_KEY
143+
),
144+
None,
145+
)
146+
assert user_agent is not None
147+
assert expected_client_info in user_agent

0 commit comments

Comments
 (0)
Please sign in to comment.