Skip to content

Commit d323800

Browse files
authored
FFM-8544 Reorganise streaming logs (#77)
* FFM-8300 Log status codes correctly * FFM-8544 Only log error in SSE if a reconnect has breached threshold * FFM-8544 Change logging * FFM-8544 Make streaming, not sse client, responsible for network warnings * FFM-8544 1.2.1 release prep
1 parent 751c12c commit d323800

File tree

9 files changed

+28
-20
lines changed

9 files changed

+28
-20
lines changed

featureflags/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
__author__ = """Enver Bisevac"""
44
__email__ = "enver.bisevac@harness.io"
5-
__version__ = '1.2.0'
5+
__version__ = '1.2.1'

featureflags/analytics.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ def _send_data(self) -> None:
206206
unique_target)
207207
target_data_batch_index += 1
208208

209-
210209
finally:
211210
self._data = {}
212211
self._target_data_batches = [{}]
@@ -256,18 +255,17 @@ def _send_data(self) -> None:
256255

257256
# Log any error codes
258257
for unique_code, count in unique_responses_codes.items():
259-
if response.status_code >= 400:
258+
if unique_code >= 400:
260259
warn_post_metrics_target_batch_failed(
261260
f'{count} batches received code {unique_code}')
261+
continue
262262
info_metrics_target_batch_success(
263263
f'{count} batches successful')
264264

265-
266265
info_metrics_success()
267266
except httpx.RequestError as ex:
268267
warn_post_metrics_failed(ex)
269268

270-
271269
def process_target_data_batch(self, target_data_batch):
272270
batch_request_body: Metrics = Metrics(
273271
target_data=target_data_batch, metrics_data=[]

featureflags/polling.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ def stop(self):
9191
self.__running = False
9292
info_polling_stopped("Client was closed")
9393

94-
9594
def retrieve_flags_and_segments(self):
9695
t1 = Thread(target=self.__retrieve_segments)
9796
t2 = Thread(target=self.__retrieve_flags)

featureflags/sdk_logging_codes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def get_sdk_code_message(key):
2727
5002: "SSE event received: ",
2828
5003: "SSE retrying to connect in",
2929
5004: "SSE stopped",
30+
5005: "Stream is still retrying after 5 retries: ",
3031
# SDK_EVAL_6xxx - these are hardcoded in `variation.py` as they
3132
# are more complex
3233
# SDK_METRICS_7xxx
@@ -145,6 +146,10 @@ def warn_stream_retrying(seconds):
145146
log.warning(sdk_err_msg(5003, seconds))
146147

147148

149+
def warn_stream_retrying_long_duration(seconds):
150+
log.warning(sdk_err_msg(5005, f'{seconds} seconds'))
151+
152+
148153
def warn_post_metrics_failed(reason):
149154
log.warning(sdk_err_msg(7002, reason))
150155

featureflags/sse_client.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,13 @@ def __next__(self):
108108
raise EOFError()
109109
self.buf += self.decoder.decode(next_chunk)
110110

111-
except (StopIteration, requests.RequestException, EOFError) as e:
112-
if isinstance(e, StopIteration):
113-
log.error("Error when iterating through stream messages, "
114-
"attempting to resume")
111+
except (StopIteration, EOFError) as e:
115112

116113
if isinstance(e, EOFError):
117-
log.error("Received unexpected EOF from stream, "
118-
"attempting to reconnect")
114+
log.debug("Recieved empty chunk of data from stream, "
115+
"reconnecting")
119116

120-
if isinstance(e, requests.RequestException):
121-
log.error("Error encountered in stream, "
122-
"attempting to reconnect: %s", e)
117+
log.debug("Streaming stopped abruptly, reconnecting")
123118

124119
time.sleep(self.retry / 1000.0)
125120
self._connect()

featureflags/streaming.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from .models.message import Message
1414
from .sdk_logging_codes import info_stream_connected, \
1515
info_stream_event_received, warn_stream_disconnected, \
16-
warn_stream_retrying, info_stream_stopped
16+
warn_stream_retrying, info_stream_stopped, \
17+
warn_stream_retrying_long_duration
1718
from .sse_client import SSEClient
1819
from .util import log
1920

@@ -40,6 +41,7 @@ def __init__(self, repository: DataProviderInterface,
4041
self._token = token
4142
self._stream_url = f'{config.base_url}/stream?cluster={cluster}'
4243
self._repository = repository
44+
self.reconnect_timer = 0
4345

4446
def run(self):
4547
log.info("Starting StreamingProcessor connecting to uri: " +
@@ -69,18 +71,26 @@ def run(self):
6971
if self.poller.is_set() is False:
7072
self.poller.set()
7173

74+
# Check if retry attempts have reached a threshold before
75+
# we log an error to the user
76+
retry_error_log_threshold = 4
77+
if retries >= retry_error_log_threshold:
78+
warn_stream_retrying_long_duration(
79+
retry_error_log_threshold)
80+
7281
# Calculate back of sleep
7382
sleep = (BACK_OFF_IN_SECONDS * 2 ** retries +
7483
random.uniform(0, 1))
84+
7585
warn_stream_retrying(f'{sleep.__str__()}s')
7686
time.sleep(sleep)
7787
retries += 1
7888

7989
def _connect(self) -> SSEClient:
80-
return SSEClient(self._stream_url, headers={
90+
return SSEClient(url=self._stream_url, headers={
8191
'Authorization': f'Bearer {self._token}',
8292
'API-Key': self._api_key
83-
})
93+
}, retry=BACK_OFF_IN_SECONDS)
8494

8595
def process_message(self, msg: Message) -> None:
8696
processor: Union[FlagMsgProcessor, SegmentMsgProcessor, None] = None

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 1.2.0
2+
current_version = 1.2.1
33
commit = True
44
tag = True
55

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
test_suite="tests",
5858
tests_require=test_requirements,
5959
url="https://github.com/harness/ff-python-server-sdk",
60-
version='1.2.0',
60+
version='1.2.1',
6161
zip_safe=False,
6262
)

tests/unit/test_sdk_logging_codes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_logs_dont_raise_exception():
2525
sdk_codes.warn_auth_retying(1, "some error")
2626
sdk_codes.warn_stream_disconnected("example reason")
2727
sdk_codes.warn_stream_retrying(5)
28+
sdk_codes.warn_stream_retrying_long_duration(5)
2829
sdk_codes.warn_post_metrics_failed("example reason")
2930
sdk_codes.warn_post_metrics_target_batch_failed("example reason")
3031
sdk_codes.warn_default_variation_served("identifier", target, "default")

0 commit comments

Comments
 (0)