-
Notifications
You must be signed in to change notification settings - Fork 45
Fix issue with stats d method being called #140
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a704af
Fix issue with stats d method being called
DarcyRaynerDD 7063cc8
Update integration tests
DarcyRaynerDD 5cdae4b
Feedback from PR review
DarcyRaynerDD fd161c7
Fix PR issues
DarcyRaynerDD 41341df
Reformat metrics file
DarcyRaynerDD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,20 @@ | |
logger = logging.getLogger(__name__) | ||
|
||
|
||
class StatsDWrapper: | ||
class StatsWriter: | ||
def distribution(self, metric_name, value, tags=[], timestamp=None): | ||
pass | ||
|
||
def flush(self): | ||
pass | ||
|
||
def stop(self): | ||
pass | ||
|
||
|
||
class StatsDWriter(StatsWriter): | ||
""" | ||
Wraps StatsD calls, to give an identical interface to ThreadStats | ||
Wraps StatsD calls, creates a common | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfinished sentence? |
||
""" | ||
|
||
def __init__(self): | ||
|
@@ -33,21 +44,76 @@ def __init__(self): | |
def distribution(self, metric_name, value, tags=[], timestamp=None): | ||
statsd.distribution(metric_name, value, tags=tags) | ||
|
||
def flush(self, value): | ||
pass | ||
|
||
class ThreadStatsWriter(StatsWriter): | ||
""" | ||
Writes distribution metrics using thre ThreadStats class | ||
""" | ||
|
||
def __init__(self, flush_in_thread): | ||
self.thread_stats = ThreadStats(compress_payload=True) | ||
self.thread_stats.start(flush_in_thread=flush_in_thread) | ||
|
||
def distribution(self, metric_name, value, tags=[], timestamp=None): | ||
self.thread_stats.distribution( | ||
metric_name, value, tags=tags, timestamp=timestamp | ||
) | ||
|
||
def flush(self): | ||
""" "Flush distributions from ThreadStats to Datadog. | ||
Modified based on `datadog.threadstats.base.ThreadStats.flush()`, | ||
to gain better control over exception handling. | ||
""" | ||
_, dists = self.thread_stats._get_aggregate_metrics_and_dists(float("inf")) | ||
count_dists = len(dists) | ||
if not count_dists: | ||
logger.debug("No distributions to flush. Continuing.") | ||
|
||
self.thread_stats.flush_count += 1 | ||
logger.debug( | ||
"Flush #%s sending %s distributions", | ||
self.thread_stats.flush_count, | ||
count_dists, | ||
) | ||
try: | ||
self.thread_stats.reporter.flush_distributions(dists) | ||
except Exception as e: | ||
# The nature of the root issue https://bugs.python.org/issue41345 is complex, | ||
# but comprehensive tests suggest that it is safe to retry on this specific error. | ||
if isinstance( | ||
e, api.exceptions.ClientError | ||
) and "RemoteDisconnected" in str(e): | ||
logger.debug( | ||
"Retry flush #%s due to RemoteDisconnected", | ||
self.thread_stats.flush_count, | ||
) | ||
try: | ||
self.thread_stats.reporter.flush_distributions(dists) | ||
except Exception: | ||
logger.debug( | ||
"Flush #%s failed after retry", | ||
self.thread_stats.flush_count, | ||
exc_info=True, | ||
) | ||
else: | ||
logger.debug( | ||
"Flush #%s failed", self.thread_stats.flush_count, exc_info=True | ||
) | ||
|
||
def stop(self): | ||
self.thread_stats.stop() | ||
|
||
|
||
lambda_stats = None | ||
if should_use_extension: | ||
lambda_stats = StatsDWrapper() | ||
lambda_stats = StatsDWriter() | ||
else: | ||
# Periodical flushing in a background thread is NOT guaranteed to succeed | ||
# and leads to data loss. When disabled, metrics are only flushed at the | ||
# end of invocation. To make metrics submitted from a long-running Lambda | ||
# function available sooner, consider using the Datadog Lambda extension. | ||
flush_in_thread = os.environ.get("DD_FLUSH_IN_THREAD", "").lower() == "true" | ||
lambda_stats = ThreadStats(compress_payload=True) | ||
lambda_stats.start(flush_in_thread=flush_in_thread) | ||
lambda_stats = ThreadStatsWriter(flush_in_thread) | ||
|
||
|
||
def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=False): | ||
|
@@ -74,8 +140,7 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=Fal | |
|
||
|
||
def write_metric_point_to_stdout(metric_name, value, timestamp=None, tags=[]): | ||
"""Writes the specified metric point to standard output | ||
""" | ||
"""Writes the specified metric point to standard output""" | ||
logger.debug( | ||
"Sending metric %s value %s to Datadog via log forwarder", metric_name, value | ||
) | ||
|
@@ -91,40 +156,8 @@ def write_metric_point_to_stdout(metric_name, value, timestamp=None, tags=[]): | |
) | ||
|
||
|
||
def flush_thread_stats(): | ||
""""Flush distributions from ThreadStats to Datadog. | ||
|
||
Modified based on `datadog.threadstats.base.ThreadStats.flush()`, | ||
to gain better control over exception handling. | ||
""" | ||
_, dists = lambda_stats._get_aggregate_metrics_and_dists(float("inf")) | ||
count_dists = len(dists) | ||
if not count_dists: | ||
logger.debug("No distributions to flush. Continuing.") | ||
|
||
lambda_stats.flush_count += 1 | ||
logger.debug( | ||
"Flush #%s sending %s distributions", lambda_stats.flush_count, count_dists | ||
) | ||
try: | ||
lambda_stats.reporter.flush_distributions(dists) | ||
except Exception as e: | ||
# The nature of the root issue https://bugs.python.org/issue41345 is complex, | ||
# but comprehensive tests suggest that it is safe to retry on this specific error. | ||
if isinstance(e, api.exceptions.ClientError) and "RemoteDisconnected" in str(e): | ||
logger.debug( | ||
"Retry flush #%s due to RemoteDisconnected", lambda_stats.flush_count | ||
) | ||
try: | ||
lambda_stats.reporter.flush_distributions(dists) | ||
except Exception: | ||
logger.debug( | ||
"Flush #%s failed after retry", | ||
lambda_stats.flush_count, | ||
exc_info=True, | ||
) | ||
else: | ||
logger.debug("Flush #%s failed", lambda_stats.flush_count, exc_info=True) | ||
def flush_stats(): | ||
lambda_stats.flush() | ||
|
||
|
||
def are_enhanced_metrics_enabled(): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we
raise NotImplementedError
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or use https://docs.python.org/3/library/abc.html?