Changeset - 9d6c7cab31b0
[Not reviewed]
default
0 1 1
drewp@bigasterisk.com - 20 months ago 2023-05-24 06:44:13
drewp@bigasterisk.com
refactor, though i think i want to remove this since it's redundant with metrics
2 files changed with 30 insertions and 29 deletions:
0 comments (0 inline, 0 general)
light9/metrics.py
Show inline comments
 
@@ -78,77 +78,48 @@ def metrics(name: str, **labels):
 

	
 

	
 
class _CycloneMetrics(cyclone.web.RequestHandler):
 

	
 
    def get(self):
 
        self.add_header('content-type', 'text/plain')
 
        self.write(generate_latest(REGISTRY))
 

	
 

	
 
def metricsRoute() -> Tuple[str, Type[cyclone.web.RequestHandler]]:
 
    return ('/metrics', _CycloneMetrics)
 

	
 

	
 
"""
 
stuff we used to have in greplin. Might be nice to get (client-side-computed) min/max/stddev back.
 

	
 
class PmfStat(Stat):
 
  A stat that stores min, max, mean, standard deviation, and some
 
  percentiles for arbitrary floating-point data. This is potentially a
 
  bit expensive, so its child values are only updated once every
 
  twenty seconds.
 

	
 

	
 

	
 
metrics consumer side can do this with the changing counts:
 

	
 
class RecentFps:
 
  def __init__(self, window=20):
 
    self.window = window
 
    self.recentTimes = []
 

	
 
  def mark(self):
 
    now = time.time()
 
    self.recentTimes.append(now)
 
    self.recentTimes = self.recentTimes[-self.window:]
 

	
 
  def rate(self):
 
    def dec(innerFunc):
 
      def f(*a, **kw):
 
        self.mark()
 
        return innerFunc(*a, **kw)
 
      return f
 
    return dec
 

	
 
  def __call__(self):
 
    if len(self.recentTimes) < 2:
 
      return {}
 
    recents = sorted(round(1 / (b - a), 3)
 
                      for a, b in zip(self.recentTimes[:-1],
 
                                      self.recentTimes[1:]))
 
    avg = (len(self.recentTimes) - 1) / (
 
      self.recentTimes[-1] - self.recentTimes[0])
 
    return {'average': round(avg, 5), 'recents': recents}
 

	
 

	
 
i think prometheus covers this one:
 

	
 
import psutil
 
def gatherProcessStats():
 
    procStats = scales.collection('/process',
 
                                  scales.DoubleStat('time'),
 
                                  scales.DoubleStat('cpuPercent'),
 
                                  scales.DoubleStat('memMb'),
 
    )
 
    proc = psutil.Process()
 
    lastCpu = [0.]
 
    def updateTimeStat():
 
        now = time.time()
 
        procStats.time = round(now, 3)
 
        if now - lastCpu[0] > 3:
 
            procStats.cpuPercent = round(proc.cpu_percent(), 6) # (since last call)
 
            lastCpu[0] = now
 
        procStats.memMb = round(proc.memory_info().rss / 1024 / 1024, 6)
 
    task.LoopingCall(updateTimeStat).start(.1)
 

	
 
"""
 

	
light9/recentfps.py
Show inline comments
 
new file 100644
 
# server side version of what the metrics consumer does with changing counts
 
import time
 

	
 
class RecentFps:
 
  def __init__(self, window=20):
 
    self.window = window
 
    self.recentTimes = []
 

	
 
  def mark(self):
 
    now = time.time()
 
    self.recentTimes.append(now)
 
    self.recentTimes = self.recentTimes[-self.window:]
 

	
 
  def rate(self):
 
    def dec(innerFunc):
 
      def f(*a, **kw):
 
        self.mark()
 
        return innerFunc(*a, **kw)
 
      return f
 
    return dec
 

	
 
  def __call__(self):
 
    if len(self.recentTimes) < 2:
 
      return {}
 
    recents = sorted(round(1 / (b - a), 3)
 
                      for a, b in zip(self.recentTimes[:-1],
 
                                      self.recentTimes[1:]))
 
    avg = (len(self.recentTimes) - 1) / (
 
      self.recentTimes[-1] - self.recentTimes[0])
 
    return {'average': round(avg, 5), 'recents': recents}
0 comments (0 inline, 0 general)