Provide some process resource usage metrics

This commit is contained in:
Paul "LeoNerd" Evans 2015-03-05 16:15:21 +00:00
parent fa319a5786
commit 399689dcc7

View File

@ -13,7 +13,11 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Because otherwise 'resource' collides with synapse.metrics.resource
from __future__ import absolute_import
import logging import logging
from resource import getrusage, getpagesize, RUSAGE_SELF
from .metric import CounterMetric, CallbackMetric, TimerMetric, CacheMetric from .metric import CounterMetric, CallbackMetric, TimerMetric, CacheMetric
@ -76,6 +80,9 @@ def get_metrics_for(name):
def render_all(): def render_all():
strs = [] strs = []
# TODO(paul): Internal hack
update_resource_metrics()
for name in sorted(all_metrics.keys()): for name in sorted(all_metrics.keys()):
try: try:
strs += all_metrics[name].render() strs += all_metrics[name].render()
@ -84,3 +91,23 @@ def render_all():
logger.exception("Failed to render %s metric", name) logger.exception("Failed to render %s metric", name)
return "\n".join(strs) return "\n".join(strs)
# Now register some standard process-wide state metrics, to give indications of
# process resource usage
rusage = None
PAGE_SIZE = getpagesize()
def update_resource_metrics():
global rusage
rusage = getrusage(RUSAGE_SELF)
resource_metrics = get_metrics_for("process.resource")
# msecs
resource_metrics.register_callback("utime", lambda: rusage.ru_utime * 1000)
resource_metrics.register_callback("stime", lambda: rusage.ru_stime * 1000)
# pages
resource_metrics.register_callback("maxrss", lambda: rusage.ru_maxrss * PAGE_SIZE)