Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error counters #98

Merged
merged 2 commits into from
Mar 25, 2020
Merged
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
21 changes: 20 additions & 1 deletion signalfx/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(self, token, endpoint=DEFAULT_INGEST_ENDPOINT,
self._queue = queue.Queue()
self._thread_running = False
self._lock = threading.Lock()
self._error_counters = collections.defaultdict(lambda: 0)

user_agent = ['{0}/{1}'.format(version.name, version.version)]
if type(user_agents) == list:
Expand Down Expand Up @@ -206,6 +207,23 @@ def stop(self, msg='Thread stopped'):
self._send_thread.join()
_logger.debug(msg)

def _inc_error(self, error_type):
"""Increment internal counter of errors encountered.

Args:
error_type (string): the exception class name or other error
descriptor.
"""
with self._lock:
self._error_counters[error_type] += 1

def reset_error_counters(self):
"""Reset dict of error counters to 0 and return the previous values."""
with self._lock:
previous = self._error_counters
self._error_counters = collections.defaultdict(lambda: 0)
return previous

def _send(self):
try:
while self._thread_running or not self._queue.empty():
Expand All @@ -223,7 +241,8 @@ def _send(self):
'{0}/{1}'.format(
self._endpoint,
self._INGEST_ENDPOINT_DATAPOINT_SUFFIX))
except:
except Exception as err:
self._inc_error(err.__class__.__name__)
_logger.exception('Posting data to SignalFx failed.')
except KeyboardInterrupt:
self.stop(msg='Thread stopped by keyboard interrupt.')
Expand Down