Skip to content

Commit d2da843

Browse files
committed
cluster: add application_info
Implement clustr.application_info to make driver send following startup options to server: 1. `APPLICATION_NAME` - ID what application is using driver, example: repo of the application 2. `APPLICATION_VERSION` - Version of the application, example: release version or commit id of the application 3. `CLIENT_ID` - unique id of the client instance, example: pod name All strings.
1 parent d5834c6 commit d2da843

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

cassandra/cluster.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,19 @@ def __init__(self, load_balancing_policy=None, retry_policy=None,
511511
self.graph_options.set_source_analytics()
512512

513513

514+
class ApplicationInfo(object):
515+
def __init__(self, application_name=None, application_version=None, client_id=None):
516+
if not isinstance(application_name, str):
517+
raise TypeError('application_name must be a string')
518+
if not isinstance(application_version, str):
519+
raise TypeError('application_version must be a string')
520+
if not isinstance(client_id, str):
521+
raise TypeError('client_id must be a string')
522+
self.application_name = application_name
523+
self.application_version = application_version
524+
self.client_id = client_id
525+
526+
514527
class ProfileManager(object):
515528

516529
def __init__(self):
@@ -706,6 +719,15 @@ class Cluster(object):
706719
Setting this to :const:`False` disables compression.
707720
"""
708721

722+
application_info = None
723+
"""
724+
An instance of :class:`.cluster.ApplicationInfo` or one of its subclasses.
725+
726+
Defaults to None
727+
728+
When not None makes driver sends information about application that uses driver in startup frame
729+
"""
730+
709731
_auth_provider = None
710732
_auth_provider_callable = None
711733

@@ -1204,6 +1226,7 @@ def __init__(self,
12041226
shard_aware_options=None,
12051227
metadata_request_timeout=None,
12061228
column_encryption_policy=None,
1229+
application_info=None
12071230
):
12081231
"""
12091232
``executor_threads`` defines the number of threads in a pool for handling asynchronous tasks such as
@@ -1329,6 +1352,14 @@ def __init__(self,
13291352
raise TypeError("address_translator should not be a class, it should be an instance of that class")
13301353
self.address_translator = address_translator
13311354

1355+
if application_info is not None:
1356+
if isinstance(application_info, type):
1357+
raise TypeError("application_info should not be a class, it should be an instance of ApplicationInfo class")
1358+
if not isinstance(application_info, ApplicationInfo):
1359+
raise TypeError(
1360+
"application_info should be an instance of ApplicationInfo class")
1361+
self.application_info = application_info
1362+
13321363
if timestamp_generator is not None:
13331364
if not callable(timestamp_generator):
13341365
raise ValueError("timestamp_generator must be callable")
@@ -1779,6 +1810,7 @@ def _make_connection_kwargs(self, endpoint, kwargs_dict):
17791810
kwargs_dict.setdefault('user_type_map', self._user_types)
17801811
kwargs_dict.setdefault('allow_beta_protocol_version', self.allow_beta_protocol_version)
17811812
kwargs_dict.setdefault('no_compact', self.no_compact)
1813+
kwargs_dict.setdefault('application_info', self.application_info)
17821814

17831815
return kwargs_dict
17841816

cassandra/connection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def __init__(self, host='127.0.0.1', port=9042, authenticator=None,
774774
cql_version=None, protocol_version=ProtocolVersion.MAX_SUPPORTED, is_control_connection=False,
775775
user_type_map=None, connect_timeout=None, allow_beta_protocol_version=False, no_compact=False,
776776
ssl_context=None, owning_pool=None, shard_id=None, total_shards=None,
777-
on_orphaned_stream_released=None):
777+
on_orphaned_stream_released=None, application_info=None):
778778
# TODO next major rename host to endpoint and remove port kwarg.
779779
self.endpoint = host if isinstance(host, EndPoint) else DefaultEndPoint(host, port)
780780

@@ -797,6 +797,7 @@ def __init__(self, host='127.0.0.1', port=9042, authenticator=None,
797797
self._socket_writable = True
798798
self.orphaned_request_ids = set()
799799
self._on_orphaned_stream_released = on_orphaned_stream_released
800+
self.application_info = application_info
800801

801802
if ssl_options:
802803
self.ssl_options.update(self.endpoint.ssl_options or {})
@@ -1439,6 +1440,13 @@ def _send_startup_message(self, compression=None, no_compact=False, extra_option
14391440
opts = {'DRIVER_NAME': DRIVER_NAME,
14401441
'DRIVER_VERSION': DRIVER_VERSION,
14411442
**extra_options}
1443+
if self.application_info:
1444+
if self.application_info.application_name:
1445+
opts['APPLICATION_NAME'] = self.application_info.application_name
1446+
if self.application_info.application_version:
1447+
opts['APPLICATION_VERSION'] = self.application_info.application_version
1448+
if self.application_info.client_id:
1449+
opts['CLIENT_ID'] = self.application_info.client_id
14421450
if compression:
14431451
opts['COMPRESSION'] = compression
14441452
if no_compact:

0 commit comments

Comments
 (0)