-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Support memory profiling metrics #4239
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -33,6 +33,14 @@ | |
TRACE_LOCK = threading.Lock() | ||
|
||
|
||
class MemoryProfileMetric(object): | ||
__slots__ = ('name', 'value') | ||
|
||
def __init__(self, name, value): | ||
self.name = 'datadog.agent.profile.memory.{}'.format(name) | ||
self.value = float(value) | ||
|
||
|
||
def get_sign(n): | ||
return '-' if n < 0 else '+' | ||
|
||
|
@@ -91,7 +99,7 @@ def get_unit_formatter(unit): | |
return lambda n: format_units(unit, *convert_units(n, to=unit)) | ||
|
||
|
||
def write_pretty_top(path, snapshot, unit_formatter, key_type, limit, cumulative): | ||
def gather_top(metrics, path, snapshot, unit_formatter, key_type, limit, cumulative): | ||
top_stats = snapshot.statistics(key_type, cumulative=cumulative) | ||
|
||
with open(path, 'w', encoding='utf-8') as f: | ||
|
@@ -121,9 +129,11 @@ def write_pretty_top(path, snapshot, unit_formatter, key_type, limit, cumulative | |
amount, unit = unit_formatter(total) | ||
f.write('Total allocated size: {} {}\n'.format(amount, unit)) | ||
|
||
metrics.append(MemoryProfileMetric('check_run_total', total)) | ||
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. maybe |
||
|
||
def write_pretty_diff( | ||
path, current_snapshot, previous_snapshot, unit_formatter, key_type, limit, cumulative, diff_order | ||
|
||
def gather_diff( | ||
metrics, path, current_snapshot, previous_snapshot, unit_formatter, key_type, limit, cumulative, diff_order | ||
): | ||
top_stats = current_snapshot.compare_to(previous_snapshot, key_type=key_type, cumulative=cumulative) | ||
|
||
|
@@ -250,13 +260,16 @@ def profile_memory(f, config, namespaces=None, args=(), kwargs=None): | |
unit = config.get('profile_memory_unit', DEFAULT_UNIT) | ||
unit_formatter = get_unit_formatter(unit) | ||
|
||
# Metrics to send | ||
metrics = [] | ||
|
||
# First, write the prettified snapshot | ||
snapshot_dir = os.path.join(location, 'snapshots') | ||
if not os.path.isdir(snapshot_dir): | ||
os.makedirs(snapshot_dir) | ||
|
||
new_snapshot = os.path.join(snapshot_dir, get_timestamp_filename('snapshot')) | ||
write_pretty_top(new_snapshot, snapshot, unit_formatter, sort_by, limit, combine) | ||
gather_top(metrics, new_snapshot, snapshot, unit_formatter, sort_by, limit, combine) | ||
|
||
# Then, compute the diff if there was a previous run | ||
previous_snapshot_dump = os.path.join(location, 'last-snapshot') | ||
|
@@ -269,7 +282,9 @@ def profile_memory(f, config, namespaces=None, args=(), kwargs=None): | |
|
||
# and write it | ||
new_diff = os.path.join(diff_dir, get_timestamp_filename('diff')) | ||
write_pretty_diff(new_diff, snapshot, previous_snapshot, unit_formatter, sort_by, limit, combine, diff_order) | ||
gather_diff(metrics, new_diff, snapshot, previous_snapshot, unit_formatter, sort_by, limit, combine, diff_order) | ||
|
||
# Finally, dump the current snapshot for doing a diff on the next run | ||
snapshot.dump(previous_snapshot_dump) | ||
|
||
return metrics |
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.
perhaps we could put the
datadog.agent.profile.memory
namespace in a global, or at leastdatadog.agent.profile
so we can reuse when/if thecpu
profiling metrics come along?