Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions UnleashClient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from UnleashClient.periodic_tasks import fetch_and_load_features, aggregate_and_send_metrics
from UnleashClient.strategies import ApplicationHostname, Default, GradualRolloutRandom, \
GradualRolloutSessionId, GradualRolloutUserId, UserWithId, RemoteAddress
from UnleashClient.constants import METRIC_LAST_SENT_TIME
from .utils import LOGGER


Expand Down Expand Up @@ -51,7 +52,8 @@ def __init__(self,
self.scheduler = BackgroundScheduler()
self.fl_job: Job = None
self.metric_job: Job = None
self.metrics_last_sent_time = datetime.now()
self.cache[METRIC_LAST_SENT_TIME] = datetime.now()
self.cache.sync()

# Mappings
default_strategy_mapping = {
Expand Down Expand Up @@ -97,7 +99,7 @@ def initialize_client(self) -> None:
"instance_id": self.unleash_instance_id,
"custom_headers": self.unleash_custom_headers,
"features": self.features,
"last_sent": self.metrics_last_sent_time
"ondisk_cache": self.cache
}

# Register app
Expand Down
3 changes: 2 additions & 1 deletion UnleashClient/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Library
# Library
SDK_NAME = "unleash-client-python"
SDK_VERSION = "2.0.0"
REQUEST_TIMEOUT = 30
METRIC_LAST_SENT_TIME = "mlst"

# =Unleash=
APPLICATION_HEADERS = {"Content-Type": "application/json"}
Expand Down
9 changes: 6 additions & 3 deletions UnleashClient/periodic_tasks/send_metrics.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from collections import ChainMap
from datetime import datetime
import fcache
from UnleashClient.api import send_metrics
from UnleashClient.constants import METRIC_LAST_SENT_TIME


def aggregate_and_send_metrics(url: str,
app_name: str,
instance_id: str,
custom_headers: dict,
features: dict,
last_sent: datetime
ondisk_cache: fcache.cache
) -> None:
feature_stats_list = []

Expand All @@ -27,11 +29,12 @@ def aggregate_and_send_metrics(url: str,
"appName": app_name,
"instanceId": instance_id,
"bucket": {
"start": last_sent.isoformat(),
"start": ondisk_cache[METRIC_LAST_SENT_TIME].isoformat(),
"stop": datetime.now().isoformat(),
"toggles": dict(ChainMap(*feature_stats_list))
}
}

send_metrics(url, metrics_request, custom_headers)
last_sent = datetime.now()
ondisk_cache[METRIC_LAST_SENT_TIME] = datetime.now()
ondisk_cache.sync()
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Next

**Bugfixes**
* (Major) Fix issue where `bucket.start` value sent to Unleash was never updated.

## v2.0.0

**Bugfixes**
Expand Down
28 changes: 28 additions & 0 deletions tests/integration_tests/integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import time
from UnleashClient import UnleashClient

# ---
import logging
import sys

root = logging.getLogger()
root.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
# ---


my_client = UnleashClient(
url="http://localhost:4242/api",
app_name="pyIvan"
)

my_client.initialize_client()

while True:
time.sleep(10)
print(my_client.is_enabled("Demo"))
10 changes: 7 additions & 3 deletions tests/unit_tests/periodic/test_aggregate_and_send_metrics.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import datetime
import responses
from fcache.cache import FileCache
from tests.utilities.testing_constants import URL, APP_NAME, INSTANCE_ID, CUSTOM_HEADERS, IP_LIST
from UnleashClient.constants import METRICS_URL
from UnleashClient.constants import METRICS_URL, METRIC_LAST_SENT_TIME
from UnleashClient.periodic_tasks import aggregate_and_send_metrics
from UnleashClient.features import Feature
from UnleashClient.strategies import RemoteAddress, Default
Expand All @@ -16,7 +17,9 @@
def test_aggregate_and_send_metrics():
responses.add(responses.POST, FULL_METRICS_URL, json={}, status=200)

last_run_datetime = datetime.datetime.now() - datetime.timedelta(seconds=30)
start_time = datetime.datetime.now() - datetime.timedelta(seconds=60)
cache = FileCache("TestCache")
cache[METRIC_LAST_SENT_TIME] = start_time
strategies = [RemoteAddress(parameters={"IPs": IP_LIST}), Default()]
my_feature1 = Feature("My Feature1", True, strategies)
my_feature1.yes_count = 1
Expand All @@ -28,11 +31,12 @@ def test_aggregate_and_send_metrics():

features = {"My Feature1": my_feature1, "My Feature 2": my_feature2}

aggregate_and_send_metrics(URL, APP_NAME, INSTANCE_ID, CUSTOM_HEADERS, features, last_run_datetime)
aggregate_and_send_metrics(URL, APP_NAME, INSTANCE_ID, CUSTOM_HEADERS, features, cache)

assert len(responses.calls) == 1
request = json.loads(responses.calls[0].request.body)

assert len(request['bucket']["toggles"].keys()) == 2
assert request['bucket']["toggles"]["My Feature1"]["yes"] == 1
assert request['bucket']["toggles"]["My Feature1"]["no"] == 1
assert cache[METRIC_LAST_SENT_TIME] > start_time