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

Support memory profiling metrics #4239

Merged
merged 2 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 5 additions & 1 deletion datadog_checks_base/datadog_checks/base/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,13 @@ def run(self):
from ..utils.agent.memory import TRACE_LOCK, profile_memory

with TRACE_LOCK:
profile_memory(
metrics = profile_memory(
self.check, self.init_config, namespaces=self.check_id.split(':', 1), args=(instance,)
)

tags = ['check_name:{}'.format(self.name), 'check_version:{}'.format(self.check_version)]
for m in metrics:
self.gauge(m.name, m.value, tags=tags)
else:
self.check(instance)

Expand Down
25 changes: 20 additions & 5 deletions datadog_checks_base/datadog_checks/base/utils/agent/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

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 least datadog.agent.profile so we can reuse when/if the cpu profiling metrics come along?

self.value = float(value)


def get_sign(n):
return '-' if n < 0 else '+'

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe check_run_alloc? I feel like total might mislead to think it's the entire memory being used by the check, where that wouldn't be entirely accurate.


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)

Expand Down Expand Up @@ -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')
Expand All @@ -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