Skip to content

Commit ba19c54

Browse files
committed
Ability to pass additional info to sdk header
1 parent 6116843 commit ba19c54

5 files changed

Lines changed: 25 additions & 4 deletions

File tree

ydb/_utilities.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ def future():
3838
return futures.Future()
3939

4040

41-
def x_ydb_sdk_build_info_header():
42-
return ("x-ydb-sdk-build-info", "ydb-python-sdk/" + ydb_version.VERSION)
41+
def x_ydb_sdk_build_info_header(additional_sdk_headers):
42+
sdk_header_list = ["ydb-python-sdk/" + ydb_version.VERSION]
43+
sdk_header_list.extend(additional_sdk_headers)
44+
return ("x-ydb-sdk-build-info", ";".join(sdk_header_list))
4345

4446

4547
def is_secure_protocol(endpoint):

ydb/_utilities_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
22

33
from ydb._utilities import check_module_exists
4+
from ydb._utilities import x_ydb_sdk_build_info_header
5+
from .ydb_version import VERSION
46

57

68
@pytest.mark.parametrize(
@@ -9,3 +11,15 @@
911
)
1012
def test_check_module_exists(path, result):
1113
assert check_module_exists(path) == result
14+
15+
16+
def test_x_ydb_sdk_build_info_header():
17+
assert x_ydb_sdk_build_info_header(()) == ("x-ydb-sdk-build-info", "ydb-python-sdk/" + VERSION)
18+
assert x_ydb_sdk_build_info_header(("lib1/0.1.0",)) == (
19+
"x-ydb-sdk-build-info",
20+
"ydb-python-sdk/" + VERSION + ";lib1/0.1.0",
21+
)
22+
assert x_ydb_sdk_build_info_header(("lib1/0.1.0", "lib2/0.2.0")) == (
23+
"x-ydb-sdk-build-info",
24+
"ydb-python-sdk/" + VERSION + ";lib1/0.1.0;lib2/0.2.0",
25+
)

ydb/aio/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async def _construct_metadata(
7070
if settings.request_type is not None:
7171
metadata.append((YDB_REQUEST_TYPE_HEADER, settings.request_type))
7272

73-
metadata.append(_utilities.x_ydb_sdk_build_info_header())
73+
metadata.append(_utilities.x_ydb_sdk_build_info_header(getattr(driver_config, "_additional_sdk_headers", ())))
7474
return metadata
7575

7676

ydb/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def _construct_metadata(driver_config, settings):
175175
metadata.append((YDB_REQUEST_TYPE_HEADER, settings.request_type))
176176
metadata.extend(getattr(settings, "headers", []))
177177

178-
metadata.append(_utilities.x_ydb_sdk_build_info_header())
178+
metadata.append(_utilities.x_ydb_sdk_build_info_header(getattr(driver_config, "_additional_sdk_headers", ())))
179179
return metadata
180180

181181

ydb/driver.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class DriverConfig(object):
110110
"discovery_request_timeout",
111111
"compression",
112112
"disable_discovery",
113+
"_additional_sdk_headers",
113114
)
114115

115116
def __init__(
@@ -135,6 +136,8 @@ def __init__(
135136
discovery_request_timeout: int = 10,
136137
compression: Optional[grpc.Compression] = None,
137138
disable_discovery: bool = False,
139+
*,
140+
_additional_sdk_headers: Tuple[str, ...] = (),
138141
) -> None:
139142
"""
140143
A driver config to initialize a driver instance
@@ -156,6 +159,7 @@ def __init__(
156159
:param grpc_lb_policy_name: A load balancing policy to be used for discovery channel construction. Default value is `round_round`
157160
:param discovery_request_timeout: A default timeout to complete the discovery. The default value is 10 seconds.
158161
:param disable_discovery: If True, endpoint discovery is disabled and only the start endpoint is used for all requests.
162+
:param _additional_sdk_headers: Reserved for SDK integrations (e.g. dbapi, sqlalchemy). Do not use in application code.
159163
160164
"""
161165
self.endpoint = endpoint
@@ -184,6 +188,7 @@ def __init__(
184188
self.discovery_request_timeout = discovery_request_timeout
185189
self.compression = compression
186190
self.disable_discovery = disable_discovery
191+
self._additional_sdk_headers = _additional_sdk_headers
187192

188193
def set_database(self, database: str) -> "DriverConfig":
189194
self.database = database

0 commit comments

Comments
 (0)