From dbcb01aae1a9a6f7bb35753f6f58e85fd06aed2a Mon Sep 17 00:00:00 2001 From: sjaanus Date: Thu, 27 Feb 2025 14:06:25 +0200 Subject: [PATCH 01/18] new --- UnleashClient/__init__.py | 5 ++++- UnleashClient/periodic_tasks/send_metrics.py | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/UnleashClient/__init__.py b/UnleashClient/__init__.py index 4b005e5..022202b 100644 --- a/UnleashClient/__init__.py +++ b/UnleashClient/__init__.py @@ -110,6 +110,7 @@ def __init__( self.unleash_app_name = app_name self.unleash_environment = environment self.unleash_instance_id = instance_id + self.connection_id = str(uuid.uuid4()) self.unleash_refresh_interval = refresh_interval self.unleash_request_timeout = request_timeout self.unleash_request_retries = request_retries @@ -215,7 +216,8 @@ def initialize_client(self, fetch_toggles: bool = True) -> None: try: headers = { **self.unleash_custom_headers, - "unleash-connection-id": str(uuid.uuid4()), + "unleash-connection-id": self.connection_id, + "unleash-interval-id": self.unleash_refresh_interval, "unleash-appname": self.unleash_app_name, "unleash-sdk": f"{SDK_NAME}:{SDK_VERSION}", } @@ -224,6 +226,7 @@ def initialize_client(self, fetch_toggles: bool = True) -> None: metrics_args = { "url": self.unleash_url, "app_name": self.unleash_app_name, + "connection_id": self.connection_id, "instance_id": self.unleash_instance_id, "headers": headers, "custom_options": self.unleash_custom_options, diff --git a/UnleashClient/periodic_tasks/send_metrics.py b/UnleashClient/periodic_tasks/send_metrics.py index ef532ca..55fed20 100644 --- a/UnleashClient/periodic_tasks/send_metrics.py +++ b/UnleashClient/periodic_tasks/send_metrics.py @@ -12,6 +12,7 @@ def aggregate_and_send_metrics( url: str, app_name: str, instance_id: str, + connection_id: str, headers: dict, custom_options: dict, request_timeout: int, @@ -22,6 +23,7 @@ def aggregate_and_send_metrics( metrics_request = { "appName": app_name, "instanceId": instance_id, + "connectionId": connection_id, "bucket": metrics_bucket, "platformName": python_implementation(), "platformVersion": python_version(), From 898d8b12d760a28c20228176fcfb830a9f9cda27 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 28 Feb 2025 08:53:41 +0200 Subject: [PATCH 02/18] Updates --- UnleashClient/__init__.py | 8 +++++++- UnleashClient/api/register.py | 2 ++ tests/unit_tests/api/test_metrics.py | 6 ++++++ tests/unit_tests/api/test_register.py | 5 ++++- tests/unit_tests/test_client.py | 2 ++ tests/utilities/mocks/mock_metrics.py | 1 + tests/utilities/testing_constants.py | 1 + 7 files changed, 23 insertions(+), 2 deletions(-) diff --git a/UnleashClient/__init__.py b/UnleashClient/__init__.py index 022202b..08a4965 100644 --- a/UnleashClient/__init__.py +++ b/UnleashClient/__init__.py @@ -184,6 +184,10 @@ def __init__( engine=self.engine, ) + @property + def unleash_refresh_interval_str_millis(self) -> str: + return str(self.unleash_refresh_interval * 1000) + def initialize_client(self, fetch_toggles: bool = True) -> None: """ Initializes client and starts communication with central unleash server(s). @@ -217,7 +221,7 @@ def initialize_client(self, fetch_toggles: bool = True) -> None: headers = { **self.unleash_custom_headers, "unleash-connection-id": self.connection_id, - "unleash-interval-id": self.unleash_refresh_interval, + "unleash-interval-id": self.unleash_refresh_interval_str_millis, "unleash-appname": self.unleash_app_name, "unleash-sdk": f"{SDK_NAME}:{SDK_VERSION}", } @@ -227,6 +231,7 @@ def initialize_client(self, fetch_toggles: bool = True) -> None: "url": self.unleash_url, "app_name": self.unleash_app_name, "connection_id": self.connection_id, + "interval": self.unleash_refresh_interval, "instance_id": self.unleash_instance_id, "headers": headers, "custom_options": self.unleash_custom_options, @@ -240,6 +245,7 @@ def initialize_client(self, fetch_toggles: bool = True) -> None: self.unleash_url, self.unleash_app_name, self.unleash_instance_id, + self.connection_id, self.unleash_metrics_interval, headers, self.unleash_custom_options, diff --git a/UnleashClient/api/register.py b/UnleashClient/api/register.py index 24c0d6e..1802448 100644 --- a/UnleashClient/api/register.py +++ b/UnleashClient/api/register.py @@ -21,6 +21,7 @@ def register_client( url: str, app_name: str, instance_id: str, + connection_id: str, metrics_interval: int, headers: dict, custom_options: dict, @@ -47,6 +48,7 @@ def register_client( registration_request = { "appName": app_name, "instanceId": instance_id, + "connectionId": connection_id, "sdkVersion": f"{SDK_NAME}:{SDK_VERSION}", "strategies": [*supported_strategies], "started": datetime.now(timezone.utc).isoformat(), diff --git a/tests/unit_tests/api/test_metrics.py b/tests/unit_tests/api/test_metrics.py index 2e3d356..88ebc54 100644 --- a/tests/unit_tests/api/test_metrics.py +++ b/tests/unit_tests/api/test_metrics.py @@ -1,3 +1,5 @@ +import json + import responses from pytest import mark, param from requests import ConnectionError @@ -36,5 +38,9 @@ def test_send_metrics(payload, status, expected): URL, MOCK_METRICS_REQUEST, CUSTOM_HEADERS, CUSTOM_OPTIONS, REQUEST_TIMEOUT ) + request = json.loads(responses.calls[0].request.body) + assert len(responses.calls) == 1 assert expected(result) + + assert request["connectionId"] == MOCK_METRICS_REQUEST.get('connectionId') diff --git a/tests/unit_tests/api/test_register.py b/tests/unit_tests/api/test_register.py index 3cfc6e6..0aadaac 100644 --- a/tests/unit_tests/api/test_register.py +++ b/tests/unit_tests/api/test_register.py @@ -11,7 +11,7 @@ INSTANCE_ID, METRICS_INTERVAL, REQUEST_TIMEOUT, - URL, + URL, CONNECTION_ID, ) from UnleashClient.api import register_client from UnleashClient.constants import CLIENT_SPEC_VERSION, REGISTER_URL @@ -40,6 +40,7 @@ def test_register_client(payload, status, expected): URL, APP_NAME, INSTANCE_ID, + CONNECTION_ID, METRICS_INTERVAL, CUSTOM_HEADERS, CUSTOM_OPTIONS, @@ -59,6 +60,7 @@ def test_register_includes_metadata(): URL, APP_NAME, INSTANCE_ID, + CONNECTION_ID, METRICS_INTERVAL, CUSTOM_HEADERS, CUSTOM_OPTIONS, @@ -71,5 +73,6 @@ def test_register_includes_metadata(): assert request["yggdrasilVersion"] is not None assert request["specVersion"] == CLIENT_SPEC_VERSION + assert request["connectionId"] == CONNECTION_ID assert request["platformName"] is not None assert request["platformVersion"] is not None diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index c114eb4..d4bb4b6 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1044,11 +1044,13 @@ def test_identification_headers_sent_and_consistent(unleash_client): unleash_client.initialize_client() connection_id = responses.calls[0].request.headers["UNLEASH-CONNECTION-ID"] + # interval = responses.calls[0].request.headers["UNLEASH-INTERVAL"] app_name = responses.calls[0].request.headers["UNLEASH-APPNAME"] sdk = responses.calls[0].request.headers["UNLEASH-SDK"] for api_call in responses.calls: assert api_call.request.headers["UNLEASH-CONNECTION-ID"] == connection_id + # assert api_call.request.headers["UNLEASH-INTERVAL"] == interval assert api_call.request.headers["UNLEASH-APPNAME"] == app_name assert api_call.request.headers["UNLEASH-SDK"] == sdk diff --git a/tests/utilities/mocks/mock_metrics.py b/tests/utilities/mocks/mock_metrics.py index ceaa8ab..66ab96e 100644 --- a/tests/utilities/mocks/mock_metrics.py +++ b/tests/utilities/mocks/mock_metrics.py @@ -1,6 +1,7 @@ MOCK_METRICS_REQUEST = { "appName": "appName", "instanceId": "instanceId", + "connectionId": "connectionId", "bucket": { "start": "2016-11-03T07:16:43.572Z", "stop": "2016-11-03T07:16:53.572Z", diff --git a/tests/utilities/testing_constants.py b/tests/utilities/testing_constants.py index ca05b6a..1ece2f4 100644 --- a/tests/utilities/testing_constants.py +++ b/tests/utilities/testing_constants.py @@ -4,6 +4,7 @@ APP_NAME = "pytest" ENVIRONMENT = "unit" INSTANCE_ID = "123" +CONNECTION_ID = "test-connection-id" REFRESH_INTERVAL = 3 REFRESH_JITTER = None METRICS_INTERVAL = 2 From 69a7fde71970bb23a97a260874d9c5dd5122ddfd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Feb 2025 06:53:50 +0000 Subject: [PATCH 03/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unit_tests/api/test_metrics.py | 2 +- tests/unit_tests/api/test_register.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit_tests/api/test_metrics.py b/tests/unit_tests/api/test_metrics.py index 88ebc54..35ff98d 100644 --- a/tests/unit_tests/api/test_metrics.py +++ b/tests/unit_tests/api/test_metrics.py @@ -43,4 +43,4 @@ def test_send_metrics(payload, status, expected): assert len(responses.calls) == 1 assert expected(result) - assert request["connectionId"] == MOCK_METRICS_REQUEST.get('connectionId') + assert request["connectionId"] == MOCK_METRICS_REQUEST.get("connectionId") diff --git a/tests/unit_tests/api/test_register.py b/tests/unit_tests/api/test_register.py index 0aadaac..972f56a 100644 --- a/tests/unit_tests/api/test_register.py +++ b/tests/unit_tests/api/test_register.py @@ -6,12 +6,13 @@ from tests.utilities.testing_constants import ( APP_NAME, + CONNECTION_ID, CUSTOM_HEADERS, CUSTOM_OPTIONS, INSTANCE_ID, METRICS_INTERVAL, REQUEST_TIMEOUT, - URL, CONNECTION_ID, + URL, ) from UnleashClient.api import register_client from UnleashClient.constants import CLIENT_SPEC_VERSION, REGISTER_URL From efa1174e175d8346dd2697a549a3ceeee9a01050 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 28 Feb 2025 09:54:36 +0200 Subject: [PATCH 04/18] Updates --- UnleashClient/__init__.py | 1 - tests/unit_tests/test_client.py | 37 +++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/UnleashClient/__init__.py b/UnleashClient/__init__.py index 08a4965..1922305 100644 --- a/UnleashClient/__init__.py +++ b/UnleashClient/__init__.py @@ -231,7 +231,6 @@ def initialize_client(self, fetch_toggles: bool = True) -> None: "url": self.unleash_url, "app_name": self.unleash_app_name, "connection_id": self.connection_id, - "interval": self.unleash_refresh_interval, "instance_id": self.unleash_instance_id, "headers": headers, "custom_options": self.unleash_custom_options, diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index d4bb4b6..dc7e6f9 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1,3 +1,4 @@ +import json import time import warnings from datetime import datetime, timezone @@ -1044,13 +1045,11 @@ def test_identification_headers_sent_and_consistent(unleash_client): unleash_client.initialize_client() connection_id = responses.calls[0].request.headers["UNLEASH-CONNECTION-ID"] - # interval = responses.calls[0].request.headers["UNLEASH-INTERVAL"] app_name = responses.calls[0].request.headers["UNLEASH-APPNAME"] sdk = responses.calls[0].request.headers["UNLEASH-SDK"] for api_call in responses.calls: assert api_call.request.headers["UNLEASH-CONNECTION-ID"] == connection_id - # assert api_call.request.headers["UNLEASH-INTERVAL"] == interval assert api_call.request.headers["UNLEASH-APPNAME"] == app_name assert api_call.request.headers["UNLEASH-SDK"] == sdk @@ -1080,3 +1079,37 @@ def test_identification_headers_unique_connection_id(): "UNLEASH-CONNECTION-ID" ] assert connection_id_first_client != connection_id_second_client + +@responses.activate +def test_identification_values_are_passed_in(): + responses.add(responses.POST, URL + REGISTER_URL, json={}, status=202) + responses.add( + responses.GET, URL + FEATURES_URL, json=MOCK_FEATURE_RESPONSE, status=200 + ) + responses.add(responses.POST, URL + METRICS_URL, json={}, status=202) + + input_interval = 1 + unleash_client = UnleashClient( + URL, APP_NAME, refresh_interval=input_interval, metrics_interval=1 + ) + expected_connection_id = unleash_client.connection_id + expected_interval = str(input_interval * 1000) + + unleash_client.initialize_client() + register_request = responses.calls[0].request + reqister_body = json.loads(register_request.body) + + assert register_request.headers[ "UNLEASH-CONNECTION-ID"] == expected_connection_id + assert reqister_body['connectionId'] == expected_connection_id + + features_request = responses.calls[1].request + + assert features_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id + assert features_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval + + time.sleep(2) + metrics_request = responses.calls[2].request + + assert metrics_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id + assert metrics_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval + From 1eff13934f628a7ab9cfa0e1840f30c4226837e8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Feb 2025 07:55:53 +0000 Subject: [PATCH 05/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unit_tests/test_client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index dc7e6f9..4b58a7d 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1080,6 +1080,7 @@ def test_identification_headers_unique_connection_id(): ] assert connection_id_first_client != connection_id_second_client + @responses.activate def test_identification_values_are_passed_in(): responses.add(responses.POST, URL + REGISTER_URL, json={}, status=202) @@ -1099,8 +1100,8 @@ def test_identification_values_are_passed_in(): register_request = responses.calls[0].request reqister_body = json.loads(register_request.body) - assert register_request.headers[ "UNLEASH-CONNECTION-ID"] == expected_connection_id - assert reqister_body['connectionId'] == expected_connection_id + assert register_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id + assert reqister_body["connectionId"] == expected_connection_id features_request = responses.calls[1].request @@ -1112,4 +1113,3 @@ def test_identification_values_are_passed_in(): assert metrics_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id assert metrics_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval - From e7f7b202f5bc30467f16bd6bfff66b0989ebfd7d Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 28 Feb 2025 10:18:13 +0200 Subject: [PATCH 06/18] Updates --- tests/unit_tests/periodic/test_aggregate_and_send_metrics.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/periodic/test_aggregate_and_send_metrics.py b/tests/unit_tests/periodic/test_aggregate_and_send_metrics.py index 8fd4807..c7dde88 100644 --- a/tests/unit_tests/periodic/test_aggregate_and_send_metrics.py +++ b/tests/unit_tests/periodic/test_aggregate_and_send_metrics.py @@ -9,7 +9,7 @@ CUSTOM_OPTIONS, INSTANCE_ID, REQUEST_TIMEOUT, - URL, + URL, CONNECTION_ID, ) from UnleashClient.constants import ( CLIENT_SPEC_VERSION, @@ -31,6 +31,7 @@ def test_no_metrics(): URL, APP_NAME, INSTANCE_ID, + CONNECTION_ID, CUSTOM_HEADERS, CUSTOM_OPTIONS, REQUEST_TIMEOUT, @@ -51,6 +52,7 @@ def test_metrics_metadata_is_sent(): URL, APP_NAME, INSTANCE_ID, + CONNECTION_ID, CUSTOM_HEADERS, CUSTOM_OPTIONS, REQUEST_TIMEOUT, From e02111fc44a7a46ee281e662b83ec4d8c649044b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Feb 2025 08:18:32 +0000 Subject: [PATCH 07/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unit_tests/periodic/test_aggregate_and_send_metrics.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/periodic/test_aggregate_and_send_metrics.py b/tests/unit_tests/periodic/test_aggregate_and_send_metrics.py index c7dde88..8621d94 100644 --- a/tests/unit_tests/periodic/test_aggregate_and_send_metrics.py +++ b/tests/unit_tests/periodic/test_aggregate_and_send_metrics.py @@ -5,11 +5,12 @@ from tests.utilities.testing_constants import ( APP_NAME, + CONNECTION_ID, CUSTOM_HEADERS, CUSTOM_OPTIONS, INSTANCE_ID, REQUEST_TIMEOUT, - URL, CONNECTION_ID, + URL, ) from UnleashClient.constants import ( CLIENT_SPEC_VERSION, From c2e013512799cbe3f3b6082d6da797f86a51f03e Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 28 Feb 2025 12:15:21 +0200 Subject: [PATCH 08/18] Updates --- UnleashClient/__init__.py | 6 +++++- tests/unit_tests/test_client.py | 10 +++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/UnleashClient/__init__.py b/UnleashClient/__init__.py index 1922305..89f821d 100644 --- a/UnleashClient/__init__.py +++ b/UnleashClient/__init__.py @@ -110,7 +110,7 @@ def __init__( self.unleash_app_name = app_name self.unleash_environment = environment self.unleash_instance_id = instance_id - self.connection_id = str(uuid.uuid4()) + self._connection_id = str(uuid.uuid4()) self.unleash_refresh_interval = refresh_interval self.unleash_request_timeout = request_timeout self.unleash_request_retries = request_retries @@ -188,6 +188,10 @@ def __init__( def unleash_refresh_interval_str_millis(self) -> str: return str(self.unleash_refresh_interval * 1000) + @property + def connection_id(self): + return self._connection_id + def initialize_client(self, fetch_toggles: bool = True) -> None: """ Initializes client and starts communication with central unleash server(s). diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index 4b58a7d..db60eb6 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1082,19 +1082,15 @@ def test_identification_headers_unique_connection_id(): @responses.activate -def test_identification_values_are_passed_in(): +def test_identification_values_are_passed_in(unleash_client): responses.add(responses.POST, URL + REGISTER_URL, json={}, status=202) responses.add( responses.GET, URL + FEATURES_URL, json=MOCK_FEATURE_RESPONSE, status=200 ) responses.add(responses.POST, URL + METRICS_URL, json={}, status=202) - input_interval = 1 - unleash_client = UnleashClient( - URL, APP_NAME, refresh_interval=input_interval, metrics_interval=1 - ) expected_connection_id = unleash_client.connection_id - expected_interval = str(input_interval * 1000) + expected_interval = str(REFRESH_INTERVAL * 1000) unleash_client.initialize_client() register_request = responses.calls[0].request @@ -1108,7 +1104,7 @@ def test_identification_values_are_passed_in(): assert features_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id assert features_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval - time.sleep(2) + time.sleep(METRICS_INTERVAL + 2) metrics_request = responses.calls[2].request assert metrics_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id From b0913a647d2584c8a7df06dfb0768c1f6c04ce86 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 28 Feb 2025 13:06:07 +0200 Subject: [PATCH 09/18] Updates --- tests/unit_tests/test_client.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index db60eb6..19c2575 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1082,30 +1082,35 @@ def test_identification_headers_unique_connection_id(): @responses.activate -def test_identification_values_are_passed_in(unleash_client): +def test_identification_values_are_passed_in(): responses.add(responses.POST, URL + REGISTER_URL, json={}, status=202) responses.add( responses.GET, URL + FEATURES_URL, json=MOCK_FEATURE_RESPONSE, status=200 ) responses.add(responses.POST, URL + METRICS_URL, json={}, status=202) + input_interval = 1 + unleash_client = UnleashClient( + URL, APP_NAME, refresh_interval=input_interval, metrics_interval=1 + ) expected_connection_id = unleash_client.connection_id - expected_interval = str(REFRESH_INTERVAL * 1000) + expected_interval = str(input_interval * 1000) unleash_client.initialize_client() register_request = responses.calls[0].request reqister_body = json.loads(register_request.body) - assert register_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id - assert reqister_body["connectionId"] == expected_connection_id + assert register_request.headers[ "UNLEASH-CONNECTION-ID"] == expected_connection_id + assert reqister_body['connectionId'] == expected_connection_id features_request = responses.calls[1].request assert features_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id assert features_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval - time.sleep(METRICS_INTERVAL + 2) + time.sleep(2) metrics_request = responses.calls[2].request assert metrics_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id assert metrics_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval + From 06e82561a02702c062a7e018dc176a69cc173590 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Feb 2025 11:09:33 +0000 Subject: [PATCH 10/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unit_tests/test_client.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index 19c2575..4b58a7d 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1100,8 +1100,8 @@ def test_identification_values_are_passed_in(): register_request = responses.calls[0].request reqister_body = json.loads(register_request.body) - assert register_request.headers[ "UNLEASH-CONNECTION-ID"] == expected_connection_id - assert reqister_body['connectionId'] == expected_connection_id + assert register_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id + assert reqister_body["connectionId"] == expected_connection_id features_request = responses.calls[1].request @@ -1113,4 +1113,3 @@ def test_identification_values_are_passed_in(): assert metrics_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id assert metrics_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval - From c1501cb525b9df4f2f57ff9525953bd58d284e62 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 28 Feb 2025 13:29:31 +0200 Subject: [PATCH 11/18] Updates --- UnleashClient/__init__.py | 1 - tests/unit_tests/test_client.py | 11 ++++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/UnleashClient/__init__.py b/UnleashClient/__init__.py index 89f821d..1988db6 100644 --- a/UnleashClient/__init__.py +++ b/UnleashClient/__init__.py @@ -289,7 +289,6 @@ def initialize_client(self, fetch_toggles: bool = True) -> None: executor=self.unleash_executor_name, kwargs=job_args, ) - if not self.unleash_disable_metrics: self.metric_job = self.unleash_scheduler.add_job( aggregate_and_send_metrics, diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index 19c2575..dca3c6c 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1098,10 +1098,12 @@ def test_identification_values_are_passed_in(): unleash_client.initialize_client() register_request = responses.calls[0].request - reqister_body = json.loads(register_request.body) + register_body = json.loads(register_request.body) assert register_request.headers[ "UNLEASH-CONNECTION-ID"] == expected_connection_id - assert reqister_body['connectionId'] == expected_connection_id + assert register_body['connectionId'] == expected_connection_id + + unleash_client.is_enabled("registerMetricsFlag") features_request = responses.calls[1].request @@ -1109,8 +1111,11 @@ def test_identification_values_are_passed_in(): assert features_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval time.sleep(2) - metrics_request = responses.calls[2].request + metrics_request = [call for call in responses.calls if METRICS_URL in call.request.url][0].request + metrics_body = json.loads(metrics_request.body) + assert metrics_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id assert metrics_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval + assert metrics_body['connectionId'] == expected_connection_id From 6b66c386090aec29ebce36973b8c8fa050f81b91 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Feb 2025 11:30:05 +0000 Subject: [PATCH 12/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unit_tests/test_client.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index dca3c6c..d4100ca 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1100,8 +1100,8 @@ def test_identification_values_are_passed_in(): register_request = responses.calls[0].request register_body = json.loads(register_request.body) - assert register_request.headers[ "UNLEASH-CONNECTION-ID"] == expected_connection_id - assert register_body['connectionId'] == expected_connection_id + assert register_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id + assert register_body["connectionId"] == expected_connection_id unleash_client.is_enabled("registerMetricsFlag") @@ -1111,11 +1111,11 @@ def test_identification_values_are_passed_in(): assert features_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval time.sleep(2) - metrics_request = [call for call in responses.calls if METRICS_URL in call.request.url][0].request + metrics_request = [ + call for call in responses.calls if METRICS_URL in call.request.url + ][0].request metrics_body = json.loads(metrics_request.body) - assert metrics_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id assert metrics_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval - assert metrics_body['connectionId'] == expected_connection_id - + assert metrics_body["connectionId"] == expected_connection_id From a620fe1b91e99dc4864e146c8b08b204900cceb6 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Tue, 4 Mar 2025 09:30:03 +0200 Subject: [PATCH 13/18] Fix intervals --- UnleashClient/__init__.py | 24 +++++++++++++++++++----- tests/unit_tests/test_client.py | 14 ++++++++------ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/UnleashClient/__init__.py b/UnleashClient/__init__.py index 1988db6..996d8c4 100644 --- a/UnleashClient/__init__.py +++ b/UnleashClient/__init__.py @@ -5,6 +5,7 @@ import warnings from dataclasses import asdict from datetime import datetime, timezone +from sys import base_prefix from typing import Any, Callable, Dict, Optional from apscheduler.executors.pool import ThreadPoolExecutor @@ -188,6 +189,10 @@ def __init__( def unleash_refresh_interval_str_millis(self) -> str: return str(self.unleash_refresh_interval * 1000) + @property + def unleash_metrics_interval_str_millis(self) -> str: + return str(self.unleash_metrics_interval * 1000) + @property def connection_id(self): return self._connection_id @@ -222,21 +227,25 @@ def initialize_client(self, fetch_toggles: bool = True) -> None: if not self.is_initialized: # pylint: disable=no-else-raise try: - headers = { + base_headers = { **self.unleash_custom_headers, "unleash-connection-id": self.connection_id, - "unleash-interval-id": self.unleash_refresh_interval_str_millis, "unleash-appname": self.unleash_app_name, "unleash-sdk": f"{SDK_NAME}:{SDK_VERSION}", } + metrics_headers = { + **base_headers, + "unleash-interval": self.unleash_metrics_interval_str_millis, + } + # Setup metrics_args = { "url": self.unleash_url, "app_name": self.unleash_app_name, "connection_id": self.connection_id, "instance_id": self.unleash_instance_id, - "headers": headers, + "headers": metrics_headers, "custom_options": self.unleash_custom_options, "request_timeout": self.unleash_request_timeout, "engine": self.engine, @@ -250,18 +259,23 @@ def initialize_client(self, fetch_toggles: bool = True) -> None: self.unleash_instance_id, self.connection_id, self.unleash_metrics_interval, - headers, + base_headers, self.unleash_custom_options, self.strategy_mapping, self.unleash_request_timeout, ) if fetch_toggles: + fetch_headers = { + **base_headers, + "unleash-interval": self.unleash_refresh_interval_str_millis, + } + job_args = { "url": self.unleash_url, "app_name": self.unleash_app_name, "instance_id": self.unleash_instance_id, - "headers": headers, + "headers": fetch_headers, "custom_options": self.unleash_custom_options, "cache": self.cache, "engine": self.engine, diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index dca3c6c..3c90490 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1089,12 +1089,14 @@ def test_identification_values_are_passed_in(): ) responses.add(responses.POST, URL + METRICS_URL, json={}, status=202) - input_interval = 1 + refresh_interval = 1 + metrics_interval = 2 unleash_client = UnleashClient( - URL, APP_NAME, refresh_interval=input_interval, metrics_interval=1 + URL, APP_NAME, refresh_interval=refresh_interval, metrics_interval=metrics_interval ) expected_connection_id = unleash_client.connection_id - expected_interval = str(input_interval * 1000) + expected_refresh_interval = str(refresh_interval * 1000) + expected_metrics_interval = str(metrics_interval * 1000) unleash_client.initialize_client() register_request = responses.calls[0].request @@ -1108,14 +1110,14 @@ def test_identification_values_are_passed_in(): features_request = responses.calls[1].request assert features_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id - assert features_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval + assert features_request.headers["UNLEASH-INTERVAL"] == expected_refresh_interval - time.sleep(2) + time.sleep(3) metrics_request = [call for call in responses.calls if METRICS_URL in call.request.url][0].request metrics_body = json.loads(metrics_request.body) assert metrics_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id - assert metrics_request.headers["UNLEASH-INTERVAL-ID"] == expected_interval + assert metrics_request.headers["UNLEASH-INTERVAL"] == expected_metrics_interval assert metrics_body['connectionId'] == expected_connection_id From 4b7ec00345c8dbdf6cc5670f507a2ee3ec4b1e68 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 4 Mar 2025 07:30:47 +0000 Subject: [PATCH 14/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unit_tests/test_client.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index 3c90490..cacdd7f 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1092,7 +1092,10 @@ def test_identification_values_are_passed_in(): refresh_interval = 1 metrics_interval = 2 unleash_client = UnleashClient( - URL, APP_NAME, refresh_interval=refresh_interval, metrics_interval=metrics_interval + URL, + APP_NAME, + refresh_interval=refresh_interval, + metrics_interval=metrics_interval, ) expected_connection_id = unleash_client.connection_id expected_refresh_interval = str(refresh_interval * 1000) @@ -1102,8 +1105,8 @@ def test_identification_values_are_passed_in(): register_request = responses.calls[0].request register_body = json.loads(register_request.body) - assert register_request.headers[ "UNLEASH-CONNECTION-ID"] == expected_connection_id - assert register_body['connectionId'] == expected_connection_id + assert register_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id + assert register_body["connectionId"] == expected_connection_id unleash_client.is_enabled("registerMetricsFlag") @@ -1113,11 +1116,11 @@ def test_identification_values_are_passed_in(): assert features_request.headers["UNLEASH-INTERVAL"] == expected_refresh_interval time.sleep(3) - metrics_request = [call for call in responses.calls if METRICS_URL in call.request.url][0].request + metrics_request = [ + call for call in responses.calls if METRICS_URL in call.request.url + ][0].request metrics_body = json.loads(metrics_request.body) - assert metrics_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id assert metrics_request.headers["UNLEASH-INTERVAL"] == expected_metrics_interval - assert metrics_body['connectionId'] == expected_connection_id - + assert metrics_body["connectionId"] == expected_connection_id From d5338df70b370a17bdbabd815c7396d7db28fef7 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Tue, 4 Mar 2025 14:06:24 +0200 Subject: [PATCH 15/18] Fix --- UnleashClient/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/UnleashClient/__init__.py b/UnleashClient/__init__.py index 996d8c4..6b449e4 100644 --- a/UnleashClient/__init__.py +++ b/UnleashClient/__init__.py @@ -5,7 +5,6 @@ import warnings from dataclasses import asdict from datetime import datetime, timezone -from sys import base_prefix from typing import Any, Callable, Dict, Optional from apscheduler.executors.pool import ThreadPoolExecutor From b5e955a58480ac9d728790b2f01126bea08c7937 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Tue, 4 Mar 2025 15:38:23 +0200 Subject: [PATCH 16/18] Fix --- tests/unit_tests/test_client.py | 39 ++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index cacdd7f..067da1d 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1,5 +1,6 @@ import json import time +import uuid import warnings from datetime import datetime, timezone from pathlib import Path @@ -1097,6 +1098,8 @@ def test_identification_values_are_passed_in(): refresh_interval=refresh_interval, metrics_interval=metrics_interval, ) + + expected_connection_id = unleash_client.connection_id expected_refresh_interval = str(refresh_interval * 1000) expected_metrics_interval = str(metrics_interval * 1000) @@ -1105,22 +1108,48 @@ def test_identification_values_are_passed_in(): register_request = responses.calls[0].request register_body = json.loads(register_request.body) - assert register_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id - assert register_body["connectionId"] == expected_connection_id + assert "connectionId" in register_body, "Key missing: connectionId" + try: + uuid.UUID(register_body["connectionId"]) + except ValueError: + assert False, "Invalid UUID format in connectionId" + + assert "UNLEASH-CONNECTION-ID" in register_request.headers, "Header missing: UNLEASH-CONNECTION-ID" + try: + uuid.UUID(register_request.headers["UNLEASH-CONNECTION-ID"]) + except ValueError: + assert False, "Invalid UUID format in UNLEASH-CONNECTION-ID" + unleash_client.is_enabled("registerMetricsFlag") features_request = responses.calls[1].request - assert features_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id assert features_request.headers["UNLEASH-INTERVAL"] == expected_refresh_interval + assert "UNLEASH-CONNECTION-ID" in features_request.headers, "Header missing: UNLEASH-CONNECTION-ID" + + try: + uuid.UUID(features_request.headers["UNLEASH-CONNECTION-ID"]) + except ValueError: + assert False, "Invalid UUID format in UNLEASH-CONNECTION-ID" + time.sleep(3) metrics_request = [ call for call in responses.calls if METRICS_URL in call.request.url ][0].request metrics_body = json.loads(metrics_request.body) - assert metrics_request.headers["UNLEASH-CONNECTION-ID"] == expected_connection_id assert metrics_request.headers["UNLEASH-INTERVAL"] == expected_metrics_interval - assert metrics_body["connectionId"] == expected_connection_id + + assert "connectionId" in metrics_body, "Key missing: connectionId" + try: + uuid.UUID(metrics_body["connectionId"]) + except ValueError: + assert False, "Invalid UUID format in connectionId" + + assert "UNLEASH-CONNECTION-ID" in metrics_request.headers, "Header missing: UNLEASH-CONNECTION-ID" + try: + uuid.UUID(metrics_request.headers["UNLEASH-CONNECTION-ID"]) + except ValueError: + assert False, "Invalid UUID format in UNLEASH-CONNECTION-ID" From beb8b6a18250db13bfe38c4c63d2339f2e037aff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 4 Mar 2025 13:39:04 +0000 Subject: [PATCH 17/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unit_tests/test_client.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index 067da1d..40dae68 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1099,7 +1099,6 @@ def test_identification_values_are_passed_in(): metrics_interval=metrics_interval, ) - expected_connection_id = unleash_client.connection_id expected_refresh_interval = str(refresh_interval * 1000) expected_metrics_interval = str(metrics_interval * 1000) @@ -1114,20 +1113,23 @@ def test_identification_values_are_passed_in(): except ValueError: assert False, "Invalid UUID format in connectionId" - assert "UNLEASH-CONNECTION-ID" in register_request.headers, "Header missing: UNLEASH-CONNECTION-ID" + assert ( + "UNLEASH-CONNECTION-ID" in register_request.headers + ), "Header missing: UNLEASH-CONNECTION-ID" try: uuid.UUID(register_request.headers["UNLEASH-CONNECTION-ID"]) except ValueError: assert False, "Invalid UUID format in UNLEASH-CONNECTION-ID" - unleash_client.is_enabled("registerMetricsFlag") features_request = responses.calls[1].request assert features_request.headers["UNLEASH-INTERVAL"] == expected_refresh_interval - assert "UNLEASH-CONNECTION-ID" in features_request.headers, "Header missing: UNLEASH-CONNECTION-ID" + assert ( + "UNLEASH-CONNECTION-ID" in features_request.headers + ), "Header missing: UNLEASH-CONNECTION-ID" try: uuid.UUID(features_request.headers["UNLEASH-CONNECTION-ID"]) @@ -1148,7 +1150,9 @@ def test_identification_values_are_passed_in(): except ValueError: assert False, "Invalid UUID format in connectionId" - assert "UNLEASH-CONNECTION-ID" in metrics_request.headers, "Header missing: UNLEASH-CONNECTION-ID" + assert ( + "UNLEASH-CONNECTION-ID" in metrics_request.headers + ), "Header missing: UNLEASH-CONNECTION-ID" try: uuid.UUID(metrics_request.headers["UNLEASH-CONNECTION-ID"]) except ValueError: From e31d1ce2e8175013ba5f2c6b1a50ce05e8bdf169 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Tue, 4 Mar 2025 15:52:24 +0200 Subject: [PATCH 18/18] Fix --- tests/unit_tests/test_client.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/unit_tests/test_client.py b/tests/unit_tests/test_client.py index 067da1d..0a72c12 100644 --- a/tests/unit_tests/test_client.py +++ b/tests/unit_tests/test_client.py @@ -1099,8 +1099,6 @@ def test_identification_values_are_passed_in(): metrics_interval=metrics_interval, ) - - expected_connection_id = unleash_client.connection_id expected_refresh_interval = str(refresh_interval * 1000) expected_metrics_interval = str(metrics_interval * 1000)