Skip to content

Commit

Permalink
Merge pull request #64 from boarik/feature/fix_pep8
Browse files Browse the repository at this point in the history
PEP8: Reformat files according to PEP8 standards.
  • Loading branch information
omergertel authored Dec 19, 2018
2 parents a691743 + 1cab48b commit b71056e
Show file tree
Hide file tree
Showing 34 changed files with 653 additions and 400 deletions.
20 changes: 9 additions & 11 deletions example_pymetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from pyformance import global_registry



class Collector(object):
# TODO: use meters and histograms instead of gauges if possible
Expand All @@ -23,7 +22,7 @@ def __init__(self, registry=None):
registry = global_registry()
self.registry = registry
self._memory_usage = 0

def collect_memory(self):
if resource:
usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
Expand All @@ -34,22 +33,22 @@ def collect_memory(self):
self._memory_usage = usage
self.registry.gauge("python.memory.usage").set_value(usage)
self.registry.gauge("python.memory.increase").set_value(increase)

def collect_threads(self):
counter = 0
alive = 0
daemon = 0
for thread in threading.enumerate():
counter +=1
counter += 1
if thread.isDaemon():
daemon += 1
if thread.isAlive():
alive += 1
#switch_interval = sys.getcheckinterval()
# switch_interval = sys.getcheckinterval()
self.registry.gauge("python.thread.count").set_value(counter)
self.registry.gauge("python.thread.daemon").set_value(daemon)
self.registry.gauge("python.thread.alive").set_value(alive)

def collect_garbage(self):
(count0, count1, count2) = gc.get_count()
(threshold0, threshold1, threshold2) = gc.get_threshold()
Expand All @@ -62,7 +61,7 @@ def collect_garbage(self):
self.registry.gauge("python.gc.objects.count").set_value(object_count)
self.registry.gauge("python.gc.referrers.count").set_value(referrers_count)
self.registry.gauge("python.gc.referents.count").set_value(referents_count)

def collect_processes(self):
counter = 0
alive = 0
Expand All @@ -76,18 +75,17 @@ def collect_processes(self):
self.registry.gauge("python.processes.count").set_value(counter)
self.registry.gauge("python.processes.alive").set_value(alive)
self.registry.gauge("python.processes.daemon").set_value(daemon)



def collect(self):
self.collect_memory()
self.collect_garbage()
self.collect_threads()
self.collect_processes()




if __name__ == "__main__":
from pyformance.reporters import ConsoleReporter

reporter = ConsoleReporter()
col = Collector()
col.collect()
Expand Down
32 changes: 20 additions & 12 deletions example_sysmetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ def collect_disk_io(self, whitelist=[]):
if not whitelist or entry in whitelist:
for k, v in six.iteritems(stat._asdict()):
self.registry.gauge("disk-%s.%s" % (entry, k)).set_value(v)

def collect_network_io(self, whitelist=[]):
stats = psutil.net_io_counters(pernic=True)
for entry, stat in six.iteritems(stats):
if not whitelist or entry in whitelist:
for k, v in six.iteritems(stat._asdict()):
self.registry.gauge("nic-%s.%s" % (entry.replace(" ", "_"), k)).set_value(v)
self.registry.gauge(
"nic-%s.%s" % (entry.replace(" ", "_"), k)
).set_value(v)

def collect_cpu_times(self, whitelist=[]):
stats = psutil.cpu_times(percpu=True)
Expand All @@ -44,23 +46,28 @@ def collect_swap_usage(self):
stats = psutil.swap_memory()
for k, v in six.iteritems(stats._asdict()):
self.registry.gauge("swap.%s" % k).set_value(v)

def collect_virtmem_usage(self):
stats = psutil.virtual_memory()
for k, v in six.iteritems(stats._asdict()):
self.registry.gauge("virtmem.%s" % k).set_value(v)
self.registry.gauge("virtmem.%s" % k).set_value(v)

def collect_uptime(self):
uptime = int(time.time()) - int(psutil.boot_time())
self.registry.gauge("uptime").set_value(uptime)

def collect_disk_usage(self, whitelist=[]):
for partition in psutil.disk_partitions():
if not whitelist or partition.mountpoint in whitelist or partition.device in whitelist:
if (
not whitelist
or partition.mountpoint in whitelist
or partition.device in whitelist
):
usage = psutil.disk_usage(partition.mountpoint)
if platform.system() == "Windows":
disk_name = "-" + \
partition.mountpoint.replace("\\", "").replace(":", "")
disk_name = "-" + partition.mountpoint.replace("\\", "").replace(
":", ""
)
else:
disk_name = partition.mountpoint.replace("/", "-")
if disk_name == "-":
Expand All @@ -71,9 +78,9 @@ def collect_disk_usage(self, whitelist=[]):

def collect_loadavgs(self):
loadavgs = os.getloadavg()
self.registry.gauge('loadavg_1min').set_value(loadavgs[0])
self.registry.gauge('loadavg_5min').set_value(loadavgs[1])
self.registry.gauge('loadavg_15min').set_value(loadavgs[2])
self.registry.gauge("loadavg_1min").set_value(loadavgs[0])
self.registry.gauge("loadavg_5min").set_value(loadavgs[1])
self.registry.gauge("loadavg_15min").set_value(loadavgs[2])

def collect(self):
self.collect_disk_io()
Expand All @@ -85,9 +92,10 @@ def collect(self):
self.collect_disk_usage()
self.collect_loadavgs()


if __name__ == "__main__":
from pyformance.reporters import ConsoleReporter

reporter = ConsoleReporter()
col = Collector()
col.collect()
Expand Down
11 changes: 9 additions & 2 deletions pyformance/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
__import__('pkg_resources').declare_namespace(__name__)
__import__("pkg_resources").declare_namespace(__name__)

from .registry import MetricsRegistry, global_registry, set_global_registry
from .registry import timer, counter, meter, histogram, gauge
from .registry import dump_metrics, clear, count_calls, meter_calls, hist_calls, time_calls
from .registry import (
dump_metrics,
clear,
count_calls,
meter_calls,
hist_calls,
time_calls,
)
from .meters.timer import call_too_long
5 changes: 2 additions & 3 deletions pyformance/meters/histogram.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
import math
from threading import Lock
from ..stats. samples import ExpDecayingSample, DEFAULT_SIZE, DEFAULT_ALPHA
from ..stats.samples import ExpDecayingSample, DEFAULT_SIZE, DEFAULT_ALPHA


class Histogram(object):
Expand All @@ -10,8 +10,7 @@ class Histogram(object):
A metric which calculates the distribution of a value.
"""

def __init__(self, size=DEFAULT_SIZE, alpha=DEFAULT_ALPHA, clock=time,
sample=None):
def __init__(self, size=DEFAULT_SIZE, alpha=DEFAULT_ALPHA, clock=time, sample=None):
"""
Creates a new instance of a L{Histogram}.
"""
Expand Down
22 changes: 16 additions & 6 deletions pyformance/meters/timer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time

try:
from blinker import Namespace
except ImportError:
Expand All @@ -21,8 +22,15 @@ class Timer(object):
"""

def __init__(self, threshold=None, size=DEFAULT_SIZE, alpha=DEFAULT_ALPHA,
clock=time, sink=None, sample=None):
def __init__(
self,
threshold=None,
size=DEFAULT_SIZE,
alpha=DEFAULT_ALPHA,
clock=time,
sink=None,
sample=None,
):
super(Timer, self).__init__()
self.meter = Meter(clock=clock)
self.hist = Histogram(size=size, alpha=alpha, clock=clock, sample=sample)
Expand Down Expand Up @@ -99,7 +107,6 @@ def clear(self):


class TimerContext(object):

def __init__(self, timer, clock, *args, **kwargs):
super(TimerContext, self).__init__()
self.clock = clock
Expand All @@ -111,9 +118,12 @@ def __init__(self, timer, clock, *args, **kwargs):
def stop(self):
elapsed = self.clock.time() - self.start_time
self.timer._update(elapsed)
if self.timer.threshold and self.timer.threshold < elapsed and call_too_long is not None:
call_too_long.send(
self.timer, elapsed=elapsed, *self.args, **self.kwargs)
if (
self.timer.threshold
and self.timer.threshold < elapsed
and call_too_long is not None
):
call_too_long.send(self.timer, elapsed=elapsed, *self.args, **self.kwargs)
return elapsed

def __enter__(self):
Expand Down
Loading

0 comments on commit b71056e

Please sign in to comment.