Skip to content

Commit 4dba56a

Browse files
authored
Add Memory Utilization Metrics and Tests. (#141)
* Add memory utilization percentage metric. #3 * Add tests for memory usage metrics. #3 * Remove use of count dictionary. #3
1 parent 7af1799 commit 4dba56a

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

newrelic/samplers/memory_usage.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
memory usage.
1717
1818
"""
19+
import os
1920

20-
from newrelic.common.system_info import physical_memory_used
21+
from newrelic.common.system_info import physical_memory_used, total_physical_memory
2122

2223
from newrelic.samplers.decorators import data_source_generator
2324

25+
PID = os.getpid()
26+
2427

2528
@data_source_generator(name='Memory Usage')
2629
def memory_usage_data_source():
27-
yield ('Memory/Physical', physical_memory_used())
30+
yield ('Memory/Physical/%d' % (PID), physical_memory_used())
31+
yield ('Memory/Physical/Utilization/%d' % (PID), physical_memory_used()/total_physical_memory())
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
import os
17+
18+
from newrelic.samplers.memory_usage import memory_usage_data_source
19+
20+
@pytest.fixture
21+
def data_source():
22+
sampler = memory_usage_data_source(settings=())["factory"](environ=())
23+
yield sampler
24+
25+
PID = os.getpid()
26+
27+
EXPECTED_METRICS = (
28+
"Memory/Physical/%d" % PID,
29+
"Memory/Physical/Utilization/%d" % PID,
30+
)
31+
32+
33+
def test_gc_metrics_collection(data_source):
34+
metrics_table = set(m[0] for m in (data_source() or ()))
35+
36+
for metric in EXPECTED_METRICS:
37+
assert metric in metrics_table

0 commit comments

Comments
 (0)