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 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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# (C) Datadog, Inc. 2019
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
METRIC_PROFILE_NAMESPACE = 'datadog.agent.profile'
27 changes: 22 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 @@ -9,6 +9,8 @@

from binary import BinaryUnits, convert_units

from .common import METRIC_PROFILE_NAMESPACE

try:
import tracemalloc
except ImportError:
Expand All @@ -33,6 +35,14 @@
TRACE_LOCK = threading.Lock()


class MemoryProfileMetric(object):
__slots__ = ('name', 'value')

def __init__(self, name, value):
self.name = '{}.memory.{}'.format(METRIC_PROFILE_NAMESPACE, name)
self.value = float(value)


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

Expand Down Expand Up @@ -91,7 +101,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 +131,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_alloc', total))

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 +262,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 +284,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